|
| 1 | +import wpReducer, { initialState } from 'reducers/wp_reducer'; |
| 2 | +import { |
| 3 | + ActionWPTypes, |
| 4 | + TestingWPAction, |
| 5 | + Post, |
| 6 | + User, |
| 7 | + FetchPostAction, |
| 8 | + FetchPostsAction, |
| 9 | + DeletePostAction, |
| 10 | + UpdatePostAction, |
| 11 | + PublishPostAction, |
| 12 | + FetchCurrentUserAction |
| 13 | +} from 'actions'; |
| 14 | + |
| 15 | +describe('WP reducer', () => { |
| 16 | + let post: Post; |
| 17 | + let postArr: Post[]; |
| 18 | + beforeEach(() => { |
| 19 | + post = { |
| 20 | + id: 1, |
| 21 | + title: { |
| 22 | + rendered: 'Test Title' |
| 23 | + }, |
| 24 | + content: { |
| 25 | + rendered: 'Test Content' |
| 26 | + }, |
| 27 | + excerpt: { |
| 28 | + rendered: 'Test Excerpt' |
| 29 | + }, |
| 30 | + modified: 'Sept 2019', |
| 31 | + date: 'Sept 2019' |
| 32 | + }; |
| 33 | + |
| 34 | + postArr = [post, { ...post, id: 2 }, { ...post, id: 3 }]; |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return default state', () => { |
| 38 | + const action = { |
| 39 | + type: ActionWPTypes.forTesting |
| 40 | + } as TestingWPAction; |
| 41 | + |
| 42 | + const newState = wpReducer(initialState, action); |
| 43 | + expect(newState).toMatchObject(initialState); |
| 44 | + }); |
| 45 | + |
| 46 | + it('handles an action of type fetchPosts', () => { |
| 47 | + const action = { |
| 48 | + type: ActionWPTypes.fetchPosts, |
| 49 | + payload: postArr |
| 50 | + } as FetchPostsAction; |
| 51 | + |
| 52 | + const newState = wpReducer(initialState, action); |
| 53 | + expect(newState.posts).toEqual(postArr); |
| 54 | + }); |
| 55 | + |
| 56 | + it('handles an action of type fetchPost', () => { |
| 57 | + const action = { |
| 58 | + type: ActionWPTypes.fetchPost, |
| 59 | + payload: post |
| 60 | + } as FetchPostAction; |
| 61 | + |
| 62 | + const newState = wpReducer(initialState, action); |
| 63 | + expect(newState.post).toMatchObject(post); |
| 64 | + }); |
| 65 | + |
| 66 | + it('handles an action of type deletePost', () => { |
| 67 | + const action = { |
| 68 | + type: ActionWPTypes.deletePost, |
| 69 | + payload: 1 |
| 70 | + } as DeletePostAction; |
| 71 | + |
| 72 | + const expectedOutput = [{ ...post, id: 2 }, { ...post, id: 3 }]; |
| 73 | + const newState = wpReducer({ ...initialState, posts: postArr }, action); |
| 74 | + expect(newState.posts).toEqual(expectedOutput); |
| 75 | + }); |
| 76 | + |
| 77 | + it('handles an action of type updatePost', () => { |
| 78 | + const action = { |
| 79 | + type: ActionWPTypes.updatePost |
| 80 | + } as UpdatePostAction; |
| 81 | + |
| 82 | + const newState = wpReducer(initialState, action); |
| 83 | + expect(newState).toMatchObject(initialState); |
| 84 | + }); |
| 85 | + |
| 86 | + it('handles an action of type publishPost', () => { |
| 87 | + const action = { |
| 88 | + type: ActionWPTypes.publishPost |
| 89 | + } as PublishPostAction; |
| 90 | + |
| 91 | + const newState = wpReducer(initialState, action); |
| 92 | + expect(newState).toMatchObject(initialState); |
| 93 | + }); |
| 94 | + |
| 95 | + it('handles an action of type fetchCurrentUser', () => { |
| 96 | + const sampleUser: User = { |
| 97 | + id: 1, |
| 98 | + name: 'Lougie', |
| 99 | + description: 'Developer', |
| 100 | + url: 'https://lougiequisel.com' |
| 101 | + }; |
| 102 | + const action = { |
| 103 | + type: ActionWPTypes.fetchCurrentUser, |
| 104 | + payload: sampleUser |
| 105 | + } as FetchCurrentUserAction; |
| 106 | + |
| 107 | + const newState = wpReducer(initialState, action); |
| 108 | + expect(newState.currentUser).toMatchObject(sampleUser); |
| 109 | + }); |
| 110 | +}); |
0 commit comments