|
| 1 | +import React from 'react'; |
| 2 | +import { searchReducer, initialState } from 'search/search.reducer'; |
| 3 | +import { SEARCH_REPOS, SEARCH_USERS } from 'search/search.type'; |
| 4 | +import { repos, users } from 'testData/api/search'; |
| 5 | +import { authError } from 'testData/api/error'; |
| 6 | + |
| 7 | +describe('Search Reducer', () => { |
| 8 | + it('should return the initial state', () => { |
| 9 | + expect(searchReducer(undefined, {})).toEqual(initialState); |
| 10 | + }); |
| 11 | + |
| 12 | + /** |
| 13 | + * Search repositories. |
| 14 | + */ |
| 15 | + it('should set repos to empty and set isPendingSearchRepos to true when SEARCH_REPOS.PENDING action is dispatched', () => { |
| 16 | + const expectedState = { |
| 17 | + ...initialState, |
| 18 | + repos: [], |
| 19 | + isPendingSearchRepos: true, |
| 20 | + }; |
| 21 | + const action = { |
| 22 | + type: SEARCH_REPOS.PENDING, |
| 23 | + }; |
| 24 | + |
| 25 | + expect(searchReducer(initialState, action)).toEqual(expectedState); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should set repos from payload of SEARCH_REPOS.SUCCESS action and set isPendingSearchRepos to false', () => { |
| 29 | + const expectedState = { |
| 30 | + ...initialState, |
| 31 | + repos: repos.items, |
| 32 | + isPendingSearchRepos: false, |
| 33 | + }; |
| 34 | + // The 'searchRepos' action creator dispatches this action with repos.items payload. |
| 35 | + const action = { |
| 36 | + type: SEARCH_REPOS.SUCCESS, |
| 37 | + payload: repos.items, |
| 38 | + }; |
| 39 | + |
| 40 | + expect(searchReducer(initialState, action)).toEqual(expectedState); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should set an error from payload of SEARCH_REPOS.ERROR action and set isPendingSearchRepos to false', () => { |
| 44 | + const expectedState = { |
| 45 | + ...initialState, |
| 46 | + error: authError, |
| 47 | + isPendingSearchRepos: false, |
| 48 | + }; |
| 49 | + // The 'searchRepos' action creator dispatches this action with data.items payload. |
| 50 | + const action = { |
| 51 | + type: SEARCH_REPOS.ERROR, |
| 52 | + payload: authError, |
| 53 | + }; |
| 54 | + |
| 55 | + expect(searchReducer(initialState, action)).toEqual(expectedState); |
| 56 | + }); |
| 57 | + |
| 58 | + /** |
| 59 | + * Search users. |
| 60 | + */ |
| 61 | + it('should set users to empty and set isPendingSearchUsers to true when SEARCH_USERS.PENDING action is dispatched', () => { |
| 62 | + const expectedState = { |
| 63 | + ...initialState, |
| 64 | + users: [], |
| 65 | + isPendingSearchUsers: true, |
| 66 | + }; |
| 67 | + const action = { |
| 68 | + type: SEARCH_USERS.PENDING, |
| 69 | + }; |
| 70 | + |
| 71 | + expect(searchReducer(initialState, action)).toEqual(expectedState); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should set users from payload of SEARCH_USERS.SUCCESS action and set isPendingSearchUsers to false', () => { |
| 75 | + const expectedState = { |
| 76 | + ...initialState, |
| 77 | + users: users.items, |
| 78 | + isPendingSearchUsers: false, |
| 79 | + }; |
| 80 | + // The 'searchUsers' action creator dispatches this action with data.items payload. |
| 81 | + const action = { |
| 82 | + type: SEARCH_USERS.SUCCESS, |
| 83 | + payload: users.items, |
| 84 | + }; |
| 85 | + |
| 86 | + expect(searchReducer(initialState, action)).toEqual(expectedState); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should set an error from payload of SEARCH_USERS.ERROR action and set isPendingSearchUsers to false', () => { |
| 90 | + const expectedState = { |
| 91 | + ...initialState, |
| 92 | + error: authError, |
| 93 | + isPendingSearchUsers: false, |
| 94 | + }; |
| 95 | + // The 'searchRepos' action creator dispatches this action with repos.items payload. |
| 96 | + const action = { |
| 97 | + type: SEARCH_REPOS.ERROR, |
| 98 | + payload: authError, |
| 99 | + }; |
| 100 | + |
| 101 | + expect(searchReducer(initialState, action)).toEqual(expectedState); |
| 102 | + }); |
| 103 | +}); |
0 commit comments