Skip to content

Commit c3c9f16

Browse files
committed
complete reducers test, app, ungitConfig, versions
1 parent 675f63d commit c3c9f16

File tree

3 files changed

+301
-0
lines changed

3 files changed

+301
-0
lines changed

src-react/__tests__/reducers/app.js

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import sinon from 'sinon';
2+
import * as types from 'constants/action-types';
3+
4+
import app from 'reducers/app';
5+
6+
describe('app.js reducers', () => {
7+
let initialState;
8+
9+
beforeEach(() => {
10+
initialState = {
11+
gitVersionErrorVisible: false,
12+
showNewVersionAvailable: false,
13+
showBugtrackingNagscreen: false,
14+
showNPSSurvey: false
15+
};
16+
});
17+
18+
describe('when action doesn\'t match any case' , () => {
19+
20+
it('should return original state except the showNPSSurvey state', () => {
21+
const state = app(initialState, { type: 'no-op' });
22+
expect(state.gitVersionErrorVisible).toEqual(initialState.gitVersionErrorVisible);
23+
expect(state.showNewVersionAvailable).toEqual(initialState.showNewVersionAvailable);
24+
expect(state.showBugtrackingNagscreen).toEqual(initialState.showBugtrackingNagscreen);
25+
});
26+
27+
describe('but last NPS dismissed >= 6 month and Math.random < 0.01', () => {
28+
beforeEach(() => {
29+
localStorage.setItem('NPSSurveyLastDismissed', 1466227885);
30+
sinon.stub(Math, 'random').returns(0.006);
31+
});
32+
33+
it('showNPSSurvey state will be true', () => {
34+
const state = app(initialState, { type: 'no-op' });
35+
expect(state.showNPSSurvey).toEqual(true);
36+
});
37+
38+
afterEach(() => {
39+
Math.random.restore();
40+
});
41+
});
42+
43+
describe('but last NPS dismissed < 6 month', () => {
44+
beforeEach(() => {
45+
localStorage.setItem('NPSSurveyLastDismissed', Date.now());
46+
sinon.stub(Math, 'random').returns(0.1);
47+
});
48+
49+
it('showNPSSurvey state will be false', () => {
50+
const state = app(initialState, { type: 'no-op' });
51+
expect(state.showNPSSurvey).toEqual(false);
52+
});
53+
54+
afterEach(() => {
55+
Math.random.restore();
56+
});
57+
});
58+
59+
describe('but Math.random >= 0.01', () => {
60+
beforeEach(() => {
61+
localStorage.setItem('NPSSurveyLastDismissed', 1466227885);
62+
sinon.stub(Math, 'random').returns(0.1);
63+
});
64+
65+
it('showNPSSurvey state will be false', () => {
66+
const state = app(initialState, { type: 'no-op' });
67+
expect(state.showNPSSurvey).toEqual(false);
68+
});
69+
70+
afterEach(() => {
71+
Math.random.restore();
72+
});
73+
});
74+
});
75+
76+
describe('when RECEIVE_UNGIT_CONFIG action dispatch', () => {
77+
describe('if ungitConfig.config.bugtracking is false and bugtrackingNagscreenDismissed is false', () => {
78+
it('showBugtrackingNagscreen state will be true', () => {
79+
const ungitConfig = { config: { bugtracking: false } };
80+
const state = app(initialState, { type: types.RECEIVE_UNGIT_CONFIG, payload: ungitConfig }, { ungitConfig });
81+
expect(state.showBugtrackingNagscreen).toEqual(true);
82+
});
83+
});
84+
85+
describe('if ungitConfig.config.bugtracking is true', () => {
86+
it('showBugtrackingNagscreen state will be false', () => {
87+
const ungitConfig = { config: { bugtracking: true } };
88+
const state = app(initialState, { type: types.RECEIVE_UNGIT_CONFIG, payload: ungitConfig }, { ungitConfig });
89+
expect(state.showBugtrackingNagscreen).toEqual(false);
90+
});
91+
});
92+
93+
describe('if bugtrackingNagscreenDismissed is true', () => {
94+
it('showBugtrackingNagscreen state will be false', () => {
95+
localStorage.setItem('bugtrackingNagscreenDismissed', true);
96+
const ungitConfig = { config: { bugtracking: false } };
97+
const state = app(initialState, { type: types.RECEIVE_UNGIT_CONFIG, payload: ungitConfig }, { ungitConfig });
98+
expect(state.showBugtrackingNagscreen).toEqual(false);
99+
});
100+
});
101+
});
102+
103+
describe('when RECEIVE_GIT_VERSION action dispatch', () => {
104+
describe('if ungitConfig.config.gitVersionCheckOverride is false and gitVersionError is defined && gitVersionErrorDismissed is false', () => {
105+
it('gitVersionErrorVisible state will be true', () => {
106+
localStorage.setItem('gitVersionErrorDismissed', false);
107+
const ungitConfig = { config: { gitVersionCheckOverride: false } };
108+
const gitVersion = {
109+
satisfied: false,
110+
error: 'Failed to parse git version number.'
111+
};
112+
const config = { ungitConfig, versions: { gitVersion } };
113+
114+
const state = app(initialState, { type: types.RECEIVE_GIT_VERSION, payload: gitVersion }, config);
115+
expect(state.gitVersionErrorVisible).toEqual(true);
116+
});
117+
});
118+
119+
describe('if ungitConfig.config.gitVersionCheckOverride is true', () => {
120+
it('gitVersionErrorVisible state will be false', () => {
121+
localStorage.setItem('gitVersionErrorDismissed', false);
122+
const ungitConfig = { config: { gitVersionCheckOverride: true } };
123+
const gitVersion = {
124+
satisfied: false,
125+
error: 'Failed to parse git version number.'
126+
};
127+
const config = { ungitConfig, versions: { gitVersion } };
128+
129+
const state = app(initialState, { type: types.RECEIVE_GIT_VERSION, payload: gitVersion }, config);
130+
expect(state.gitVersionErrorVisible).toEqual(false);
131+
});
132+
});
133+
134+
describe('if gitVersionError is not defined', () => {
135+
it('gitVersionErrorVisible state will be false', () => {
136+
localStorage.setItem('gitVersionErrorDismissed', false);
137+
const ungitConfig = { config: { gitVersionCheckOverride: false } };
138+
const gitVersion = { satisfied: true };
139+
const config = { ungitConfig, versions: { gitVersion } };
140+
141+
const state = app(initialState, { type: types.RECEIVE_GIT_VERSION, payload: gitVersion }, config);
142+
expect(state.gitVersionErrorVisible).toEqual(false);
143+
});
144+
});
145+
146+
describe('if gitVersionErrorDismissed is true', () => {
147+
it('gitVersionErrorVisible state will be false', () => {
148+
localStorage.setItem('gitVersionErrorDismissed', true);
149+
const ungitConfig = { config: { gitVersionCheckOverride: false } };
150+
const gitVersion = {
151+
satisfied: false,
152+
error: 'Failed to parse git version number.'
153+
};
154+
const config = { ungitConfig, versions: { gitVersion } };
155+
156+
const state = app(initialState, { type: types.RECEIVE_GIT_VERSION, payload: gitVersion }, config);
157+
expect(state.gitVersionErrorVisible).toEqual(false);
158+
});
159+
});
160+
});
161+
162+
describe('when RECEIVE_LATEST_VERSION action dispatch', () => {
163+
describe('gitVersionCheckOverride is false and latestVersion.outdated is true', () => {
164+
it('showNewVersionAvailable state will be true', () => {
165+
const ungitConfig = { config: { gitVersionCheckOverride: false } };
166+
const latestVersion = { outdated: true };
167+
const config = { ungitConfig, versions: { latestVersion } };
168+
169+
const state = app(initialState, { type: types.RECEIVE_LATEST_VERSION, payload: latestVersion }, config);
170+
expect(state.showNewVersionAvailable).toEqual(true);
171+
});
172+
});
173+
174+
describe('gitVersionCheckOverride is true', () => {
175+
it('showNewVersionAvailable state will be false', () => {
176+
const ungitConfig = { config: { gitVersionCheckOverride: true } };
177+
const latestVersion = { outdated: true };
178+
const config = { ungitConfig, versions: { latestVersion } };
179+
180+
const state = app(initialState, { type: types.RECEIVE_LATEST_VERSION, payload: latestVersion }, config);
181+
expect(state.showNewVersionAvailable).toEqual(false);
182+
});
183+
});
184+
185+
describe('latestVersion.outdated is true', () => {
186+
it('showNewVersionAvailable state will be false', () => {
187+
const ungitConfig = { config: { gitVersionCheckOverride: false } };
188+
const latestVersion = { outdated: false };
189+
const config = { ungitConfig, versions: { latestVersion } };
190+
191+
const state = app(initialState, { type: types.RECEIVE_LATEST_VERSION, payload: latestVersion }, config);
192+
expect(state.showNewVersionAvailable).toEqual(false);
193+
});
194+
});
195+
});
196+
197+
describe('when RECEIVE_USER_CONFIG action dispatch', () => {
198+
describe('userConfig.bugtracking = false and bugtrackingNagscreenDismissed = false', () => {
199+
it('showBugtrackingNagscreen state will be true', () => {
200+
const userConfig = { bugtracking: false };
201+
const ungitConfig = { config: { bugtracking: true } };
202+
localStorage.setItem('bugtrackingNagscreenDismissed', false);
203+
204+
const state = app(initialState, { type: types.RECEIVE_USER_CONFIG, payload: userConfig }, { userConfig, ungitConfig });
205+
expect(state.showBugtrackingNagscreen).toEqual(true);
206+
});
207+
});
208+
209+
describe('userConfig.bugtracking = true', () => {
210+
it('showBugtrackingNagscreen state will be false', () => {
211+
const userConfig = { bugtracking: true };
212+
const ungitConfig = { config: { bugtracking: true } };
213+
localStorage.setItem('bugtrackingNagscreenDismissed', false);
214+
215+
const state = app(initialState, { type: types.RECEIVE_USER_CONFIG, payload: userConfig }, { userConfig, ungitConfig });
216+
expect(state.showBugtrackingNagscreen).toEqual(false);
217+
});
218+
});
219+
220+
describe('bugtrackingNagscreenDismissed = true', () => {
221+
it('showBugtrackingNagscreen state will be false', () => {
222+
const userConfig = { bugtracking: false };
223+
const ungitConfig = { config: { bugtracking: true } };
224+
localStorage.setItem('bugtrackingNagscreenDismissed', true);
225+
226+
const state = app(initialState, { type: types.RECEIVE_USER_CONFIG, payload: userConfig }, { userConfig, ungitConfig });
227+
expect(state.showBugtrackingNagscreen).toEqual(false);
228+
});
229+
});
230+
});
231+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as types from 'constants/action-types';
2+
3+
import userConfig from 'reducers/user-config';
4+
5+
describe('user-config.js reducers', () => {
6+
let initialState;
7+
8+
it('should return original state if action doesn\'t match any case' , () => {
9+
const state = userConfig(initialState, { type: 'no-op' });
10+
expect(state).toEqual({});
11+
});
12+
13+
describe('when RECEIVE_USER_CONFIG action dispatch', () => {
14+
it('state should have user configuration correctly', () => {
15+
// TODO: consider to use sinon to mock data and make mock data reuseable and meaningful
16+
const action = { type: types.RECEIVE_USER_CONFIG, payload: {
17+
port: 8080,
18+
bugtracking: true
19+
} };
20+
21+
const state = userConfig(initialState, action);
22+
expect(JSON.stringify(state)).toEqual(JSON.stringify(action.payload));
23+
});
24+
});
25+
26+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as types from 'constants/action-types';
2+
3+
import versions from 'reducers/versions';
4+
5+
describe('versions.js reducers', () => {
6+
const initialState = {
7+
gitVersion: null,
8+
latestVersion: null
9+
};
10+
11+
it('should return original state if action doesn\'t match any case' , () => {
12+
const state = versions(initialState, { type: 'no-op' });
13+
expect(state).toEqual(initialState);
14+
});
15+
16+
describe('when RECEIVE_GIT_VERSION action dispatch', () => {
17+
it('state should have git version configuration correctly', () => {
18+
// TODO: consider to use sinon to mock data and make mock data reuseable and meaningful
19+
const action = { type: types.RECEIVE_GIT_VERSION, payload: {
20+
requiredVersion: '>=1.8.x',
21+
satisfied: true,
22+
version: '2.3.2'
23+
} };
24+
25+
const state = versions(initialState, action);
26+
expect(JSON.stringify(state.gitVersion)).toEqual(JSON.stringify(action.payload));
27+
});
28+
});
29+
30+
describe('when RECEIVE_LATEST_VERSION action dispatch', () => {
31+
it('state should have lastest configuration correctly', () => {
32+
// TODO: consider to use sinon to mock data and make mock data reuseable and meaningful
33+
const action = { type: types.RECEIVE_LATEST_VERSION, payload: {
34+
currentVersion: 'dev-1.1.16-071f30a',
35+
latestVersion: '1.1.19',
36+
outdated: false
37+
} };
38+
39+
const state = versions(initialState, action);
40+
expect(JSON.stringify(state.latestVersion)).toEqual(JSON.stringify(action.payload));
41+
});
42+
});
43+
44+
});

0 commit comments

Comments
 (0)