Skip to content

Commit d4a1cd6

Browse files
committed
exportStores/importStores and createTestingPinia
1 parent 18fc885 commit d4a1cd6

File tree

10 files changed

+209
-119
lines changed

10 files changed

+209
-119
lines changed

composables/project_manager.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import { useAppStore } from "@/stores/app.js"
2-
import { useInfraStore } from "@ogw_f/stores/infra"
3-
import { viewer_call } from "@/composables/viewer_call.js"
4-
import { useGeodeStore } from "@/stores/geode.js"
51
import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_schemas.json"
62
import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_schemas.json"
73

@@ -13,10 +9,10 @@ export function useProjectManager() {
139
geode.start_request()
1410
try {
1511
await useInfraStore().create_connection()
16-
const snapshot = appStore.exportStore()
12+
const snapshot = appStore.exportStores()
1713

1814
const schema = back_schemas.opengeodeweb_back.project.export_project
19-
const url = `${geode.base_url}${schema.route}`
15+
const url = `${geode.base_url}${schema.$id}`
2016
const method = schema.methods[0]
2117

2218
const response = await fetch(url, {
@@ -55,7 +51,7 @@ export function useProjectManager() {
5551
params: {},
5652
})
5753

58-
await appStore.importStore(snapshot)
54+
await appStore.importStores(snapshot)
5955
} finally {
6056
geode.stop_request()
6157
}

package-lock.json

Lines changed: 134 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stores/app.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ export const useAppStore = defineStore("app", () => {
1515
stores.push(store)
1616
}
1717

18-
function exportStore() {
18+
function exportStores() {
1919
const snapshot = {}
2020
let exportCount = 0
2121

2222
for (const store of stores) {
23-
if (!store.exportStore) {
23+
if (!store.exportStores) {
2424
continue
2525
}
2626
const storeId = store.$id
2727
try {
28-
snapshot[storeId] = store.exportStore()
28+
snapshot[storeId] = store.exportStores()
2929
exportCount++
3030
} catch (error) {
3131
console.error(`[AppStore] Error exporting store "${storeId}":`, error)
@@ -35,22 +35,22 @@ export const useAppStore = defineStore("app", () => {
3535
return snapshot
3636
}
3737

38-
async function importStore(snapshot) {
38+
async function importStores(snapshot) {
3939
if (!snapshot) {
4040
console.warn("[AppStore] import called with invalid snapshot")
4141
return
4242
}
4343
let importedCount = 0
4444
const notFoundStores = []
4545
for (const store of stores) {
46-
if (!store.importStore) continue
46+
if (!store.importStores) continue
4747
const storeId = store.$id
4848
if (!snapshot[storeId]) {
4949
notFoundStores.push(storeId)
5050
continue
5151
}
5252
try {
53-
await store.importStore(snapshot[storeId])
53+
await store.importStores(snapshot[storeId])
5454
importedCount++
5555
} catch (error) {
5656
console.error(`[AppStore] Error importing store "${storeId}":`, error)
@@ -67,7 +67,7 @@ export const useAppStore = defineStore("app", () => {
6767
return {
6868
stores,
6969
registerStore,
70-
exportStore,
71-
importStore,
70+
exportStores,
71+
importStores,
7272
}
7373
})

stores/data_base.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_schemas.json"
22
import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_schemas.json"
3-
import { viewer_call } from "@/composables/viewer_call.js"
43

54
export const useDataBaseStore = defineStore("dataBase", () => {
65
const treeview_store = useTreeviewStore()
@@ -135,11 +134,11 @@ export const useDataBaseStore = defineStore("dataBase", () => {
135134
return flat_indexes.filter((index) => index !== null)
136135
}
137136

138-
function exportStore() {
137+
function exportStores() {
139138
return { db: JSON.parse(JSON.stringify(db)) }
140139
}
141140

142-
async function importStore(snapshot) {
141+
async function importStores(snapshot) {
143142
const entries = snapshot?.db || {}
144143
const hybrid_store = useHybridViewerStore()
145144
await hybrid_store.initHybridViewer()
@@ -164,7 +163,7 @@ export const useDataBaseStore = defineStore("dataBase", () => {
164163
getSurfacesUuids,
165164
getBlocksUuids,
166165
getFlatIndexes,
167-
exportStore,
168-
importStore,
166+
exportStores,
167+
importStores,
169168
}
170169
})

stores/data_style.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ export const useDataStyleStore = defineStore("dataStyle", () => {
4646
return modelStyleStore.modelMeshComponentVisibility(id, "Edge", null)
4747
}
4848

49-
function exportStore() {
49+
function exportStores() {
5050
return { styles: dataStyleState.styles }
5151
}
5252

53-
async function importStore(snapshot) {
53+
async function importStores(snapshot) {
5454
dataStyleState.styles = snapshot?.styles || {}
5555
}
5656

@@ -60,8 +60,8 @@ export const useDataStyleStore = defineStore("dataStyle", () => {
6060
setVisibility,
6161
setModelEdgesVisibility,
6262
modelEdgesVisibility,
63-
exportStore,
64-
importStore,
63+
exportStores,
64+
importStores,
6565
...meshStyleStore,
6666
...modelStyleStore,
6767
}

stores/hybrid_viewer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ export const useHybridViewerStore = defineStore("hybridViewer", () => {
192192
Object.keys(db).forEach((id) => delete db[id])
193193
}
194194

195-
function exportStore() {
195+
function exportStores() {
196196
return { zScale: zScale.value }
197197
}
198198

199-
async function importStore(snapshot) {
199+
async function importStores(snapshot) {
200200
const z_scale = snapshot?.zScale
201201
if (z_scale != null) {
202202
await setZScaling(z_scale)
@@ -215,7 +215,7 @@ export const useHybridViewerStore = defineStore("hybridViewer", () => {
215215
setContainer,
216216
zScale,
217217
clear,
218-
exportStore,
219-
importStore,
218+
exportStores,
219+
importStores,
220220
}
221221
})

stores/treeview.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ export const useTreeviewStore = defineStore("treeview", () => {
1717
displayed_name,
1818
id,
1919
object_type,
20-
autoSelect = true,
2120
) {
2221
dataStyleStore.addDataStyle(id, geodeObject, object_type)
2322
const child = { title: displayed_name, id, object_type }
2423
for (let i = 0; i < items.value.length; i++) {
2524
if (items.value[i].title === geodeObject) {
2625
items.value[i].children.push(child)
27-
if (autoSelect) selection.value.push(child)
26+
selection.value.push(child)
2827
return
2928
}
3029
}
3130
items.value.push({ title: geodeObject, children: [child] })
32-
if (autoSelect) selection.value.push(child)
31+
selection.value.push(child)
3332
}
3433

3534
function displayAdditionalTree(id) {
@@ -53,7 +52,7 @@ export const useTreeviewStore = defineStore("treeview", () => {
5352
panelWidth.value = width
5453
}
5554

56-
function exportStore() {
55+
function exportStores() {
5756
return {
5857
isAdditionnalTreeDisplayed: isAdditionnalTreeDisplayed.value,
5958
panelWidth: panelWidth.value,
@@ -64,7 +63,7 @@ export const useTreeviewStore = defineStore("treeview", () => {
6463
}
6564
}
6665

67-
async function importStore(snapshot) {
66+
async function importStores(snapshot) {
6867
selection.value = snapshot?.selection || []
6968
isAdditionnalTreeDisplayed.value =
7069
snapshot?.isAdditionnalTreeDisplayed || false
@@ -87,7 +86,7 @@ export const useTreeviewStore = defineStore("treeview", () => {
8786
displayFileTree,
8887
toggleTreeView,
8988
setPanelWidth,
90-
exportStore,
91-
importStore,
89+
exportStores,
90+
importStores,
9291
}
9392
})

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { useProjectManager } from "@/composables/project_manager.js"
55

66
// Mocks
77
const mockAppStore = {
8-
exportStore: vi.fn(() => ({ projectName: "mockedProject" })),
9-
importStore: vi.fn(),
8+
exportStores: vi.fn(() => ({ projectName: "mockedProject" })),
9+
importStores: vi.fn(),
1010
}
1111
const mockInfraStore = { create_connection: vi.fn() }
1212

@@ -26,20 +26,12 @@ beforeEach(async () => {
2626
await geode_store.$reset()
2727
geode_store.base_url = ""
2828
})
29-
vi.mock("@/stores/geode.js", () => ({
30-
useGeodeStore: () => ({
31-
base_url: "http://localhost:5000",
32-
start_request: vi.fn(),
33-
stop_request: vi.fn(),
34-
do_ping: vi.fn(),
35-
}),
36-
}))
3729

3830
vi.mock("@geode/opengeodeweb-back/opengeodeweb_back_schemas.json", () => ({
3931
default: {
4032
opengeodeweb_back: {
4133
project: {
42-
export_project: { route: "/project/export_project", methods: ["POST"] },
34+
export_project: { $id: "/project/export_project", methods: ["POST"] },
4335
},
4436
},
4537
},
@@ -71,7 +63,7 @@ describe("ProjectManager composable", () => {
7163
await exportProject()
7264

7365
const app_store = useAppStore()
74-
expect(app_store.exportStore).toHaveBeenCalled()
66+
expect(app_store.exportStores).toHaveBeenCalled()
7567
expect(fetchSpy).toHaveBeenCalledTimes(1)
7668
expect(clickSpy).toHaveBeenCalled()
7769
})
@@ -88,6 +80,6 @@ describe("ProjectManager composable", () => {
8880

8981
expect(infra_store.create_connection).toHaveBeenCalled()
9082
expect(viewer_call).toHaveBeenCalled()
91-
expect(app_store.importStore).toHaveBeenCalled()
83+
expect(app_store.importStores).toHaveBeenCalled()
9284
})
9385
})

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

Lines changed: 12 additions & 4 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 { useAppStore } from "@/stores/app.js"
45
import { useDataBaseStore } from "@/stores/data_base.js"
56
import { useTreeviewStore } from "@/stores/treeview.js"
@@ -23,10 +24,17 @@ vi.mock("@/stores/hybrid_viewer.js", () => ({
2324
}),
2425
}))
2526

26-
beforeEach(() => setActivePinia(createPinia()))
27+
beforeEach(() => {
28+
setActivePinia(
29+
createTestingPinia({
30+
stubActions: false,
31+
createSpy: vi.fn,
32+
}),
33+
)
34+
})
2735

2836
describe("Project import", () => {
29-
test("app.importStore restores stores", async () => {
37+
test("app.importStores restores stores", async () => {
3038
const stores = {
3139
app: useAppStore(),
3240
dataBase: useDataBaseStore(),
@@ -75,7 +83,7 @@ describe("Project import", () => {
7583
JSON.stringify(snapshot.dataStyle, null, 2),
7684
)
7785

78-
await stores.app.importStore(snapshot)
86+
await stores.app.importStores(snapshot)
7987

8088
console.log(
8189
"[TEST ProjectImport] Treeview items after import:",

0 commit comments

Comments
 (0)