Skip to content

Commit ea63e2b

Browse files
committed
testes with import/export
1 parent a93225c commit ea63e2b

File tree

3 files changed

+52
-67
lines changed

3 files changed

+52
-67
lines changed

tests/unit/plugins/ProjectLoad.nuxt.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ vi.mock("@/stores/hybrid_viewer.js", () => ({
2525

2626
beforeEach(() => setActivePinia(createPinia()))
2727

28-
describe("Project load", () => {
29-
test("appStore.load restores stores", async () => {
28+
describe("Project import", () => {
29+
test("app.importStore restores stores", async () => {
3030
const stores = {
3131
app: useAppStore(),
3232
dataBase: useDataBaseStore(),
@@ -65,24 +65,24 @@ describe("Project load", () => {
6565
hybridViewer: { zScale: 1.5 },
6666
}
6767

68-
console.log("[TEST ProjectLoad] Snapshot keys:", Object.keys(snapshot))
68+
console.log("[TEST ProjectImport] Snapshot keys:", Object.keys(snapshot))
6969
console.log(
70-
"[TEST ProjectLoad] treeview snapshot:",
70+
"[TEST ProjectImport] treeview snapshot:",
7171
JSON.stringify(snapshot.treeview, null, 2),
7272
)
7373
console.log(
74-
"[TEST ProjectLoad] dataStyle snapshot:",
74+
"[TEST ProjectImport] dataStyle snapshot:",
7575
JSON.stringify(snapshot.dataStyle, null, 2),
7676
)
7777

78-
await stores.app.load(snapshot)
78+
await stores.app.importStore(snapshot)
7979

8080
console.log(
81-
"[TEST ProjectLoad] Treeview items after load:",
81+
"[TEST ProjectImport] Treeview items after import:",
8282
JSON.stringify(stores.treeview.items, null, 2),
8383
)
8484
console.log(
85-
"[TEST ProjectLoad] Styles after load:",
85+
"[TEST ProjectImport] Styles after import:",
8686
JSON.stringify(stores.dataStyle.styles, null, 2),
8787
)
8888

tests/unit/plugins/ProjectManager.nuxt.test.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { viewer_call } from "@/composables/viewer_call.js"
44

55
// Mocks
66
const mockAppStore = {
7-
save: vi.fn(() => ({ projectName: "mockedProject" })),
8-
load: vi.fn(),
7+
exportStore: vi.fn(() => ({ projectName: "mockedProject" })),
8+
importStore: vi.fn(),
99
}
1010
const mockInfraStore = { create_connection: vi.fn() }
1111

@@ -37,7 +37,7 @@ describe("ProjectManager plugin", () => {
3737
})
3838

3939
projectManagerPlugin.provide.project.export = async () => {
40-
const snapshot = mockAppStore.save()
40+
const snapshot = mockAppStore.exportStore()
4141
const json = JSON.stringify(snapshot, null, 2)
4242
const blob = new Blob([json], { type: "application/json" })
4343
const url = URL.createObjectURL(blob)
@@ -48,9 +48,8 @@ describe("ProjectManager plugin", () => {
4848
URL.revokeObjectURL(url)
4949
console.log("[TEST] URL's project :", { a })
5050
}
51-
5251
await projectManagerPlugin.provide.project.export()
53-
expect(mockAppStore.save).toHaveBeenCalled()
52+
expect(mockAppStore.exportStore).toHaveBeenCalled()
5453
expect(mockElement.click).toHaveBeenCalled()
5554
})
5655

@@ -65,14 +64,12 @@ describe("ProjectManager plugin", () => {
6564
viewer_call.mock?.calls,
6665
viewer_call.mock?.calls?.length || 0,
6766
)
68-
await mockAppStore.load(snapshot)
67+
await mockAppStore.importStore(snapshot)
6968
}
70-
7169
const file = { text: () => Promise.resolve('{"dataBase":{"db":{}}}') }
7270
await projectManagerPlugin.provide.project.importFile(file)
73-
7471
expect(mockInfraStore.create_connection).toHaveBeenCalled()
7572
expect(viewer_call).toHaveBeenCalled()
76-
expect(mockAppStore.load).toHaveBeenCalled()
73+
expect(mockAppStore.importStore).toHaveBeenCalled()
7774
})
7875
})

tests/unit/stores/Appstore.nuxt.test.js

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe("App Store", () => {
1616
test("initial state", () => {
1717
const app_store = useAppStore()
1818
expectTypeOf(app_store.stores).toBeArray()
19-
expectTypeOf(app_store.save).toBeFunction()
20-
expectTypeOf(app_store.load).toBeFunction()
19+
expectTypeOf(app_store.exportStore).toBeFunction()
20+
expectTypeOf(app_store.importStore).toBeFunction()
2121
expectTypeOf(app_store.registerStore).toBeFunction()
2222
})
2323
})
@@ -60,54 +60,54 @@ describe("App Store", () => {
6060
})
6161
})
6262

63-
describe("save", () => {
64-
test("save stores with save method", () => {
63+
describe("Export", () => {
64+
test("export stores with exportStore method", () => {
6565
const app_store = useAppStore()
6666
const mock_store_1 = {
6767
$id: "userStore",
68-
save: vi.fn().mockImplementation(() => ({
68+
exportStore: vi.fn().mockImplementation(() => ({
6969
name: "toto",
7070
7171
})),
72-
load: vi.fn().mockImplementation(() => {}),
72+
importStore: vi.fn().mockImplementation(() => {}),
7373
}
7474
const mock_store_2 = {
7575
$id: "geodeStore",
76-
save: vi.fn().mockImplementation(() => ({ items: [], total: 0 })),
77-
load: vi.fn().mockImplementation(() => {}),
76+
exportStore: vi.fn().mockImplementation(() => ({ items: [], total: 0 })),
77+
importStore: vi.fn().mockImplementation(() => {}),
7878
}
7979

8080
app_store.registerStore(mock_store_1)
8181
app_store.registerStore(mock_store_2)
8282

83-
const snapshot = app_store.save()
83+
const snapshot = app_store.exportStore()
8484

85-
expect(mock_store_1.save).toHaveBeenCalledTimes(1)
86-
expect(mock_store_2.save).toHaveBeenCalledTimes(1)
85+
expect(mock_store_1.exportStore).toHaveBeenCalledTimes(1)
86+
expect(mock_store_2.exportStore).toHaveBeenCalledTimes(1)
8787
expect(snapshot).toEqual({
8888
userStore: { name: "toto", email: "[email protected]" },
8989
geodeStore: { items: [], total: 0 },
9090
})
9191
})
9292

93-
test("skip stores without save method", () => {
93+
test("skip stores without exportSave method", () => {
9494
const app_store = useAppStore()
9595
const mock_store_1 = {
9696
$id: "withSave",
97-
save: vi.fn().mockImplementation(() => ({ data: "test" })),
98-
load: vi.fn().mockImplementation(() => {}),
97+
exportStore: vi.fn().mockImplementation(() => ({ data: "test" })),
98+
importStore: vi.fn().mockImplementation(() => {}),
9999
}
100100
const mock_store_2 = {
101101
$id: "withoutSave",
102-
load: vi.fn().mockImplementation(() => {}),
102+
importStore: vi.fn().mockImplementation(() => {}),
103103
}
104104

105105
app_store.registerStore(mock_store_1)
106106
app_store.registerStore(mock_store_2)
107107

108-
const snapshot = app_store.save()
108+
const snapshot = app_store.exportStore()
109109

110-
expect(mock_store_1.save).toHaveBeenCalledTimes(1)
110+
expect(mock_store_1.exportStore).toHaveBeenCalledTimes(1)
111111
expect(snapshot).toEqual({
112112
withSave: { data: "test" },
113113
})
@@ -116,76 +116,64 @@ describe("App Store", () => {
116116

117117
test("return empty snapshot when no stores registered", () => {
118118
const app_store = useAppStore()
119-
const snapshot = app_store.save()
119+
const snapshot = app_store.exportStore()
120120
expect(snapshot).toEqual({})
121121
})
122122
})
123123

124124
describe("load", () => {
125-
test("App Store > actions > load > load stores with load method", async () => {
125+
test("App Store > actions > importStore > import stores with importStore method", async () => {
126126
const appStore = useAppStore()
127-
128127
const userStore = {
129128
$id: "userStore",
130-
load: vi.fn().mockResolvedValue(),
129+
importStore: vi.fn().mockResolvedValue(),
131130
}
132131
const geodeStore = {
133132
$id: "geodeStore",
134-
load: vi.fn().mockResolvedValue(),
133+
importStore: vi.fn().mockResolvedValue(),
135134
}
136-
137135
appStore.registerStore(userStore)
138136
appStore.registerStore(geodeStore)
139-
140137
const snapshot = {
141138
userStore: { some: "data" },
142139
geodeStore: { other: "data" },
143140
}
144-
145-
await appStore.load(snapshot)
146-
expect(userStore.load).toHaveBeenCalledTimes(1)
147-
expect(geodeStore.load).toHaveBeenCalledTimes(1)
141+
await appStore.importStore(snapshot)
142+
expect(userStore.importStore).toHaveBeenCalledTimes(1)
143+
expect(geodeStore.importStore).toHaveBeenCalledTimes(1)
148144
})
149-
150-
test("skip stores without load method", () => {
145+
146+
test("skip stores without importStore method", () => {
151147
const app_store = useAppStore()
152148
const mock_store_1 = {
153-
$id: "withLoad",
149+
$id: "withImport",
154150
save: vi.fn().mockImplementation(() => {}),
155-
load: vi.fn().mockImplementation(() => {}),
151+
importStore: vi.fn().mockImplementation(() => {}),
156152
}
157153
const mock_store_2 = {
158-
$id: "withoutLoad",
154+
$id: "withoutImport",
159155
save: vi.fn().mockImplementation(() => {}),
160156
}
161-
162157
app_store.registerStore(mock_store_1)
163158
app_store.registerStore(mock_store_2)
164-
165159
const snapshot = {
166-
withLoad: { data: "test" },
167-
withoutLoad: { data: "ignored" },
160+
withImport: { data: "test" },
161+
withoutImport: { data: "ignored" },
168162
}
169-
170-
app_store.load(snapshot)
171-
172-
expect(mock_store_1.load).toHaveBeenCalledTimes(1)
173-
expect(mock_store_2.load).toBeUndefined()
163+
app_store.importStore(snapshot)
164+
expect(mock_store_1.importStore).toHaveBeenCalledTimes(1)
165+
expect(mock_store_2.importStore).toBeUndefined()
174166
})
175-
167+
176168
test("warn when store not found in snapshot", () => {
177169
const app_store = useAppStore()
178-
const console_warn_spy = vi
179-
.spyOn(console, "warn")
180-
.mockImplementation(() => {})
170+
const console_warn_spy = vi.spyOn(console, "warn").mockImplementation(() => {})
181171
const mock_store = {
182172
$id: "testStore",
183-
load: vi.fn().mockImplementation(() => {}),
173+
importStore: vi.fn().mockImplementation(() => {}),
184174
}
185-
186175
app_store.registerStore(mock_store)
187-
app_store.load({})
188-
176+
app_store.importStore({})
189177
expect(console_warn_spy).toHaveBeenCalledWith(
190178
expect.stringContaining("Stores not found in snapshot: testStore"),
191179
)

0 commit comments

Comments
 (0)