Skip to content

Commit 897f087

Browse files
committed
routeTests.spec.js update to Pinia
1 parent bdd37b0 commit 897f087

File tree

2 files changed

+154
-86
lines changed

2 files changed

+154
-86
lines changed

test/jest/__tests__/copyPaste.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { createApp } from "vue";
33
import * as All from "quasar";
44
import { setActivePinia, createPinia } from 'pinia'
5-
import * as types from "../../../types";
5+
import * as types from "types.ts";
66
import { useStore } from "src/store/main.js";
77
// import Vuex from "vuex";
88
const { Quasar, date } = All;

test/jest/__tests__/routeTests.spec.js

Lines changed: 153 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @jest-environment jsdom
44
*/
55
// import { mount, createLocalVue, shallowMount } from "@vue/test-utils";
6-
import { mount, shallowMount } from "@vue/test-utils";
6+
// import { mount, shallowMount } from "@vue/test-utils";
77
import { createApp } from "vue";
88
import * as All from "quasar";
99
const { Quasar, date } = All;
@@ -16,11 +16,15 @@ const components = Object.keys(All).reduce((object, key) => {
1616
return object;
1717
}, {});
1818

19-
import actions from "../../../src/store/options/actions";
20-
import mutations from "../../../src/store/options/mutations";
21-
import * as types from "../../../src/store/options/types";
22-
import Vuex from "vuex";
23-
import store from "../../../src/store/state/index";
19+
// import actions from "../../../src/store/options/actions";
20+
// import mutations from "../../../src/store/options/mutations";
21+
// import * as types from "../../../src/store/options/types";
22+
// import Vuex from "vuex";
23+
// import store from "../../../src/store/state/index";
24+
import { setActivePinia, createPinia } from 'pinia'
25+
import * as types from "../../../types";
26+
import { useStore } from "src/store/main.js";
27+
2428

