|
| 1 | +import * as reducers from '../bottomReducers'; |
| 2 | +import * as interfaces from '../../interfaces/Interfaces'; |
| 3 | +import * as types from '../../actionTypes/index'; |
| 4 | +import { initialApplicationState, testComponent } from '../initialState'; |
| 5 | + |
| 6 | +describe('Testing bottom reducer:', () => { |
| 7 | + let state: interfaces.ApplicationStateInt; |
| 8 | + // redefine the default state before each reducer test |
| 9 | + beforeEach(() => { |
| 10 | + state = initialApplicationState; |
| 11 | + state.components.push(testComponent); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('addProp', () => { |
| 15 | + it('Properly adds prop to a focus component', () => { |
| 16 | + const payload = { key: 'test', type: 'string' }; |
| 17 | + const newState = reducers.addProp(state, payload); |
| 18 | + let expectedProp = { |
| 19 | + id: state.focusComponent.nextPropId, |
| 20 | + key: 'test', |
| 21 | + type: 'string' |
| 22 | + }; |
| 23 | + const newAppComp = newState.components.find( |
| 24 | + (comp: interfaces.ComponentInt) => comp.id === 1 |
| 25 | + ); |
| 26 | + |
| 27 | + expect(newState.focusComponent.props[0]).toEqual(expectedProp); |
| 28 | + expect(newAppComp.props[0]).toEqual(expectedProp); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('deleteProp', () => { |
| 33 | + it('Properly deletes a prop for a focus component', () => { |
| 34 | + const payload = { key: 'test', type: 'string' }; |
| 35 | + let newState = reducers.addProp(state, payload); |
| 36 | + |
| 37 | + const propId = state.focusComponent.nextPropId; |
| 38 | + newState = reducers.deleteProp(newState, propId); |
| 39 | + |
| 40 | + expect(newState.focusComponent.props.length).toEqual(0); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('toggleCodeEdit', () => { |
| 45 | + it('Properly switches the app into "Code Edit" mode', () => { |
| 46 | + const newState = reducers.toggleCodeEdit(state); |
| 47 | + expect(newState.codeReadOnly).toEqual(false); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('toggelNative', () => { |
| 52 | + let newState: any; |
| 53 | + it('Properly switches the app into "Native" mode', () => { |
| 54 | + window.confirm = jest.fn(() => true); |
| 55 | + |
| 56 | + newState = reducers.toggleNative(state); |
| 57 | + |
| 58 | + //check that it doesn't mutate original state |
| 59 | + expect(newState).not.toBe(state); |
| 60 | + //check if the native toggle switched |
| 61 | + expect(newState.native).toEqual(true); |
| 62 | + |
| 63 | + //expect window confirm message to run |
| 64 | + expect(window.confirm).toBeCalled(); |
| 65 | + |
| 66 | + //check proper width and height |
| 67 | + expect(newState.focusComponent.position.width).toEqual(500); |
| 68 | + expect(newState.focusComponent.position.height).toEqual(850); |
| 69 | + |
| 70 | + //check if it has been marked as changed |
| 71 | + expect(newState.focusComponent.changed).toEqual(true); |
| 72 | + |
| 73 | + //check if it has been updated in components array |
| 74 | + const appInComps = newState.components.find( |
| 75 | + (e: interfaces.ComponentInt) => e.id === 1 |
| 76 | + ); |
| 77 | + |
| 78 | + //check proper width and height |
| 79 | + expect(appInComps.position.width).toEqual(500); |
| 80 | + expect(appInComps.position.height).toEqual(850); |
| 81 | + |
| 82 | + //check if it has been marked as changed |
| 83 | + expect(appInComps.changed).toEqual(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it('Properly switches the app back into "React" mode', () => { |
| 87 | + window.confirm = jest.fn(() => true); |
| 88 | + |
| 89 | + newState = reducers.toggleNative(newState); |
| 90 | + |
| 91 | + //check if the native toggle switched |
| 92 | + expect(newState.native).toEqual(false); |
| 93 | + |
| 94 | + //expect window confirm message to run |
| 95 | + expect(window.confirm).toBeCalled(); |
| 96 | + |
| 97 | + //check proper width and height |
| 98 | + expect(newState.focusComponent.position.width).toEqual(1200); |
| 99 | + expect(newState.focusComponent.position.height).toEqual(800); |
| 100 | + |
| 101 | + //check if it has been marked as changed |
| 102 | + expect(newState.focusComponent.changed).toEqual(true); |
| 103 | + |
| 104 | + //check if it has been updated in components array |
| 105 | + const appInComps = newState.components.find( |
| 106 | + (e: interfaces.ComponentInt) => e.id === 1 |
| 107 | + ); |
| 108 | + |
| 109 | + //check proper width and height |
| 110 | + expect(appInComps.position.width).toEqual(1200); |
| 111 | + expect(appInComps.position.height).toEqual(800); |
| 112 | + |
| 113 | + //check if it has been marked as changed |
| 114 | + expect(appInComps.changed).toEqual(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it('Handles user choosing not to proceed when prompted', () => { |
| 118 | + window.confirm = jest.fn(() => false); |
| 119 | + |
| 120 | + newState = reducers.toggleNative(newState); |
| 121 | + |
| 122 | + //expect window confirm message to run |
| 123 | + expect(window.confirm).toBeCalled(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('updateCode', () => { |
| 128 | + it(' Properly updates the code of a given component', () => { |
| 129 | + const payload = { componentId: 1, code: 'testing code' }; |
| 130 | + const newState = reducers.updateCode(state, payload); |
| 131 | + //check if it has been marked as changed |
| 132 | + expect(newState.focusComponent.changed).toEqual(false); |
| 133 | + |
| 134 | + //check if it has been updated in components array |
| 135 | + const appInComps = newState.components.find( |
| 136 | + (e: interfaces.ComponentInt) => e.id === 1 |
| 137 | + ); |
| 138 | + |
| 139 | + //check proper width and height |
| 140 | + expect(appInComps.code).toEqual(payload.code); |
| 141 | + expect(appInComps.changed).toEqual(false); |
| 142 | + |
| 143 | + //doesn't mutate the state |
| 144 | + expect(newState).not.toBe(state); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments