|
| 1 | +/* eslint-disable max-len */ |
| 2 | +import * as types from '../constants/actionTypes'; |
| 3 | +import mainReducer from '../reducers/mainReducer'; |
| 4 | +import { |
| 5 | + toggleMode, addNewSnapshots, initialConnect, setPort, emptySnapshots, changeView, changeSlider, moveBackward, moveForward, playForward, pause, startPlaying, importSnapshots, setTab, deleteTab, |
| 6 | +} from '../actions/actions'; |
| 7 | + |
| 8 | + |
| 9 | +describe('mainReducer testing', () => { |
| 10 | + let state; |
| 11 | + let currentTab; |
| 12 | + beforeEach(() => { |
| 13 | + state = { |
| 14 | + tabs: { |
| 15 | + 87: { |
| 16 | + snapshots: [1, 2, 3, 4], |
| 17 | + sliderIndex: 2, |
| 18 | + viewIndex: -1, |
| 19 | + mode: { |
| 20 | + paused: false, |
| 21 | + locked: false, |
| 22 | + persist: false, |
| 23 | + }, |
| 24 | + intervalId: 87, |
| 25 | + playing: true, |
| 26 | + }, |
| 27 | + 75: { |
| 28 | + snapshots: [1, 2, 3, 4], |
| 29 | + sliderIndex: 3, |
| 30 | + viewIndex: -1, |
| 31 | + mode: { |
| 32 | + paused: false, |
| 33 | + locked: false, |
| 34 | + persist: false, |
| 35 | + }, |
| 36 | + intervalId: 75, |
| 37 | + playing: false, |
| 38 | + }, |
| 39 | + }, |
| 40 | + currentTab: 87, |
| 41 | + port: { |
| 42 | + postMessage: () => {}, |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + // eslint-disable-next-line prefer-destructuring |
| 47 | + currentTab = state.currentTab; |
| 48 | + }); |
| 49 | + |
| 50 | + describe('moveBackward', () => { |
| 51 | + it('should decrement sliderIndex by 1', () => { |
| 52 | + expect(mainReducer(state, moveBackward()).tabs[currentTab].sliderIndex).toEqual(1); |
| 53 | + expect(mainReducer(state, moveBackward()).tabs[currentTab].playing).toEqual(false); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('moveForward', () => { |
| 58 | + it('should increment sliderIndex by 1', () => { |
| 59 | + expect(mainReducer(state, moveForward()).tabs[currentTab].sliderIndex).toEqual(3); |
| 60 | + expect(mainReducer(state, moveForward()).tabs[currentTab].playing).toEqual(false); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('changeView', () => { |
| 65 | + it('unselect view if same index was selected', () => { |
| 66 | + state.tabs[currentTab].viewIndex = 1; |
| 67 | + expect(mainReducer(state, changeView(1)).tabs[currentTab].viewIndex).toEqual(-1); |
| 68 | + }); |
| 69 | + it('change viewIndex when unselected', () => { |
| 70 | + expect(mainReducer(state, changeView(2)).tabs[currentTab].viewIndex).toEqual(2); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('changeSlider', () => { |
| 75 | + it('should change sliderIndex', () => { |
| 76 | + expect(mainReducer(state, changeSlider(2)).tabs[currentTab].sliderIndex).toEqual(2); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('empty', () => { |
| 81 | + it('should empty snapshots except the first one', () => { |
| 82 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab].sliderIndex).toEqual(0); |
| 83 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab].viewIndex).toEqual(-1); |
| 84 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab].playing).toEqual(false); |
| 85 | + expect(mainReducer(state, emptySnapshots()).tabs[currentTab] |
| 86 | + .snapshots).toEqual(state.tabs[currentTab].snapshots.slice(0, 1)); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('setPort', () => { |
| 91 | + it('should set port when connection', () => { |
| 92 | + expect(mainReducer(state, setPort('newPort')).port).toEqual('newPort'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('Import', () => { |
| 97 | + it('impoting file should replace snapshots of devtool', () => { |
| 98 | + expect(mainReducer(state, importSnapshots([100, 101])).tabs[currentTab].snapshots).toEqual([100, 101]); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('Toggle Mode', () => { |
| 103 | + it('clicking pause button should only change pause mode', () => { |
| 104 | + const { mode } = mainReducer(state, toggleMode('paused')).tabs[currentTab]; |
| 105 | + expect(mode.paused).toBe(true); |
| 106 | + expect(mode.locked).toBe(false); |
| 107 | + expect(mode.persist).toBe(false); |
| 108 | + }); |
| 109 | + it('clicking lock button should only change lock mode', () => { |
| 110 | + const { mode } = mainReducer(state, toggleMode('locked')).tabs[currentTab]; |
| 111 | + expect(mode.paused).toBe(false); |
| 112 | + expect(mode.locked).toBe(true); |
| 113 | + expect(mode.persist).toBe(false); |
| 114 | + }); |
| 115 | + it('clicking persist button should only change persist mode', () => { |
| 116 | + const { mode } = mainReducer(state, toggleMode('persist')).tabs[currentTab]; |
| 117 | + expect(mode.paused).toBe(false); |
| 118 | + expect(mode.locked).toBe(false); |
| 119 | + expect(mode.persist).toBe(true); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('slider pause', () => { |
| 124 | + it('should set playing to false and intervalId to null', () => { |
| 125 | + const playedTab = mainReducer(state, pause()).tabs[currentTab]; |
| 126 | + expect(playedTab.playing).toBe(false); |
| 127 | + expect(playedTab.intervalId).toBe(null); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('slider play', () => { |
| 132 | + it('should set playing to true and intervalId to payload', () => { |
| 133 | + const playedTab = mainReducer(state, startPlaying(333)).tabs[currentTab]; |
| 134 | + expect(playedTab.playing).toBe(true); |
| 135 | + expect(playedTab.intervalId).toBe(333); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe('Initial Connect', () => { |
| 140 | + const newTab = { |
| 141 | + 104: { |
| 142 | + snapshots: [1, 2, 3, 8], |
| 143 | + sliderIndex: 3, |
| 144 | + viewIndex: -1, |
| 145 | + mode: { |
| 146 | + paused: false, |
| 147 | + locked: false, |
| 148 | + persist: false, |
| 149 | + }, |
| 150 | + intervalId: 912, |
| 151 | + playing: true, |
| 152 | + }, |
| 153 | + }; |
| 154 | + it('should add new tab', () => { |
| 155 | + const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104]; |
| 156 | + expect(addedTab).not.toBe(undefined); |
| 157 | + }); |
| 158 | + it('should force some values to default', () => { |
| 159 | + const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104]; |
| 160 | + expect(addedTab.sliderIndex).toBe(0); |
| 161 | + expect(addedTab.viewIndex).toBe(-1); |
| 162 | + expect(addedTab.intervalId).toBe(null); |
| 163 | + expect(addedTab.playing).toBe(false); |
| 164 | + }); |
| 165 | + it('snapshots should match the payload', () => { |
| 166 | + const addedTab = mainReducer(state, initialConnect(newTab)).tabs[104]; |
| 167 | + expect(addedTab.snapshots).toEqual(newTab[104].snapshots); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('new snapshots', () => { |
| 172 | + |
| 173 | + }); |
| 174 | +}); |
0 commit comments