|
| 1 | +/* eslint-disable max-len */ |
| 2 | +import mainReducer from '../reducers/mainReducer'; |
| 3 | +import { |
| 4 | + toggleMode, addNewSnapshots, initialConnect, setPort, emptySnapshots, changeView, changeSlider, moveBackward, moveForward, playForward, pause, startPlaying, importSnapshots, setTab, deleteTab, |
| 5 | +} from '../actions/actions'; |
| 6 | + |
| 7 | + |
| 8 | +describe('mainReducer testing', () => { |
| 9 | + let state; |
| 10 | + let currentTab; |
| 11 | + beforeEach(() => { |
| 12 | + state = { |
| 13 | + tabs: { |
| 14 | + 87: { |
| 15 | + snapshots: [1, 2, 3, 4], |
| 16 | + sliderIndex: 2, |
| 17 | + viewIndex: -1, |
| 18 | + mode: { |
| 19 | + paused: false, |
| 20 | + locked: false, |
| 21 | + persist: false, |
| 22 | + }, |
| 23 | + intervalId: 87, |
| 24 | + playing: true, |
| 25 | + }, |
| 26 | + 75: { |
| 27 | + snapshots: [1, 2, 3, 4], |
| 28 | + sliderIndex: 3, |
| 29 | + viewIndex: -1, |
| 30 | + mode: { |
| 31 | + paused: false, |
| 32 | + locked: false, |
| 33 | + persist: false, |
| 34 | + }, |
| 35 | + intervalId: 75, |
| 36 | + playing: false, |
| 37 | + }, |
| 38 | + }, |
| 39 | + currentTab: 87, |
| 40 | + port: { |
| 41 | + postMessage: () => {}, |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + // eslint-disable-next-line prefer-destructuring |
| 46 | + currentTab = state.currentTab; |
| 47 | + }); |
| 48 | + |
| 49 | + describe('moveBackward', () => { |
| 50 | + it('should decrement sliderIndex by 1', () => { |
| 51 | + expect(mainReducer(state, moveBackward()).tabs[currentTab].sliderIndex).toEqual(1); |
| 52 | + expect(mainReducer(state, moveBackward()).tabs[currentTab].playing).toEqual(false); |
| 53 | + }); |
| 54 | + it('should not decrement if sliderIndex is zero', () => { |
| 55 | + state.tabs[currentTab].sliderIndex = 0; |
| 56 | + const { sliderIndex } = mainReducer(state, moveBackward()).tabs[currentTab]; |
| 57 | + expect(sliderIndex).toBe(0); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('moveForward', () => { |
| 62 | + it('should increment sliderIndex by 1', () => { |
| 63 | + expect(mainReducer(state, moveForward()).tabs[currentTab].sliderIndex).toEqual(3); |
| 64 | + expect(mainReducer(state, moveForward()).tabs[currentTab].playing).toEqual(false); |
| 65 | + }); |
| 66 | + it('should not increment if sliderIndex at end', () => { |
| 67 | + state.tabs[currentTab].sliderIndex = 3; |
| 68 | + const { sliderIndex } = mainReducer(state, moveForward()).tabs[currentTab]; |
| 69 | + expect(sliderIndex).toBe(3); |
| 70 | + }); |
| 71 | + it('should not change playing if not coming from user', () => { |
| 72 | + const { playing } = mainReducer(state, playForward()).tabs[currentTab]; |
| 73 | + expect(playing).toBe(true); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('changeView', () => { |
| 78 | + it('unselect view if same index was selected', () => { |
| 79 | + state.tabs[currentTab].viewIndex = 1; |
| 80 | + expect(mainReducer(state, changeView(1)).tabs[currentTab].viewIndex).toEqual(-1); |
| 81 | + }); |
| 82 | + it('change viewIndex when unselected', () => { |
| 83 | + expect(mainReducer(state, changeView(2)).tabs[currentTab].viewIndex).toEqual(2); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('changeSlider', () => { |
| 88 | + it('should change sliderIndex', () => { |
| 89 | + expect(mainReducer(state, changeSlider(2)).tabs[currentTab].sliderIndex).toEqual(2); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('empty', () => { |
| 94 | + it('should empty snapshots except the first one', () => { |
| 95 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab].sliderIndex).toEqual(0); |
| 96 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab].viewIndex).toEqual(-1); |
| 97 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab].playing).toEqual(false); |
| 98 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab] |
| 99 | + .snapshots).toEqual(state.tabs[currentTab].snapshots.slice(0, 1)); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('setPort', () => { |
| 104 | + it('should set port when connection', () => { |
| 105 | + expect(mainReducer(state, setPort('newPort')).port).toEqual('newPort'); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('Import', () => { |
| 110 | + it('impoting file should replace snapshots of devtool', () => { |
| 111 | + expect(mainReducer(state, importSnapshots([100, 101])).tabs[currentTab].snapshots).toEqual([100, 101]); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('Toggle Mode', () => { |
| 116 | + it('clicking pause button should only change pause mode', () => { |
| 117 | + const { mode } = mainReducer(state, toggleMode('paused')).tabs[currentTab]; |
| 118 | + expect(mode.paused).toBe(true); |
| 119 | + expect(mode.locked).toBe(false); |
| 120 | + expect(mode.persist).toBe(false); |
| 121 | + }); |
| 122 | + it('clicking lock button should only change lock mode', () => { |
| 123 | + const { mode } = mainReducer(state, toggleMode('locked')).tabs[currentTab]; |
| 124 | + expect(mode.paused).toBe(false); |
| 125 | + expect(mode.locked).toBe(true); |
| 126 | + expect(mode.persist).toBe(false); |
| 127 | + }); |
| 128 | + it('clicking persist button should only change persist mode', () => { |
| 129 | + const { mode } = mainReducer(state, toggleMode('persist')).tabs[currentTab]; |
| 130 | + expect(mode.paused).toBe(false); |
| 131 | + expect(mode.locked).toBe(false); |
| 132 | + expect(mode.persist).toBe(true); |
| 133 | + }); |
| 134 | + it('undefined payload does nothing', () => { |
| 135 | + const { mode } = mainReducer(state, toggleMode('undefined')).tabs[currentTab]; |
| 136 | + expect(mode.paused).toBe(false); |
| 137 | + expect(mode.locked).toBe(false); |
| 138 | + expect(mode.persist).toBe(false); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe('slider pause', () => { |
| 143 | + it('should set playing to false and intervalId to null', () => { |
| 144 | + const playedTab = mainReducer(state, pause()).tabs[currentTab]; |
| 145 | + expect(playedTab.playing).toBe(false); |
| 146 | + expect(playedTab.intervalId).toBe(null); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('slider play', () => { |
| 151 | + it('should set playing to true and intervalId to payload', () => { |
| 152 | + const playedTab = mainReducer(state, startPlaying(333)).tabs[currentTab]; |
| 153 | + expect(playedTab.playing).toBe(true); |
| 154 | + expect(playedTab.intervalId).toBe(333); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('Initial Connect', () => { |
| 159 | + const newTab = { |
| 160 | + 104: { |
| 161 | + snapshots: [1, 2, 3, 8], |
| 162 | + sliderIndex: 3, |
| 163 | + viewIndex: -1, |
| 164 | + mode: { |
| 165 | + paused: false, |
| 166 | + locked: false, |
| 167 | + persist: false, |
| 168 | + }, |
| 169 | + intervalId: 912, |
| 170 | + playing: true, |
| 171 | + }, |
| 172 | + }; |
| 173 | + it('should add new tab', () => { |
| 174 | + const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104]; |
| 175 | + expect(addedTab).not.toBe(undefined); |
| 176 | + }); |
| 177 | + it('should force some values to default', () => { |
| 178 | + const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104]; |
| 179 | + expect(addedTab.sliderIndex).toBe(0); |
| 180 | + expect(addedTab.viewIndex).toBe(-1); |
| 181 | + expect(addedTab.intervalId).toBe(null); |
| 182 | + expect(addedTab.playing).toBe(false); |
| 183 | + }); |
| 184 | + it('snapshots should match the payload', () => { |
| 185 | + const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104]; |
| 186 | + expect(addedTab.snapshots).toEqual(newTab[104].snapshots); |
| 187 | + }); |
| 188 | + it('if currentTab undefined currentTab becomes firstTab', () => { |
| 189 | + state.currentTab = undefined; |
| 190 | + const addedTab = mainReducer(state, initialConnect(newTab)); |
| 191 | + expect(addedTab.currentTab).toBe(104); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe('new snapshots', () => { |
| 196 | + const newSnapshots = { |
| 197 | + 87: { |
| 198 | + snapshots: [1, 2, 3, 4, 5], |
| 199 | + sliderIndex: 2, |
| 200 | + viewIndex: -1, |
| 201 | + mode: { |
| 202 | + paused: false, |
| 203 | + locked: false, |
| 204 | + persist: false, |
| 205 | + }, |
| 206 | + intervalId: 87, |
| 207 | + playing: true, |
| 208 | + }, |
| 209 | + }; |
| 210 | + it('update snapshots of corresponding tabId', () => { |
| 211 | + const updated = mainReducer(state, addNewSnapshots(newSnapshots)); |
| 212 | + expect(updated.tabs[87].snapshots).toEqual(newSnapshots[87].snapshots); |
| 213 | + }); |
| 214 | + it('should delete tabs that are deleted from background script', () => { |
| 215 | + const updated = mainReducer(state, addNewSnapshots(newSnapshots)); |
| 216 | + expect(updated.tabs[75]).toBe(undefined); |
| 217 | + }); |
| 218 | + it('if currentTab undefined currentTab becomes first Tab', () => { |
| 219 | + state.currentTab = undefined; |
| 220 | + const updated = mainReducer(state, addNewSnapshots(newSnapshots)); |
| 221 | + expect(updated.currentTab).toBe(87); |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + describe('set_tab', () => { |
| 226 | + it('should set tab to payload', () => { |
| 227 | + const newCurrentTab = mainReducer(state, setTab(75)).currentTab; |
| 228 | + expect(newCurrentTab).toBe(75); |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + describe('delete_tab', () => { |
| 233 | + it('should delete only payload tab from state', () => { |
| 234 | + const afterDelete = mainReducer(state, deleteTab(75)); |
| 235 | + expect(afterDelete.tabs[75]).toBe(undefined); |
| 236 | + expect(afterDelete.tabs[87]).not.toBe(undefined); |
| 237 | + }); |
| 238 | + it('should change current tab if deleted tab matches current tab', () => { |
| 239 | + const afterDelete = mainReducer(state, deleteTab(87)); |
| 240 | + expect(afterDelete.tabs[87]).toBe(undefined); |
| 241 | + expect(afterDelete.tabs[75]).not.toBe(undefined); |
| 242 | + expect(afterDelete.currentTab).toBe(75); |
| 243 | + }); |
| 244 | + }); |
| 245 | + |
| 246 | + describe('default', () => { |
| 247 | + const action = { |
| 248 | + type: 'doesNotExist', |
| 249 | + payload: 'trigger', |
| 250 | + }; |
| 251 | + it('if there are no match of action types, throw error', () => { |
| 252 | + try { |
| 253 | + mainReducer(state, action); |
| 254 | + } catch (err) { |
| 255 | + expect(err).toBeInstanceOf(Error); |
| 256 | + } |
| 257 | + }); |
| 258 | + }); |
| 259 | +}); |
0 commit comments