Skip to content

Commit 22a67fe

Browse files
committed
test
1 parent 6321c8f commit 22a67fe

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

tests/unit/plugins/ProjectManager.nuxt.test.js renamed to tests/unit/composables/ProjectManager.nuxt.test.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { beforeEach, describe, expect, test, vi } from "vitest"
2-
import { setActivePinia, createPinia } from "pinia"
2+
import { setActivePinia } from "pinia"
3+
import { createTestingPinia } from "@pinia/testing"
34
import { useProjectManager } from "@/composables/project_manager.js"
45

56
// Mocks
@@ -12,10 +13,19 @@ const mockInfraStore = { create_connection: vi.fn() }
1213
vi.mock("@/stores/app.js", () => ({ useAppStore: () => mockAppStore }))
1314
vi.mock("@ogw_f/stores/infra", () => ({ useInfraStore: () => mockInfraStore }))
1415
vi.mock("@/composables/viewer_call.js", () => ({
15-
default: vi.fn(),
1616
viewer_call: vi.fn(),
1717
}))
1818

19+
beforeEach(async () => {
20+
const pinia = createTestingPinia({
21+
stubActions: false,
22+
createSpy: vi.fn,
23+
})
24+
setActivePinia(pinia)
25+
const geode_store = useGeodeStore()
26+
await geode_store.$reset()
27+
geode_store.base_url = ""
28+
})
1929
vi.mock("@/stores/geode.js", () => ({
2030
useGeodeStore: () => ({
2131
base_url: "http://localhost:5000",
@@ -49,32 +59,27 @@ vi.mock(
4959
}),
5060
)
5161

52-
beforeEach(() => setActivePinia(createPinia()))
53-
5462
describe("ProjectManager composable", () => {
5563
test("exportProject triggers download", async () => {
56-
const mockElement = { href: "", download: "", click: vi.fn() }
57-
vi.stubGlobal("document", { createElement: () => mockElement })
58-
vi.stubGlobal("URL", {
59-
createObjectURL: () => "blob:url",
60-
revokeObjectURL: vi.fn(),
64+
const clickSpy = vi
65+
.spyOn(HTMLAnchorElement.prototype, "click")
66+
.mockImplementation(() => {})
67+
vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:url")
68+
vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {})
69+
const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue({
70+
ok: true,
71+
blob: async () => new Blob(["zipcontent"], { type: "application/zip" }),
72+
headers: { get: () => 'attachment; filename="project_123.zip"' },
73+
statusText: "OK",
6174
})
62-
vi.stubGlobal(
63-
"fetch",
64-
vi.fn(async () => ({
65-
ok: true,
66-
blob: async () =>
67-
new Blob(["zipcontent"], { type: "application/zip" }),
68-
headers: { get: () => "project_123.zip" },
69-
})),
70-
)
7175

7276
const { exportProject } = useProjectManager()
7377
await exportProject()
7478

75-
expect(mockAppStore.exportStore).toHaveBeenCalled()
76-
expect(fetch).toHaveBeenCalledTimes(1)
77-
expect(mockElement.click).toHaveBeenCalled()
79+
const app_store = useAppStore()
80+
expect(app_store.exportStore).toHaveBeenCalled()
81+
expect(fetchSpy).toHaveBeenCalledTimes(1)
82+
expect(clickSpy).toHaveBeenCalled()
7883
})
7984

8085
test("importProjectFile loads snapshot", async () => {
@@ -83,8 +88,12 @@ describe("ProjectManager composable", () => {
8388
const file = { text: () => Promise.resolve('{"dataBase":{"db":{}}}') }
8489
await importProjectFile(file)
8590

86-
expect(mockInfraStore.create_connection).toHaveBeenCalled()
87-
expect((await import("@/composables/viewer_call.js")).viewer_call).toHaveBeenCalled()
88-
expect(mockAppStore.importStore).toHaveBeenCalled()
91+
const infra_store = useInfraStore()
92+
const app_store = useAppStore()
93+
const { viewer_call } = await import("@/composables/viewer_call.js")
94+
95+
expect(infra_store.create_connection).toHaveBeenCalled()
96+
expect(viewer_call).toHaveBeenCalled()
97+
expect(app_store.importStore).toHaveBeenCalled()
8998
})
9099
})

0 commit comments

Comments
 (0)