Skip to content

Commit 633971c

Browse files
author
Dobromir Hristov
committed
chore: add extra tests to apiChangesObserving.spec.js
1 parent 609f4e8 commit 633971c

File tree

1 file changed

+90
-16
lines changed

1 file changed

+90
-16
lines changed

tests/unit/mixins/apiChangesObserving.spec.js

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,39 @@
66
*
77
* See https://swift.org/LICENSE.txt for license information
88
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
9+
*/
1010

1111
import { shallowMount } from '@vue/test-utils';
1212
import apiChangesObserving from 'docc-render/mixins/apiChangesObserving';
13+
import Vue from 'vue';
14+
import { fetchAPIChangesForRoute } from 'docc-render/utils/data';
15+
import { flushPromises } from '../../../test-utils';
1316

17+
jest.mock('docc-render/utils/data');
18+
let response = { changes: 'foo' };
19+
20+
fetchAPIChangesForRoute.mockResolvedValue(response);
1421
const availableOptions = new Set(['latest_major', 'latest_minor']);
1522

16-
const createWrapperWithQuery = changeQuery => (
17-
shallowMount({
23+
// mimic reactive $route
24+
const $route = Vue.observable({
25+
path: 'foo/bar',
26+
query: {
27+
changes: null,
28+
},
29+
});
30+
31+
const $router = {
32+
push: jest.fn(),
33+
};
34+
35+
const store = {
36+
setAPIChanges: jest.fn(),
37+
};
38+
39+
const createWrapperWithQuery = (changeQuery) => {
40+
$route.query.changes = changeQuery;
41+
return shallowMount({
1842
name: 'TestComponentForapiChangesObserving',
1943
mixins: [apiChangesObserving],
2044
render() {
@@ -25,41 +49,91 @@ const createWrapperWithQuery = changeQuery => (
2549
},
2650
}, {
2751
provide: {
28-
store: {
29-
setAPIChanges: jest.fn(),
30-
},
52+
store,
3153
},
3254
mocks: {
33-
$route: {
34-
query: {
35-
changes: changeQuery,
36-
},
37-
},
38-
$router: {
39-
push: jest.fn(),
40-
},
55+
$route,
56+
$router,
4157
},
42-
})
43-
);
58+
});
59+
};
4460

4561
describe('apiChangesObserving', () => {
4662
let wrapper;
4763

64+
beforeEach(() => {
65+
jest.clearAllMocks();
66+
});
67+
afterEach(() => {
68+
wrapper.destroy();
69+
});
4870
it('turns `shouldDisplayChangesNav` true only when `selectedAPIChangesVersion` has available options', () => {
4971
const validChangeQuery = 'latest_major';
5072
const notValidChangeQuery = 'blah';
5173

5274
wrapper = createWrapperWithQuery(notValidChangeQuery);
5375
expect(wrapper.vm.shouldDisplayChangesNav).toBe(false);
76+
wrapper.destroy();
5477

5578
wrapper = createWrapperWithQuery(validChangeQuery);
5679
expect(wrapper.vm.shouldDisplayChangesNav).toBe(true);
80+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(1);
5781

5882
wrapper.vm.selectedAPIChangesVersion = notValidChangeQuery;
5983
expect(wrapper.vm.shouldDisplayChangesNav).toBe(false);
6084

6185
// make sure that if null is passed, the result is Boolean
6286
wrapper.vm.selectedAPIChangesVersion = null;
6387
expect(wrapper.vm.shouldDisplayChangesNav).toBe(false);
88+
89+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(1);
90+
});
91+
92+
it('turns `shouldDisplayChangesNav` true, on `$route.path` change, if there is a selected version', async () => {
93+
const validChangeQuery = 'latest_major';
94+
95+
wrapper = createWrapperWithQuery(validChangeQuery);
96+
expect(wrapper.vm.shouldDisplayChangesNav).toBe(true);
97+
expect($router.push).toHaveBeenCalledTimes(0);
98+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(1);
99+
expect(fetchAPIChangesForRoute).toHaveBeenLastCalledWith($route, validChangeQuery);
100+
$route.path = 'path/foo';
101+
await flushPromises();
102+
expect(wrapper.vm.shouldDisplayChangesNav).toBe(true);
103+
// push is not called, because we have changes already
104+
expect($router.push).toHaveBeenCalledTimes(0);
105+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(2);
106+
expect(fetchAPIChangesForRoute).toHaveBeenLastCalledWith($route, validChangeQuery);
107+
expect(store.setAPIChanges).toHaveBeenCalledTimes(2);
108+
expect(store.setAPIChanges).toHaveBeenLastCalledWith(response);
109+
});
110+
111+
it('does not fetch, on `path` change, if there is no version', async () => {
112+
wrapper = createWrapperWithQuery(null);
113+
expect(wrapper.vm.shouldDisplayChangesNav).toBe(false);
114+
expect($router.push).toHaveBeenCalledTimes(0);
115+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(0);
116+
$route.path = 'path/bar';
117+
await flushPromises();
118+
expect(wrapper.vm.shouldDisplayChangesNav).toBe(false);
119+
// push is not called, because we have changes already
120+
expect($router.push).toHaveBeenCalledTimes(0);
121+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(0);
122+
expect(store.setAPIChanges).toHaveBeenCalledTimes(0);
123+
});
124+
125+
it('updates the route, if only the query changed', async () => {
126+
wrapper = createWrapperWithQuery('latest_major');
127+
expect(wrapper.vm.shouldDisplayChangesNav).toBe(true);
128+
expect($router.push).toHaveBeenCalledTimes(0);
129+
await flushPromises();
130+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(1);
131+
expect($router.push).toHaveBeenCalledTimes(0);
132+
expect(store.setAPIChanges).toHaveBeenCalledTimes(1);
133+
$route.query.changes = 'latest_minor';
134+
await flushPromises();
135+
expect(fetchAPIChangesForRoute).toHaveBeenCalledTimes(2);
136+
expect($router.push).toHaveBeenCalledTimes(1);
137+
expect(store.setAPIChanges).toHaveBeenCalledTimes(2);
64138
});
65139
});

0 commit comments

Comments
 (0)