2529
/**
2630
* @description: Testing functionality of route actions and mutations
@@ -34,116 +38,180 @@ describe("Test Suite for route actions and related mutations", () => {
3438
// localVue.use(Quasar, Vuex, { components });
3539
const App = {};
3640
const app = createApp(App);
37-
app.use(Quasar, Vuex, { components });
38-
const state = { ...store };
39-
40-
test('"[types.addRouteToRouteMap]" action to commit 4 mutations', () => {
41-
const commit = jest.fn();
41+
const pinia = setActivePinia(createPinia());
42+
app.use(Quasar, pinia, { components });
43+
const store = useStore();
44+
// const state = { ...store };
45+
46+
// test('"[types.addRouteToRouteMap]" action to commit 4 mutations', () => {
47+
// const commit = jest.fn();
48+
// const payload = "testRoute";
49+
// actions[types.addRouteToRouteMap]({ state, commit }, payload);
50+
// expect(commit).toHaveBeenCalledWith(types.ADD_ROUTE, payload);
51+
// expect(commit).toHaveBeenCalledWith(types.SET_ACTIVE_ROUTE, payload);
52+
// expect(commit).toHaveBeenCalledWith(types.ADD_ROUTE_TO_COMPONENT_MAP, {
53+
// route: state.activeRoute,
54+
// children: [],
55+
// });
56+
// expect(commit).toHaveBeenCalledWith(
57+
// types.ADD_COMPONENT_TO_COMPONENT_CHILDREN,
58+
// {
59+
// component: "App",
60+
// value: state.componentMap[state.activeRoute].componentName,
61+
// }
62+
// );
63+
// });
64+
65+
// test('"[types.setActiveRoute]" action to commit SET_ACTIVE_ROUTE mutation with string payload', () => {
66+
// const commit = jest.fn();
67+
// const payload = "HomeView";
68+
// actions[types.setActiveRoute]({ state, commit }, payload);
69+
// expect(commit).toHaveBeenCalledWith(types.SET_ACTIVE_ROUTE, payload);
70+
// });
71+
72+
// test('"[types.deleteRoute]" action to commit DELETE_ROUTE mutation with string payload', () => {
73+
// const commit = jest.fn();
74+
// const payload = "HomeView";
75+
// actions[types.deleteRoute]({ state, commit }, payload);
76+
// expect(commit).toHaveBeenCalledWith(types.DELETE_ROUTE, payload);
77+
// });
78+
79+
test('addRoute function to add new route and set route imagePath to default', () => {
4280
const payload = "testRoute";
43-
actions[types.addRouteToRouteMap]({ state, commit }, payload);
44-
expect(commit).toHaveBeenCalledWith(types.ADD_ROUTE, payload);
45-
expect(commit).toHaveBeenCalledWith(types.SET_ACTIVE_ROUTE, payload);
46-
expect(commit).toHaveBeenCalledWith(types.ADD_ROUTE_TO_COMPONENT_MAP, {
47-
route: state.activeRoute,
48-
children: [],
49-
});
50-
expect(commit).toHaveBeenCalledWith(
51-
types.ADD_COMPONENT_TO_COMPONENT_CHILDREN,
52-
{
53-
component: "App",
54-
value: state.componentMap[state.activeRoute].componentName,
55-
}
56-
);
81+
store.addRoute(payload);
82+
expect(store.routes).toMatchObject({ HomeView: [], [payload]: [] });
5783
});
5884

59-
test('"[types.setActiveRoute]" action to commit SET_ACTIVE_ROUTE mutation with string payload', () => {
60-
const commit = jest.fn();
61-
const payload = "HomeView";
62-
actions[types.setActiveRoute]({ state, commit }, payload);
63-
expect(commit).toHaveBeenCalledWith(types.SET_ACTIVE_ROUTE, payload);
64-
});
65-
66-
test('"[types.deleteRoute]" action to commit DELETE_ROUTE mutation with string payload', () => {
67-
const commit = jest.fn();
68-
const payload = "HomeView";
69-
actions[types.deleteRoute]({ state, commit }, payload);
70-
expect(commit).toHaveBeenCalledWith(types.DELETE_ROUTE, payload);
71-
});
72-
73-
test('"[types.ADD_ROUTE]" mutation to add new route and set route imagePath to default', () => {
74-
const payload = "testRoute";
75-
mutations[types.ADD_ROUTE](state, payload);
76-
expect(state.routes).toMatchObject({ HomeView: [], [payload]: [] });
77-
});
85+
// test('"[types.ADD_ROUTE]" mutation to add new route and set route imagePath to default', () => {
86+
// const payload = "testRoute";
87+
// mutations[types.ADD_ROUTE](state, payload);
88+
// expect(state.routes).toMatchObject({ HomeView: [], [payload]: [] });
89+
// });
7890

79-
test('"[types.SET_ACTIVE_ROUTE]" mutation to ---', () => {
91+
test('setActiveRoute funtion to ---', () => {
8092
const payload = "testRoute";
8193

8294
// expect at test start for there to be two routes and HomeView to be active Route
83-
expect(state.routes).toMatchObject({ HomeView: [], [payload]: [] });
84-
expect(state.activeRoute).toBe("HomeView");
95+
expect(store.routes).toMatchObject({ HomeView: [], [payload]: [] });
96+
expect(store.activeRoute).toBe("HomeView");
8597

8698
// start test
87-
mutations[types.SET_ACTIVE_ROUTE](state, payload);
88-
expect(state.activeRoute).toBe(payload);
99+
store.setActiveRoute(payload);
100+
expect(store.activeRoute).toBe(payload);
89101
});
90102

91-
test('"[types.ADD_ROUTE_TO_COMPONENT_MAP]" mutation to add route object containing 3 keys to state.componentMap', () => {
92-
expect(Object.keys(state.componentMap).length).toBe(2);
93-
mutations[types.ADD_ROUTE_TO_COMPONENT_MAP](state, {
94-
route: state.activeRoute,
103+
104+
// test('"[types.SET_ACTIVE_ROUTE]" mutation to ---', () => {
105+
// const payload = "testRoute";
106+
107+
// // expect at test start for there to be two routes and HomeView to be active Route
108+
// expect(state.routes).toMatchObject({ HomeView: [], [payload]: [] });
109+
// expect(state.activeRoute).toBe("HomeView");
110+
111+
// // start test
112+
// mutations[types.SET_ACTIVE_ROUTE](state, payload);
113+
// expect(state.activeRoute).toBe(payload);
114+
// });
115+
116+
test('addRouteToComponentMap function to add route object containing 3 keys to store.componentMap', () => {
117+
expect(Object.keys(store.componentMap).length).toBe(2);
118+
store.addRouteToComponentMap({
119+
route: store.activeRoute,
95120
children: [],
96121
});
97-
expect(Object.keys(state.componentMap).length).toBe(3);
122+
expect(Object.keys(store.componentMap).length).toBe(3);
98123
// to contain componentName, children, and htmlList keys
99-
expect(Object.keys(state.componentMap[state.activeRoute])).toMatchObject([
124+
expect(Object.keys(store.componentMap[store.activeRoute])).toMatchObject([
100125
"componentName",
101126
"children",
102127
"htmlList",
103128
]);
104129
});
105130

106-
test('"[types.ADD_COMPONENT_TO_COMPONENT_CHILDREN]" mutation to add created route as one of Apps children', () => {
107-
let component = "App";
108-
let value = state.componentMap[state.activeRoute].componentName;
109-
expect(state.componentMap[component].children.length).toBe(1);
110-
mutations[types.ADD_COMPONENT_TO_COMPONENT_CHILDREN](state, {
111-
component,
112-
value,
113-
});
114-
expect(state.componentMap[component].children.length).toBe(2);
115-
expect(
116-
state.componentMap[component].children[
117-
state.componentMap[component].children.length - 1
118-
]
119-
).toBe(value);
120-
});
121-
122-
test('"[types.DELETE_ROUTE]" mutation to update state by removing a route', () => {
131+
// test('"[types.ADD_ROUTE_TO_COMPONENT_MAP]" mutation to add route object containing 3 keys to state.componentMap', () => {
132+
// expect(Object.keys(state.componentMap).length).toBe(2);
133+
// mutations[types.ADD_ROUTE_TO_COMPONENT_MAP](state, {
134+
// route: state.activeRoute,
135+
// children: [],
136+
// });
137+
// expect(Object.keys(state.componentMap).length).toBe(3);
138+
// // to contain componentName, children, and htmlList keys
139+
// expect(Object.keys(state.componentMap[state.activeRoute])).toMatchObject([
140+
// "componentName",
141+
// "children",
142+
// "htmlList",
143+
// ]);
144+
// });
145+
146+
// test('"[types.ADD_COMPONENT_TO_COMPONENT_CHILDREN]" mutation to add created route as one of Apps children', () => {
147+
// let component = "App";
148+
// let value = state.componentMap[state.activeRoute].componentName;
149+
// expect(state.componentMap[component].children.length).toBe(1);
150+
// mutations[types.ADD_COMPONENT_TO_COMPONENT_CHILDREN](state, {
151+
// component,
152+
// value,
153+
// });
154+
// expect(state.componentMap[component].children.length).toBe(2);
155+
// expect(
156+
// state.componentMap[component].children[
157+
// state.componentMap[component].children.length - 1
158+
// ]
159+
// ).toBe(value);
160+
// });
161+
162+
test('"function deleteRoute to update state by removing a route', () => {
123163
const payload = "testRoute";
124164
let flag = false;
125165
// pre mutation tests
126-
expect(state.routes[payload]).not.toBe(undefined);
127-
expect(state.componentMap[payload]).not.toBe(undefined);
128-
expect(state.imagePath[payload]).not.toBe(undefined);
129-
if (state.activeRoute === payload) {
166+
expect(store.routes[payload]).not.toBe(undefined);
167+
expect(store.componentMap[payload]).not.toBe(undefined);
168+
expect(store.imagePath[payload]).not.toBe(undefined);
169+
if (store.activeRoute === payload) {
130170
flag = true;
131171
}
132-
mutations[types.DELETE_ROUTE](state, payload);
172+
store.deleteRoute(payload);
133173

134174
// post mutation tests
135-
expect(state.routes[payload]).toBe(undefined);
136-
expect(state.componentMap[payload]).toBe(undefined);
137-
expect(state.imagePath[payload]).toBe(undefined);
175+
expect(store.routes[payload]).toBe(undefined);
176+
expect(store.componentMap[payload]).toBe(undefined);
177+
expect(store.imagePath[payload]).toBe(undefined);
138178
if (flag) {
139-
expect(state.activeRoute).not.toBe(payload);
179+
expect(store.activeRoute).not.toBe(payload);
140180
}
141181
});
142182

143-
test('"[types.SET_ACTIVE_ROUTE_ARRAY]" mutation to set state.routes[state.activeRoute] to newActiveRouteArray', () => {
183+
// test('"[types.DELETE_ROUTE]" mutation to update state by removing a route', () => {
184+
// const payload = "testRoute";
185+
// let flag = false;
186+
// // pre mutation tests
187+
// expect(state.routes[payload]).not.toBe(undefined);
188+
// expect(state.componentMap[payload]).not.toBe(undefined);
189+
// expect(state.imagePath[payload]).not.toBe(undefined);
190+
// if (state.activeRoute === payload) {
191+
// flag = true;
192+
// }
193+
// mutations[types.DELETE_ROUTE](state, payload);
194+
195+
// // post mutation tests
196+
// expect(state.routes[payload]).toBe(undefined);
197+
// expect(state.componentMap[payload]).toBe(undefined);
198+
// expect(state.imagePath[payload]).toBe(undefined);
199+
// if (flag) {
200+
// expect(state.activeRoute).not.toBe(payload);
201+
// }
202+
// });
203+
204+
test('setActiveRouteArray function to set store.routes[store.activeRoute] to newActiveRouteArray', () => {
144205
// invoked by delete active component action
145-
const newActiveRouteArray = state.routes[state.activeRoute];
146-
mutations[types.SET_ACTIVE_ROUTE_ARRAY](state, newActiveRouteArray);
147-
expect(state.routes[state.activeRoute]).toBe(newActiveRouteArray);
206+
const newActiveRouteArray = store.routes[store.activeRoute];
207+
store.setActiveRouteArray(newActiveRouteArray);
208+
expect(store.routes[store.activeRoute]).toBe(newActiveRouteArray);
148209
});
210+
211+
// test('"[types.SET_ACTIVE_ROUTE_ARRAY]" mutation to set state.routes[state.activeRoute] to newActiveRouteArray', () => {
212+
// // invoked by delete active component action
213+
// const newActiveRouteArray = state.routes[state.activeRoute];
214+
// mutations[types.SET_ACTIVE_ROUTE_ARRAY](state, newActiveRouteArray);
215+
// expect(state.routes[state.activeRoute]).toBe(newActiveRouteArray);
216+
// });
149217
});

0 commit comments

Comments
 (0)