Skip to content

Commit fb568b5

Browse files
committed
added Jest testing for context slice
1 parent f977eaf commit fb568b5

File tree

5 files changed

+193
-23
lines changed

5 files changed

+193
-23
lines changed

__tests__/contextReducer.test.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import subject from '../app/src/redux/reducers/slice/contextReducer';
2+
3+
describe('Context Reducer', () => {
4+
let state;
5+
6+
beforeEach(() => {
7+
state = {
8+
allContext: []
9+
};
10+
});
11+
12+
describe('default state', () => {
13+
it('should return a default state when given an undefined input', () => {
14+
expect(subject(undefined, { type: undefined })).toEqual(state);
15+
});
16+
});
17+
18+
describe('unrecognized action types', () => {
19+
it('should return the original state without any duplication', () => {
20+
expect(subject(state, { type: 'REMOVE_STATE' })).toBe(state);
21+
});
22+
});
23+
24+
describe('ADD_CONTEXT', () => {
25+
const action = {
26+
type: 'ADD_CONTEXT',
27+
payload: {
28+
name: 'Theme Context'
29+
}
30+
};
31+
32+
it('adds a context', () => {
33+
const { allContext } = subject(state, action);
34+
expect(allContext[0]).toEqual({
35+
name: 'Theme Context',
36+
values: [],
37+
components: []
38+
});
39+
});
40+
41+
it('returns a state object not strictly equal to the original', () => {
42+
const newState = subject(state, action);
43+
expect(newState).not.toBe(state);
44+
});
45+
46+
it('should immutably update the nested state object', () => {
47+
const { allContext } = subject(state, action);
48+
expect(allContext).not.toBe(state.allContext);
49+
});
50+
});
51+
52+
describe('ADD_CONTEXT_VALUES', () => {
53+
beforeEach(() => {
54+
state = {
55+
allContext: [
56+
{
57+
name: 'Theme Context',
58+
values: [],
59+
components: []
60+
}
61+
]
62+
};
63+
});
64+
65+
const action = {
66+
type: 'ADD_CONTEXT_VALUES',
67+
payload: {
68+
name: 'Theme Context',
69+
inputKey: 'Theme Color',
70+
inputValue: 'Dark'
71+
}
72+
};
73+
74+
it('adds a key-value pair to values array of the specified context', () => {
75+
const { allContext } = subject(state, action);
76+
expect(allContext[0].values.length).toEqual(1);
77+
expect(allContext[0].values[0].key).toEqual('Theme Color');
78+
expect(allContext[0].values[0].value).toEqual('Dark');
79+
});
80+
81+
it('includes an allContext not strictly equal to the original', () => {
82+
const { allContext } = subject(state, action);
83+
84+
expect(allContext).not.toBe(state.allContext);
85+
});
86+
});
87+
88+
describe('DELETE CONTEXT', () => {
89+
let action;
90+
beforeEach(() => {
91+
state = {
92+
allContext: [
93+
{
94+
name: 'Theme Context',
95+
values: [],
96+
components: []
97+
},
98+
{
99+
name: 'To be deleted',
100+
values: [],
101+
components: []
102+
}
103+
]
104+
};
105+
106+
action = {
107+
type: 'DELETE_CONTEXT',
108+
payload: {
109+
name: 'Theme Context'
110+
}
111+
};
112+
});
113+
114+
it('removes specified context from the state', () => {
115+
const { allContext } = subject(state, action);
116+
117+
expect(allContext.length).toEqual(1);
118+
});
119+
120+
it('includes an allContext not strictly equal to the original', () => {
121+
const { allContext } = subject(state, action);
122+
123+
expect(allContext).not.toBe(state.allContext);
124+
});
125+
});
126+
127+
describe('ADD_COMPONENT_TO_CONTEXT', () => {
128+
beforeEach(() => {
129+
state = {
130+
allContext: [
131+
{
132+
name: 'Theme Context',
133+
values: [],
134+
components: []
135+
}
136+
]
137+
};
138+
});
139+
140+
const action = {
141+
type: 'ADD_COMPONENT_TO_CONTEXT',
142+
payload: {
143+
context: {
144+
name: 'Theme Context'
145+
},
146+
component: {
147+
name: 'Main Component'
148+
}
149+
}
150+
};
151+
152+
it('adds a new component to the specified context', () => {
153+
const { allContext } = subject(state, action);
154+
155+
expect(allContext[0].components.length).toEqual(1);
156+
expect(allContext[0].components[0]).toEqual('Main Component');
157+
});
158+
159+
it('includes an allContext not strictly equal to the original', () => {
160+
const { allContext } = subject(state, action);
161+
162+
expect(allContext).not.toBe(state.allContext);
163+
});
164+
});
165+
});

app/src/redux/actions/actions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const darkModeToggle = () => ({
44
type: types.DARK_MODE_TOGGLE
55
});
66

7+
//actions for context slice
78
export const addContextActionCreator = contextName => ({
89
type: types.ADD_CONTEXT,
910
payload: contextName
@@ -19,7 +20,7 @@ export const addComponentToContext = newEntry => ({
1920
payload: newEntry
2021
});
2122

22-
export const deleteContext = (contextInput) => ({
23+
export const deleteContext = contextInput => ({
2324
type: types.DELETE_CONTEXT,
2425
payload: contextInput
25-
})
26+
});

app/src/redux/constants/actionTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export const CODE_PREVIEW_INPUT = 'CODE_PREVIEW_INPUT';
55
export const ADD_CONTEXT = 'ADD_CONTEXT';
66
export const ADD_CONTEXT_VALUES = 'ADD_CONTEXT_VALUES';
77
export const ADD_COMPONENT_TO_CONTEXT = 'ADD_COMPONENT_TO_CONTEXT';
8-
export const DELETE_CONTEXT = 'DELETE_CONTEXT;'
8+
export const DELETE_CONTEXT = 'DELETE_CONTEXT';

app/src/redux/reducers/slice/contextReducer.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import * as types from '../../constants/actionTypes';
22

33
const initialState = {
44
allContext: [
5-
{
6-
name: 'ContextExample1',
7-
values: [
8-
{ key: 'testKey1', value: 'testValue1' },
9-
{ key: 'testKey2', value: 'testValue2' }
10-
],
11-
components: ['MainContainer', 'SubmitForm']
12-
},
13-
{
14-
name: 'ContextExample2',
15-
values: [
16-
{ key: 'testKey3', value: 'testValue3' },
17-
{ key: 'testKey33', value: 'testValue33' }
18-
],
19-
components: ['MainContainer', 'EditForm', 'TableContainer']
20-
}
5+
// {
6+
// name: 'ContextExample1',
7+
// values: [
8+
// { key: 'testKey1', value: 'testValue1' },
9+
// { key: 'testKey2', value: 'testValue2' }
10+
// ],
11+
// components: ['MainContainer', 'SubmitForm']
12+
// },
13+
// {
14+
// name: 'ContextExample2',
15+
// values: [
16+
// { key: 'testKey3', value: 'testValue3' },
17+
// { key: 'testKey33', value: 'testValue33' }
18+
// ],
19+
// components: ['MainContainer', 'EditForm', 'TableContainer']
20+
// }
2121
]
2222
// allContext: []
2323
};
@@ -52,12 +52,15 @@ const contextReducer = (state = initialState, action) => {
5252
...state,
5353
allContext: newAllContext
5454
};
55+
5556
case types.DELETE_CONTEXT:
56-
const remains = state.allContext.filter((el) => el.name !== action.payload.name)
57+
const tempState = [...state.allContext];
58+
const remains = tempState.filter(el => el.name !== action.payload.name);
59+
5760
return {
5861
...state,
59-
allContext : remains
60-
}
62+
allContext: remains
63+
};
6164

6265
case types.ADD_COMPONENT_TO_CONTEXT:
6366
const newTempState = [...state.allContext];

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"dist-linux": "electron-builder --linux",
6868
"dist-windows": "electron-builder --windows",
6969
"dist-all": "npm run prod-build && electron-builder --mac --linux --windows",
70-
"test": "concurrently \"npm run dev-server\" \"cross-env NODE_ENV=test jest --verbose \"",
70+
"test": "jest --verbose",
7171
"server": "cross-env NODE_ENV=development nodemon server/server.js"
7272
},
7373
"repository": {
@@ -139,6 +139,7 @@
139139
"electron-debug": "^3.2.0",
140140
"electron-devtools-installer": "^3.2.0",
141141
"electron-window-manager": "^1.0.6",
142+
"enzyme": "^3.11.0",
142143
"esbuild-wasm": "^0.8.27",
143144
"eslint-plugin-react-hooks": "^4.2.0",
144145
"express-graphql": "^0.12.0",

0 commit comments

Comments
 (0)