Skip to content

Commit b5adf41

Browse files
authored
Feature/test helpers (#278)
* added test helpers for testing getters * updating getter tests with getter helper * updating description text for tests * fix lint
1 parent 83a04a3 commit b5adf41

File tree

9 files changed

+50
-10
lines changed

9 files changed

+50
-10
lines changed

babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
"~containers": "./client/containers",
1010
"~helpers": "./client/helpers",
1111
"~services": "./client/services",
12+
"~test": "./test/helpers",
1213
},
1314
}
1415
],

client/containers/route/getters.spec.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
// THE SOFTWARE.
2121

2222
import { ROUTE_PARAMS, ROUTE_QUERY } from './getter-types';
23-
import getters from './getters';
23+
import getterFns from './getters';
24+
import { initGetters } from '~test';
2425

2526
describe('route getters', () => {
26-
describe('calling getters[ROUTE_PARAMS](state)', () => {
27+
describe('calling getters[ROUTE_PARAMS]', () => {
2728
describe('and state.route.params is defined', () => {
2829
const state = {
2930
route: {
@@ -35,7 +36,8 @@ describe('route getters', () => {
3536
};
3637

3738
it('should return the value from state.route.params', () => {
38-
const output = getters[ROUTE_PARAMS](state);
39+
const getters = initGetters({ getterFns, state });
40+
const output = getters[ROUTE_PARAMS];
3941

4042
expect(output.paramA).toEqual('valueA');
4143
expect(output.paramB).toEqual('valueB');
@@ -48,14 +50,15 @@ describe('route getters', () => {
4850
};
4951

5052
it('should return an empty object.', () => {
51-
const output = getters[ROUTE_PARAMS](state);
53+
const getters = initGetters({ getterFns, state });
54+
const output = getters[ROUTE_PARAMS];
5255

5356
expect(output).toEqual({});
5457
});
5558
});
5659
});
5760

58-
describe('calling getters[ROUTE_QUERY](state)', () => {
61+
describe('calling getters[ROUTE_QUERY]', () => {
5962
describe('and state.route.query is defined', () => {
6063
const state = {
6164
route: {
@@ -67,7 +70,8 @@ describe('route getters', () => {
6770
};
6871

6972
it('should return the value from state.route.query', () => {
70-
const output = getters[ROUTE_QUERY](state);
73+
const getters = initGetters({ getterFns, state });
74+
const output = getters[ROUTE_QUERY];
7175

7276
expect(output.queryA).toEqual('valueA');
7377
expect(output.queryB).toEqual('valueB');
@@ -80,7 +84,8 @@ describe('route getters', () => {
8084
};
8185

8286
it('should return an empty object.', () => {
83-
const output = getters[ROUTE_QUERY](state);
87+
const getters = initGetters({ getterFns, state });
88+
const output = getters[ROUTE_QUERY];
8489

8590
expect(output).toEqual({});
8691
});

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = {
139139
// snapshotSerializers: [],
140140

141141
// The test environment that will be used for testing
142-
testEnvironment: "node",
142+
testEnvironment: "jsdom",
143143

144144
// Options that will be passed to the testEnvironment
145145
// testEnvironmentOptions: {},

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"fetch-mock": "^7.7.0",
118118
"jest": "^24.9.0",
119119
"jest-fetch-mock": "^3.0.3",
120+
"jest-useragent-mock": "^0.1.1",
120121
"mocha": "^4.0.1",
121122
"mocha-chrome": "^1.1.0",
122123
"moment-timezone": "^0.5.32",

test/helpers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as initGetters } from './init-getters';
2+
export { default as initStore } from './init-store';

test/helpers/init-getters.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import initStore from './init-store';
2+
3+
const initGetters = ({ getterFns, state }) => initStore({ getters: getterFns, state }).getters;
4+
5+
export default initGetters;

test/helpers/init-store.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Vuex from 'vuex';
2+
3+
const initStore = ({ actions = {}, getters = {}, mutations = {}, state = {} } = {}) => new Vuex.Store({
4+
actions,
5+
getters,
6+
mutations,
7+
state,
8+
});
9+
10+
export default initStore;

test/setup.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
// polyfills for web browser to node
22
import 'babel-polyfill';
3-
import AbortController from 'node-abort-controller'
3+
import AbortController from 'node-abort-controller';
44
import atob from 'atob';
55
import { injectMomentDurationFormat, jsonTryParse } from '~helpers';
6-
import { enableFetchMocks } from 'jest-fetch-mock'
6+
import { enableFetchMocks } from 'jest-fetch-mock';
7+
import { mockUserAgent } from 'jest-useragent-mock';
8+
import Vue from 'vue';
9+
import Vuex from 'vuex';
710

811
global.atob = atob;
12+
913
global.JSON.tryParse = jsonTryParse;
14+
1015
injectMomentDurationFormat();
1116

1217
if (!window.AbortController) {
1318
window.AbortController = AbortController;
1419
}
1520

1621
enableFetchMocks();
22+
1723
window.fetch = global.fetch;
24+
25+
mockUserAgent('chrome');
26+
27+
Vue.use(Vuex);

0 commit comments

Comments
 (0)