|
| 1 | +jest.mock('../../api') |
| 2 | + |
| 3 | +import{createStore}from'redux' |
| 4 | +import{api}from'../../api' |
| 5 | +import*asactionsfrom'../index' |
| 6 | + |
| 7 | +describe('actions',()=>{ |
| 8 | +conststore=()=>{ |
| 9 | +constreducer=jest.fn() |
| 10 | +const{ dispatch}=createStore(reducer) |
| 11 | +reducer.mockReset()// ignore @@redux/INIT |
| 12 | +return{ dispatch, reducer} |
| 13 | +} |
| 14 | + |
| 15 | +consteventually=(assertFn)=> |
| 16 | +newPromise((resolve,reject)=>{ |
| 17 | +setTimeout(()=>{ |
| 18 | +try{ |
| 19 | +assertFn() |
| 20 | +}catch(e){ |
| 21 | +returnreject(e) |
| 22 | +} |
| 23 | +resolve() |
| 24 | +},1) |
| 25 | +}) |
| 26 | + |
| 27 | +constexpectTypes=(reducer,types)=> |
| 28 | +()=> |
| 29 | +expect(reducer.mock.calls.map(x=>x[1].type)).toEqual(types) |
| 30 | + |
| 31 | +describe('.saveCount',()=>{ |
| 32 | +beforeEach(()=>{ |
| 33 | +api.save.mockReturnValue(Promise.resolve(null)) |
| 34 | +}) |
| 35 | + |
| 36 | +it('sends an API request',()=>{ |
| 37 | +actions.saveCount({value:'14'})(jest.fn()) |
| 38 | +expect(api.save.mock.calls.length).toEqual(1) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('when API request succeeds',()=>{ |
| 42 | +it('dispatches SAVE_COUNT_SUCCESS',()=>{ |
| 43 | +const{ dispatch, reducer}=store() |
| 44 | +actions.saveCount({value:14})(dispatch) |
| 45 | +returneventually(expectTypes(reducer,[ |
| 46 | +'SAVE_COUNT_REQUEST', |
| 47 | +'SAVE_COUNT_SUCCESS', |
| 48 | +])) |
| 49 | +}) |
| 50 | +}) |
| 51 | + |
| 52 | +describe('when API request fails',()=>{ |
| 53 | +beforeEach(()=>{ |
| 54 | +api.save.mockReturnValue(Promise.reject(newError('something terrible happened'))) |
| 55 | +}) |
| 56 | + |
| 57 | +it('dispatches SAVE_COUNT_ERROR',()=>{ |
| 58 | +const{ dispatch, reducer}=store() |
| 59 | +actions.saveCount({value:14})(dispatch) |
| 60 | +returneventually(expectTypes(reducer,[ |
| 61 | +'SAVE_COUNT_REQUEST', |
| 62 | +'SAVE_COUNT_ERROR', |
| 63 | +])) |
| 64 | +}) |
| 65 | + |
| 66 | +it('includes error message with SAVE_COUNT_ERROR',()=>{ |
| 67 | +const{ dispatch, reducer}=store() |
| 68 | +actions.saveCount({value:14})(dispatch) |
| 69 | +returneventually(()=>{ |
| 70 | +expect(reducer.mock.calls[1][1].error.message) |
| 71 | +.toEqual('something terrible happened') |
| 72 | +}) |
| 73 | +}) |
| 74 | + |
| 75 | +it('includes request with SAVE_COUNT_ERROR for convenience',()=>{ |
| 76 | +const{ dispatch, reducer}=store() |
| 77 | +actions.saveCount({value:14})(dispatch) |
| 78 | +returneventually(()=>{ |
| 79 | +expect(reducer.mock.calls[1][1].request).toEqual({value:14}) |
| 80 | +}) |
| 81 | +}) |
| 82 | +}) |
| 83 | +}) |
| 84 | + |
| 85 | +describe('.loadCount',()=>{ |
| 86 | +beforeEach(()=>{ |
| 87 | +api.load.mockReturnValue(Promise.resolve({value:14})) |
| 88 | +}) |
| 89 | + |
| 90 | +it('sends an API request',()=>{ |
| 91 | +actions.loadCount()(jest.fn()) |
| 92 | +expect(api.load.mock.calls.length).toEqual(1) |
| 93 | +}) |
| 94 | + |
| 95 | +describe('when API request succeeds',()=>{ |
| 96 | +it('dispatches LOAD_COUNT_SUCCESS',()=>{ |
| 97 | +const{ dispatch, reducer}=store() |
| 98 | +actions.loadCount()(dispatch) |
| 99 | +returneventually(expectTypes(reducer,[ |
| 100 | +'LOAD_COUNT_REQUEST', |
| 101 | +'LOAD_COUNT_SUCCESS', |
| 102 | +])) |
| 103 | +}) |
| 104 | + |
| 105 | +it('includes new value with LOAD_COUNT_SUCCESS',()=>{ |
| 106 | +const{ dispatch, reducer}=store() |
| 107 | +actions.loadCount()(dispatch) |
| 108 | +returneventually(()=>{ |
| 109 | +expect(reducer.mock.calls[1][1].response).toEqual({value:14}) |
| 110 | +}) |
| 111 | +}) |
| 112 | +}) |
| 113 | + |
| 114 | +describe('when API request fails',()=>{ |
| 115 | +beforeEach(()=>{ |
| 116 | +api.load.mockReturnValue(Promise.reject(newError('something terrible happened'))) |
| 117 | +}) |
| 118 | + |
| 119 | +it('dispatches LOAD_COUNT_ERROR',()=>{ |
| 120 | +const{ dispatch, reducer}=store() |
| 121 | +actions.loadCount()(dispatch) |
| 122 | +returneventually(expectTypes(reducer,[ |
| 123 | +'LOAD_COUNT_REQUEST', |
| 124 | +'LOAD_COUNT_ERROR', |
| 125 | +])) |
| 126 | +}) |
| 127 | + |
| 128 | +it('includes error message with LOAD_COUNT_ERROR',()=>{ |
| 129 | +const{ dispatch, reducer}=store() |
| 130 | +actions.loadCount()(dispatch) |
| 131 | +returneventually(()=>{ |
| 132 | +expect(reducer.mock.calls[1][1].error.message) |
| 133 | +.toEqual('something terrible happened') |
| 134 | +}) |
| 135 | +}) |
| 136 | +}) |
| 137 | +}) |
| 138 | +}) |