|
| 1 | +import test from 'tape'; |
| 2 | +import { createDuck } from '../build/index.js'; |
| 3 | + |
| 4 | +test('create duck', t => { |
| 5 | + t.plan(6); |
| 6 | + |
| 7 | + const duck = createDuck('test', 'redux-duck'); |
| 8 | + |
| 9 | + t.ok(duck.defineType, 'it should had a key `defineType`'); |
| 10 | + t.equals( |
| 11 | + typeof duck.defineType, |
| 12 | + 'function', |
| 13 | + 'it should be a function' |
| 14 | + ); |
| 15 | + |
| 16 | + t.ok(duck.createReducer, 'it should had a key `createReducer`'); |
| 17 | + t.equals( |
| 18 | + typeof duck.createReducer, |
| 19 | + 'function', |
| 20 | + 'it should be a function' |
| 21 | + ); |
| 22 | + |
| 23 | + t.ok(duck.createAction, 'it should had a key `createAction`'); |
| 24 | + t.equals( |
| 25 | + typeof duck.createAction, |
| 26 | + 'function', |
| 27 | + 'it should be a function' |
| 28 | + ); |
| 29 | +}); |
| 30 | + |
| 31 | +test('define type', t => { |
| 32 | + t.plan(2); |
| 33 | + |
| 34 | + const duck1 = createDuck('test1', 'redux-duck'); |
| 35 | + const duck2 = createDuck('test2', 'redux-duck'); |
| 36 | + |
| 37 | + const type1 = duck1.defineType('TYPE'); |
| 38 | + const type2 = duck2.defineType('TYPE'); |
| 39 | + |
| 40 | + t.equals( |
| 41 | + type1, |
| 42 | + 'redux-duck/test1/TYPE', |
| 43 | + 'it should be the expected action type string' |
| 44 | + ); |
| 45 | + |
| 46 | + t.equals( |
| 47 | + type2, |
| 48 | + 'redux-duck/test2/TYPE', |
| 49 | + 'it should be the expected action type string' |
| 50 | + ); |
| 51 | +}); |
| 52 | + |
| 53 | +test('create action creator', t => { |
| 54 | + t.plan(3); |
| 55 | + |
| 56 | + const duck = createDuck('test', 'redux-duck'); |
| 57 | + |
| 58 | + const type = duck.defineType('TYPE'); |
| 59 | + |
| 60 | + const createType = duck.createAction(type); |
| 61 | + |
| 62 | + const testData = { |
| 63 | + id: 123, |
| 64 | + message: 'hello world', |
| 65 | + }; |
| 66 | + |
| 67 | + const action = createType(testData); |
| 68 | + |
| 69 | + const emptyActon = createType(); |
| 70 | + |
| 71 | + t.equals( |
| 72 | + typeof createType, |
| 73 | + 'function', |
| 74 | + 'it should create a valid function' |
| 75 | + ); |
| 76 | + |
| 77 | + t.deepEquals( |
| 78 | + action, |
| 79 | + { |
| 80 | + type, |
| 81 | + payload: testData, |
| 82 | + }, |
| 83 | + 'it should create a valid action object' |
| 84 | + ); |
| 85 | + |
| 86 | + t.deepEquals( |
| 87 | + emptyActon, |
| 88 | + { |
| 89 | + type, |
| 90 | + }, |
| 91 | + 'it should be able to create an action without payload' |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +test('create reducer', t => { |
| 96 | + t.plan(3); |
| 97 | + |
| 98 | + const duck = createDuck('test', 'redux-duck'); |
| 99 | + |
| 100 | + const type = duck.defineType('TYPE'); |
| 101 | + |
| 102 | + const reducer = duck.createReducer({ |
| 103 | + [type]: (state, action) => ({ |
| 104 | + ...state, |
| 105 | + [action.payload.id]: action.payload, |
| 106 | + }), |
| 107 | + }, {}); |
| 108 | + |
| 109 | + const testData = { |
| 110 | + id: 123, |
| 111 | + message: 'hello world', |
| 112 | + }; |
| 113 | + |
| 114 | + const testAction = { |
| 115 | + type, |
| 116 | + payload: testData, |
| 117 | + }; |
| 118 | + |
| 119 | + const state = reducer({}, testAction); |
| 120 | + |
| 121 | + t.equals( |
| 122 | + typeof reducer, |
| 123 | + 'function', |
| 124 | + 'the reducer should be a function' |
| 125 | + ); |
| 126 | + |
| 127 | + t.deepEquals( |
| 128 | + reducer(), |
| 129 | + {}, |
| 130 | + 'the reducer should be able to return the default state' |
| 131 | + ); |
| 132 | + |
| 133 | + t.deepEquals( |
| 134 | + state, |
| 135 | + { |
| 136 | + [testData.id]: testData, |
| 137 | + }, |
| 138 | + 'the reducer should work with the defined cases' |
| 139 | + ); |
| 140 | +}); |
0 commit comments