|
| 1 | +import { |
| 2 | + EVENT_SUBSCRIPTION_ADD, |
| 3 | + EVENT_SUBSCRIPTION_REMOVE, |
| 4 | +} from '../../constants'; |
| 5 | +import subscriptionsReducers from '../subscriptionsReducers'; |
| 6 | + |
| 7 | +describe('subscriptionsReducers', () => { |
| 8 | + test('on unrecognized action, returns input state unchanged', () => { |
| 9 | + const prevState = { hello: 'world' }; |
| 10 | + const newState = subscriptionsReducers(prevState, {}); |
| 11 | + expect(newState).toEqual(prevState); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('EVENT_SUBSCRIPTION_ADD', () => { |
| 15 | + test('if new subscriptions do not exist in state, add them', () => { |
| 16 | + const prevState = []; |
| 17 | + const action = { |
| 18 | + type: EVENT_SUBSCRIPTION_ADD, |
| 19 | + subscriptions: [ |
| 20 | + { |
| 21 | + name: 'some stream', |
| 22 | + stream_id: 1, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'some other stream', |
| 26 | + stream_id: 2, |
| 27 | + } |
| 28 | + ], |
| 29 | + }; |
| 30 | + const expectedState = [ |
| 31 | + { |
| 32 | + name: 'some stream', |
| 33 | + stream_id: 1, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'some other stream', |
| 37 | + stream_id: 2, |
| 38 | + } |
| 39 | + ]; |
| 40 | + |
| 41 | + const newState = subscriptionsReducers(prevState, action); |
| 42 | + |
| 43 | + expect(newState).toEqual(expectedState); |
| 44 | + }); |
| 45 | + |
| 46 | + test('if some of subscriptions already exist, do not add them', () => { |
| 47 | + const prevState = [ |
| 48 | + { |
| 49 | + color: 'red', |
| 50 | + stream_id: 1, |
| 51 | + name: 'some stream' |
| 52 | + } |
| 53 | + ]; |
| 54 | + const action = { |
| 55 | + type: EVENT_SUBSCRIPTION_ADD, |
| 56 | + subscriptions: [ |
| 57 | + { |
| 58 | + name: 'some stream', |
| 59 | + stream_id: 1, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: 'some other stream', |
| 63 | + stream_id: 2, |
| 64 | + } |
| 65 | + ], |
| 66 | + }; |
| 67 | + const expectedState = [ |
| 68 | + { |
| 69 | + color: 'red', |
| 70 | + name: 'some stream', |
| 71 | + stream_id: 1, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: 'some other stream', |
| 75 | + stream_id: 2, |
| 76 | + } |
| 77 | + ]; |
| 78 | + |
| 79 | + const newState = subscriptionsReducers(prevState, action); |
| 80 | + |
| 81 | + expect(newState).toEqual(expectedState); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('EVENT_SUBSCRIPTION_REMOVE', () => { |
| 86 | + test('removes subscriptions from state', () => { |
| 87 | + const prevState = [ |
| 88 | + { |
| 89 | + color: 'red', |
| 90 | + stream_id: 1, |
| 91 | + name: 'some stream' |
| 92 | + }, |
| 93 | + { |
| 94 | + color: 'green', |
| 95 | + stream_id: 2, |
| 96 | + name: 'other stream' |
| 97 | + }, |
| 98 | + { |
| 99 | + color: 'blue', |
| 100 | + stream_id: 3, |
| 101 | + name: 'third stream' |
| 102 | + }, |
| 103 | + ]; |
| 104 | + const action = { |
| 105 | + type: EVENT_SUBSCRIPTION_REMOVE, |
| 106 | + subscriptions: [ |
| 107 | + { |
| 108 | + name: 'some stream', |
| 109 | + stream_id: 1, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: 'some other stream', |
| 113 | + stream_id: 2, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }; |
| 117 | + const expectedState = [ |
| 118 | + { |
| 119 | + color: 'blue', |
| 120 | + stream_id: 3, |
| 121 | + name: 'third stream' |
| 122 | + }, |
| 123 | + ]; |
| 124 | + |
| 125 | + const newState = subscriptionsReducers(prevState, action); |
| 126 | + |
| 127 | + expect(newState).toEqual(expectedState); |
| 128 | + }); |
| 129 | + |
| 130 | + test('removes subscriptions that exist, do nothing if not', () => { |
| 131 | + const prevState = [ |
| 132 | + { |
| 133 | + name: 'some stream', |
| 134 | + stream_id: 1, |
| 135 | + }, |
| 136 | + ]; |
| 137 | + const action = { |
| 138 | + type: EVENT_SUBSCRIPTION_REMOVE, |
| 139 | + subscriptions: [ |
| 140 | + { |
| 141 | + name: 'some stream', |
| 142 | + stream_id: 1, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: 'some other stream', |
| 146 | + stream_id: 2, |
| 147 | + } |
| 148 | + ], |
| 149 | + }; |
| 150 | + const expectedState = []; |
| 151 | + |
| 152 | + const newState = subscriptionsReducers(prevState, action); |
| 153 | + |
| 154 | + expect(newState).toEqual(expectedState); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments