Skip to content

Commit 60dde01

Browse files
committed
Implement testing for Wordpress & Messages reducers
1 parent b0dc88e commit 60dde01

File tree

9 files changed

+206
-24
lines changed

9 files changed

+206
-24
lines changed

src/actions/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ export const unAuthUser = () => {
6969
};
7070
};
7171

72-
export interface ForTestingAction {
72+
export interface TestingAuthAction {
7373
type: ActionAuthTypes.forTesting;
7474
}

src/actions/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ export enum ActionWPTypes {
44
deletePost = 'DELETE_POST',
55
updatePost = 'UPDATE_POST',
66
publishPost = 'PUBLISH_POST',
7-
fetchCurrentUser = 'FETCH_CURRENT_USER'
7+
fetchCurrentUser = 'FETCH_CURRENT_USER',
8+
forTesting = 'TESTING_WP_TYPE'
89
}
910

1011
export enum ActionAuthTypes {
1112
authUser = 'AUTH_USER',
1213
unAuthUser = 'UNAUTH_USER',
1314
currentUser = 'CURRENT_USER',
14-
forTesting = 'FOR_TESTING'
15+
forTesting = 'TESTING_AUTH_TYPE'
1516
}
1617

1718
export enum ActionMessagesTypes {
1819
warningMsg = 'WARNING_MESSAGE',
1920
successMsg = 'SUCCESS_MESSAGE',
20-
clearMsg = 'CLEAR_MSG'
21+
clearMsg = 'CLEAR_MSG',
22+
forTesting = 'TESTING_MESSAGE_TYPE'
2123
}

src/actions/wp.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,11 @@ export const publishPost = (newPostData: NewPostData, callback: () => void) => {
180180
callback();
181181
};
182182
};
183+
184+
export interface TestingMessageAction {
185+
type: ActionMessagesTypes.forTesting;
186+
}
187+
188+
export interface TestingWPAction {
189+
type: ActionWPTypes.forTesting;
190+
}

src/reducers/__tests__/auth.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import {
33
ActionAuthTypes,
44
AuthUserAction,
55
UnAuthUserAction,
6-
ForTestingAction
6+
TestingAuthAction
77
} from 'actions';
88

99
describe('Auth reducer', () => {
1010
it('should return default state', () => {
1111
const action = {
1212
type: ActionAuthTypes.forTesting
13-
} as ForTestingAction;
13+
} as TestingAuthAction;
1414

1515
const newState = authReducer(initialState, action);
1616

17-
expect(newState.authenticated).toEqual(false);
17+
expect(newState).toMatchObject(initialState);
1818
});
1919

2020
it('handles an action type of authUser', () => {

src/reducers/__tests__/msg.test.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import msgReducer, { initialState } from 'reducers/msg_reducer';
2+
import {
3+
ActionMessagesTypes,
4+
TestingMessageAction,
5+
ClearMessageAction,
6+
SuccessMessageAction
7+
} from 'actions';
8+
9+
describe('Message reducer', () => {
10+
it('should return default state', () => {
11+
const action = {
12+
type: ActionMessagesTypes.forTesting
13+
} as TestingMessageAction;
14+
15+
const newState = msgReducer(initialState, action);
16+
17+
expect(newState).toMatchObject(initialState);
18+
});
19+
20+
it('handles an action type of clearMsg', () => {
21+
const action = {
22+
type: ActionMessagesTypes.clearMsg
23+
} as ClearMessageAction;
24+
25+
const newState = msgReducer(initialState, action);
26+
27+
expect(newState).toMatchObject(initialState);
28+
});
29+
30+
it('handles an action type of successMsg', () => {
31+
const testMsg = 'Test success msg';
32+
const action = {
33+
type: ActionMessagesTypes.successMsg,
34+
payload: testMsg
35+
} as SuccessMessageAction;
36+
37+
const newState = msgReducer(initialState, action);
38+
39+
expect(newState.success).toEqual(testMsg);
40+
});
41+
42+
it('handles an action type of warningMessage', () => {
43+
const testMsg = 'Test warning msg';
44+
const action = {
45+
type: ActionMessagesTypes.warningMsg,
46+
payload: testMsg
47+
};
48+
49+
const newState = msgReducer(initialState, action);
50+
51+
expect(newState.warning).toEqual(testMsg);
52+
});
53+
});

src/reducers/__tests__/wp.test.tsx

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
});

src/reducers/auth_reducer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { AuthUserAction, UnAuthUserAction, ForTestingAction } from '../actions';
1+
import {
2+
AuthUserAction,
3+
UnAuthUserAction,
4+
TestingAuthAction
5+
} from '../actions';
26
import { ActionAuthTypes } from '../actions';
37

4-
type Actions = AuthUserAction | UnAuthUserAction | ForTestingAction;
8+
type Actions = AuthUserAction | UnAuthUserAction | TestingAuthAction;
59

610
export interface AuthState {
711
authenticated: boolean;

src/reducers/msg_reducer.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,34 @@ import {
22
WarningMessageAction,
33
ActionMessagesTypes,
44
ClearMessageAction,
5-
SuccessMessageAction
6-
} from "../actions";
5+
SuccessMessageAction,
6+
TestingMessageAction
7+
} from '../actions';
78

8-
type Actions = WarningMessageAction | ClearMessageAction | SuccessMessageAction;
9+
type Actions =
10+
| WarningMessageAction
11+
| ClearMessageAction
12+
| SuccessMessageAction
13+
| TestingMessageAction;
914

1015
interface IinitialState {
11-
error: string;
1216
warning: string;
1317
success: string;
1418
}
1519

16-
const intialState: IinitialState = {
17-
error: "",
18-
warning: "",
19-
success: ""
20+
export const initialState: IinitialState = {
21+
warning: '',
22+
success: ''
2023
};
2124

22-
export default function(state = intialState, action: Actions) {
25+
export default function(state = initialState, action: Actions) {
2326
switch (action.type) {
2427
case ActionMessagesTypes.warningMsg:
2528
return { ...state, warning: action.payload };
26-
case ActionMessagesTypes.clearMsg:
27-
return intialState;
2829
case ActionMessagesTypes.successMsg:
2930
return { ...state, success: action.payload };
31+
case ActionMessagesTypes.clearMsg:
32+
return initialState;
3033
default:
3134
return state;
3235
}

src/reducers/wp_reducer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@ import {
77
UpdatePostAction,
88
PublishPostAction,
99
DeletePostAction,
10+
TestingWPAction,
1011
Post
11-
} from "../actions";
12+
} from '../actions';
1213

1314
type Actions =
1415
| FetchCurrentUserAction
1516
| FetchPostsAction
1617
| FetchPostAction
1718
| UpdatePostAction
1819
| PublishPostAction
19-
| DeletePostAction;
20+
| DeletePostAction
21+
| TestingWPAction;
2022

2123
interface InitialState {
2224
currentUser?: User;
2325
posts: Post[];
2426
post?: Post;
2527
}
2628

27-
const initialValues: InitialState = {
29+
export const initialState: InitialState = {
2830
posts: []
2931
};
3032

31-
export default function(state = initialValues, action: Actions) {
33+
export default function(state = initialState, action: Actions) {
3234
switch (action.type) {
3335
case ActionWPTypes.fetchCurrentUser:
3436
return { ...state, currentUser: action.payload };

0 commit comments

Comments
 (0)