Skip to content

Commit bec31c5

Browse files
authored
Feature/route container (#275)
* move graph store into containers. Small refactor of store. Fix how tests initialize store. * small refactor to graph getters * accedently removed persist * route container * adding container and hook up route sync * adding route sync * copy store refactor from another PR * updating store with route * copying changes from another PR * added test for gettert * adding unit tests for action creator * fix lint
1 parent 17e7d1d commit bec31c5

File tree

12 files changed

+343
-4
lines changed

12 files changed

+343
-4
lines changed

client/containers/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export {
2424
getters as graphGetters,
2525
mutations as graphMutations,
2626
} from './graph';
27+
export {
28+
actionCreator as routeActionCreator,
29+
actionTypes as routeActionTypes,
30+
getters as routeGetters,
31+
getterTypes as routeGetterTypes,
32+
} from './route';
2733
export {
2834
container as SettingsWorkflowHistory,
2935
getDefaultState as getSettingsWorkflowHistoryDefaultState,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import { ROUTE_PUSH, ROUTE_REPLACE, ROUTE_UPDATE_QUERY } from './action-types';
23+
import { ROUTE_QUERY } from './getter-types';
24+
25+
const actionCreator = router => ({
26+
[ROUTE_PUSH]: (_, args) => router.push(args),
27+
[ROUTE_REPLACE]: (_, args) => router.replace(args),
28+
[ROUTE_UPDATE_QUERY]: ({ getters }, args) => {
29+
const query = getters[ROUTE_QUERY];
30+
31+
router.replace({
32+
query: {
33+
...query,
34+
...args,
35+
},
36+
});
37+
},
38+
});
39+
40+
export default actionCreator;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import actionCreator from './action-creator';
23+
import { ROUTE_PUSH, ROUTE_REPLACE, ROUTE_UPDATE_QUERY } from './action-types';
24+
import { ROUTE_QUERY } from './getter-types';
25+
26+
const getRouterMock = () => ({
27+
push: jest.fn(),
28+
replace: jest.fn(),
29+
});
30+
31+
describe('route action creator', () => {
32+
describe('when calling actions[ROUTE_PUSH] with "/test"', () => {
33+
it('should call router.push with "/test".', () => {
34+
const router = getRouterMock();
35+
const actions = actionCreator(router);
36+
37+
actions[ROUTE_PUSH](null, '/test');
38+
expect(router.push).toHaveBeenCalledWith('/test');
39+
});
40+
});
41+
42+
describe('when calling actions[ROUTE_REPLACE] with "/test"', () => {
43+
it('should call router.replace with "/test".', () => {
44+
const router = getRouterMock();
45+
const actions = actionCreator(router);
46+
47+
actions[ROUTE_REPLACE](null, '/test');
48+
expect(router.replace).toHaveBeenCalledWith('/test');
49+
});
50+
});
51+
52+
describe('when calling actions[ROUTE_UPDATE_QUERY] with a getter and test object', () => {
53+
it('should call router.replace with the original getter query with the test object override.', () => {
54+
const router = getRouterMock();
55+
const getters = {
56+
[ROUTE_QUERY]: {
57+
otherQuery: 'otherValue',
58+
test: 'valueA',
59+
},
60+
};
61+
const actions = actionCreator(router);
62+
63+
actions[ROUTE_UPDATE_QUERY]({ getters }, { test: 'valueB' });
64+
expect(router.replace).toHaveBeenCalledWith({
65+
query: {
66+
otherQuery: 'otherValue',
67+
test: 'valueB',
68+
},
69+
});
70+
});
71+
});
72+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
export const ROUTE_PUSH = 'ROUTE_PUSH';
23+
export const ROUTE_REPLACE = 'ROUTE_REPLACE';
24+
export const ROUTE_UPDATE_QUERY = 'ROUTE_UPDATE_QUERY';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
export const ROUTE_PARAMS = 'ROUTE_PARAMS';
23+
export const ROUTE_QUERY = 'ROUTE_QUERY';

client/containers/route/getters.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import { get } from 'lodash-es';
23+
import { ROUTE_PARAMS, ROUTE_QUERY } from './getter-types';
24+
25+
const getters = {
26+
[ROUTE_PARAMS]: state => get(state, 'route.params', {}),
27+
[ROUTE_QUERY]: state => get(state, 'route.query', {}),
28+
};
29+
30+
export default getters;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import { ROUTE_PARAMS, ROUTE_QUERY } from './getter-types';
23+
import getters from './getters';
24+
25+
describe('route getters', () => {
26+
describe('calling getters[ROUTE_PARAMS](state)', () => {
27+
describe('and state.route.params is defined', () => {
28+
const state = {
29+
route: {
30+
params: {
31+
paramA: 'valueA',
32+
paramB: 'valueB',
33+
},
34+
},
35+
};
36+
37+
it('should return the value from state.route.params', () => {
38+
const output = getters[ROUTE_PARAMS](state);
39+
40+
expect(output.paramA).toEqual('valueA');
41+
expect(output.paramB).toEqual('valueB');
42+
});
43+
});
44+
45+
describe('and state.route.params is not defined', () => {
46+
const state = {
47+
route: {},
48+
};
49+
50+
it('should return an empty object.', () => {
51+
const output = getters[ROUTE_PARAMS](state);
52+
53+
expect(output).toEqual({});
54+
});
55+
});
56+
});
57+
58+
describe('calling getters[ROUTE_QUERY](state)', () => {
59+
describe('and state.route.query is defined', () => {
60+
const state = {
61+
route: {
62+
query: {
63+
queryA: 'valueA',
64+
queryB: 'valueB',
65+
},
66+
},
67+
};
68+
69+
it('should return the value from state.route.query', () => {
70+
const output = getters[ROUTE_QUERY](state);
71+
72+
expect(output.queryA).toEqual('valueA');
73+
expect(output.queryB).toEqual('valueB');
74+
});
75+
});
76+
77+
describe('and state.route.query is not defined', () => {
78+
const state = {
79+
route: {},
80+
};
81+
82+
it('should return an empty object.', () => {
83+
const output = getters[ROUTE_QUERY](state);
84+
85+
expect(output).toEqual({});
86+
});
87+
});
88+
});
89+
});

client/containers/route/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2021 Uber Technologies Inc.
2+
//
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
import actionCreator from './action-creator';
23+
import { ROUTE_PUSH, ROUTE_REPLACE, ROUTE_UPDATE_QUERY } from './action-types';
24+
import { ROUTE_PARAMS, ROUTE_QUERY } from './getter-types';
25+
import getters from './getters';
26+
27+
const actionTypes = {
28+
ROUTE_PUSH,
29+
ROUTE_REPLACE,
30+
ROUTE_UPDATE_QUERY,
31+
};
32+
33+
const getterTypes = {
34+
ROUTE_PARAMS,
35+
ROUTE_QUERY,
36+
};
37+
38+
export { actionCreator, actionTypes, getterTypes, getters };

client/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import infiniteScroll from 'vue-infinite-scroll';
2525
import vueSelect from 'vue-select';
2626
import vueModal from 'vue-js-modal';
2727
import vueSplit from 'vue-split-panel';
28+
import { sync } from 'vuex-router-sync';
2829
import qs from 'friendly-querystring';
2930
import moment from 'moment';
3031
import promiseFinally from 'promise.prototype.finally';
@@ -344,7 +345,9 @@ if (typeof mocha === 'undefined') {
344345
document.body.appendChild(document.createElement('main'));
345346
}
346347

347-
const store = initStore();
348+
const store = initStore({ router });
349+
350+
sync(store, router);
348351

349352
// eslint-disable-next-line no-new
350353
new Vue({

0 commit comments

Comments
 (0)