|
| 1 | +import { flushPromises, mount } from "@vue/test-utils"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import type { BoardConfig } from "@/types/api"; |
| 5 | + |
| 6 | +const listBoards = vi.fn(); |
| 7 | +const listSessions = vi.fn(); |
| 8 | +const deleteBoard = vi.fn(); |
| 9 | +const uiStore = { |
| 10 | + clearMessages: vi.fn(), |
| 11 | + setError: vi.fn(), |
| 12 | + setSuccess: vi.fn(), |
| 13 | + confirm: vi.fn(), |
| 14 | +}; |
| 15 | + |
| 16 | +vi.mock("@/api", () => ({ |
| 17 | + api: { |
| 18 | + listBoards, |
| 19 | + listSessions, |
| 20 | + deleteBoard, |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("@/stores/ui", () => ({ |
| 25 | + useUiStore: () => uiStore, |
| 26 | +})); |
| 27 | + |
| 28 | +function makeBoard(id = "rk3568-1"): BoardConfig { |
| 29 | + return { |
| 30 | + id, |
| 31 | + board_type: "rk3568", |
| 32 | + tags: ["lab"], |
| 33 | + serial: null, |
| 34 | + power_management: { |
| 35 | + kind: "custom", |
| 36 | + power_on_cmd: "echo on", |
| 37 | + power_off_cmd: "echo off", |
| 38 | + }, |
| 39 | + boot: { |
| 40 | + kind: "uboot", |
| 41 | + use_tftp: true, |
| 42 | + dtb_name: null, |
| 43 | + kernel_load_addr: null, |
| 44 | + fit_load_addr: null, |
| 45 | + bootm_addr: null, |
| 46 | + network_mode: "dhcp", |
| 47 | + board_ip: null, |
| 48 | + server_ip: null, |
| 49 | + netmask: null, |
| 50 | + gatewayip: null, |
| 51 | + }, |
| 52 | + notes: null, |
| 53 | + disabled: false, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +describe("BoardsView", () => { |
| 58 | + beforeEach(() => { |
| 59 | + listBoards.mockReset(); |
| 60 | + listSessions.mockReset(); |
| 61 | + deleteBoard.mockReset(); |
| 62 | + uiStore.clearMessages.mockReset(); |
| 63 | + uiStore.setError.mockReset(); |
| 64 | + uiStore.setSuccess.mockReset(); |
| 65 | + uiStore.confirm.mockReset(); |
| 66 | + uiStore.confirm.mockResolvedValue(true); |
| 67 | + listBoards.mockResolvedValue([makeBoard()]); |
| 68 | + listSessions.mockResolvedValue({ sessions: [] }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("links board edit actions to the registered edit route", async () => { |
| 72 | + const BoardsView = (await import("./BoardsView.vue")).default; |
| 73 | + const wrapper = mount(BoardsView, { |
| 74 | + global: { |
| 75 | + stubs: { |
| 76 | + RouterLink: { |
| 77 | + name: "RouterLink", |
| 78 | + props: ["to"], |
| 79 | + template: "<a><slot /></a>", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }); |
| 84 | + await flushPromises(); |
| 85 | + |
| 86 | + const editLink = wrapper.findAllComponents({ name: "RouterLink" }).find((link) => { |
| 87 | + return link.text() === "编辑"; |
| 88 | + }); |
| 89 | + |
| 90 | + expect(editLink?.props("to")).toEqual({ |
| 91 | + name: "admin-resource-board-edit", |
| 92 | + params: { boardId: "rk3568-1" }, |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments