Skip to content

Commit 05d9584

Browse files
committed
mainReducer testing 100%
1 parent fdcd3e9 commit 05d9584

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/app/__tests__/mainReducer.test.jsx

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,27 @@ describe('mainReducer testing', () => {
5252
expect(mainReducer(state, moveBackward()).tabs[currentTab].sliderIndex).toEqual(1);
5353
expect(mainReducer(state, moveBackward()).tabs[currentTab].playing).toEqual(false);
5454
});
55+
it('should not decrement if sliderIndex is zero', () => {
56+
state.tabs[currentTab].sliderIndex = 0;
57+
const { sliderIndex } = mainReducer(state, moveBackward()).tabs[currentTab];
58+
expect(sliderIndex).toBe(0);
59+
});
5560
});
5661

5762
describe('moveForward', () => {
5863
it('should increment sliderIndex by 1', () => {
5964
expect(mainReducer(state, moveForward()).tabs[currentTab].sliderIndex).toEqual(3);
6065
expect(mainReducer(state, moveForward()).tabs[currentTab].playing).toEqual(false);
6166
});
67+
it('should not increment if sliderIndex at end', () => {
68+
state.tabs[currentTab].sliderIndex = 3;
69+
const { sliderIndex } = mainReducer(state, moveForward()).tabs[currentTab];
70+
expect(sliderIndex).toBe(3);
71+
});
72+
it('should not change playing if not coming from user', () => {
73+
const { playing } = mainReducer(state, playForward()).tabs[currentTab];
74+
expect(playing).toBe(true);
75+
});
6276
});
6377

6478
describe('changeView', () => {
@@ -118,6 +132,12 @@ describe('mainReducer testing', () => {
118132
expect(mode.locked).toBe(false);
119133
expect(mode.persist).toBe(true);
120134
});
135+
it('undefined payload does nothing', () => {
136+
const { mode } = mainReducer(state, toggleMode('undefined')).tabs[currentTab];
137+
expect(mode.paused).toBe(false);
138+
expect(mode.locked).toBe(false);
139+
expect(mode.persist).toBe(false);
140+
});
121141
});
122142

123143
describe('slider pause', () => {
@@ -153,7 +173,7 @@ describe('mainReducer testing', () => {
153173
};
154174
it('should add new tab', () => {
155175
const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104];
156-
expect(addedTab).not.toBe(undefined);
176+
expect(addedTab).not.toBe(undefined);
157177
});
158178
it('should force some values to default', () => {
159179
const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104];
@@ -166,9 +186,75 @@ describe('mainReducer testing', () => {
166186
const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104];
167187
expect(addedTab.snapshots).toEqual(newTab[104].snapshots);
168188
});
189+
it('if currentTab undefined currentTab becomes firstTab', () => {
190+
state.currentTab = undefined;
191+
const addedTab = mainReducer(state, initialConnect(newTab));
192+
expect(addedTab.currentTab).toBe(104);
193+
});
169194
});
170195

171196
describe('new snapshots', () => {
197+
const newSnapshots = {
198+
87: {
199+
snapshots: [1, 2, 3, 4, 5],
200+
sliderIndex: 2,
201+
viewIndex: -1,
202+
mode: {
203+
paused: false,
204+
locked: false,
205+
persist: false,
206+
},
207+
intervalId: 87,
208+
playing: true,
209+
},
210+
};
211+
it('update snapshots of corresponding tabId', () => {
212+
const updated = mainReducer(state, addNewSnapshots(newSnapshots));
213+
expect(updated.tabs[87].snapshots).toEqual(newSnapshots[87].snapshots);
214+
});
215+
it('should delete tabs that are deleted from background script', () => {
216+
const updated = mainReducer(state, addNewSnapshots(newSnapshots));
217+
expect(updated.tabs[75]).toBe(undefined);
218+
});
219+
it('if currentTab undefined currentTab becomes first Tab', () => {
220+
state.currentTab = undefined;
221+
const updated = mainReducer(state, addNewSnapshots(newSnapshots));
222+
expect(updated.currentTab).toBe(87);
223+
});
224+
});
172225

226+
describe('set_tab', () => {
227+
it('should set tab to payload', () => {
228+
const newCurrentTab = mainReducer(state, setTab(75)).currentTab;
229+
expect(newCurrentTab).toBe(75);
230+
});
231+
});
232+
233+
describe('delete_tab', () => {
234+
it('should delete only payload tab from state', () => {
235+
const afterDelete = mainReducer(state, deleteTab(75));
236+
expect(afterDelete.tabs[75]).toBe(undefined);
237+
expect(afterDelete.tabs[87]).not.toBe(undefined);
238+
});
239+
it('should change current tab if deleted tab matches current tab', () => {
240+
const afterDelete = mainReducer(state, deleteTab(87));
241+
expect(afterDelete.tabs[87]).toBe(undefined);
242+
expect(afterDelete.tabs[75]).not.toBe(undefined);
243+
expect(afterDelete.currentTab).toBe(75);
244+
});
245+
});
246+
247+
describe('default', () => {
248+
const action = {
249+
type: 'doesNotExist',
250+
payload: 'trigger',
251+
};
252+
it('if there are no match of action types, throw error', () => {
253+
try {
254+
mainReducer(state, action);
255+
} catch (err) {
256+
expect(err).toBeInstanceOf(Error);
257+
}
258+
});
173259
});
174260
});

src/app/reducers/mainReducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default (state, action) => produce(state, draft => {
9292
actionText = 'setPersist';
9393
break;
9494
default:
95+
break;
9596
}
9697
port.postMessage({ action: actionText, payload: newMode, tabId: currentTab });
9798
break;

0 commit comments

Comments
 (0)