Skip to content

Commit fd16858

Browse files
committed
Added boolean test for StateManager tab
1 parent 097bb9c commit fd16858

File tree

4 files changed

+545
-376
lines changed

4 files changed

+545
-376
lines changed

__tests__/stateManagementReducer.test.js

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,19 @@
11
import reducer from '../app/src/redux/reducers/slice/appStateSlice';
2-
import { initialState } from '../app/src/redux/reducers/slice/appStateSlice';
2+
import state from './test_utils';
33

4-
let state = JSON.parse(JSON.stringify(initialState));
5-
state.components = [
6-
{
7-
id: 1,
8-
name: 'App',
9-
style: {},
10-
code: "import React, { useState, useEffect, useContext} from 'react';\n\n\n\nimport C1 from './C1'\nconst App = (props) => {\n\n\n const [appState, setAppState] = useState<number | undefined>(1);\n\n return(\n <>\n<C1 id = \"1\" />\n </>\n );\n}\n\nexport default App\n",
11-
children: [
12-
{
13-
type: 'HTML Element',
14-
typeId: 1000,
15-
name: 'separator',
16-
childId: 1000,
17-
style: { border: 'none' },
18-
attributes: {},
19-
children: []
20-
},
21-
{
22-
type: 'Component',
23-
typeId: 2,
24-
name: 'C1',
25-
childId: 1,
26-
style: {},
27-
attributes: {},
28-
children: [],
29-
stateProps: [],
30-
passedInProps: []
31-
}
32-
],
33-
isPage: true,
34-
past: [[]],
35-
future: [],
36-
stateProps: [],
37-
useStateCodes: [
38-
'const [appState, setAppState] = useState<number | undefined>(1)'
39-
]
40-
},
41-
{
42-
id: 2,
43-
name: 'C1',
44-
nextChildId: 1,
45-
style: {},
46-
attributes: {},
47-
code: "import React, { useState, useEffect, useContext} from 'react';\n\n\n\n\nconst C1 = (props) => {\n\n\n\n return(\n <>\n\n </>\n );\n}\n\nexport default C1\n",
48-
children: [],
49-
isPage: false,
50-
past: [],
51-
future: [],
52-
stateProps: [],
53-
useStateCodes: [],
54-
passedInProps: []
55-
}
56-
];
4+
//initializing copy of initial state to be used for test suite
575

58-
describe('Testing componentReducer functionality for stateManagement tab', () => {
59-
const findComponent = (components, componentId) => {
60-
return components.find((elem) => elem.id === componentId);
61-
};
6+
const findComponent = (components, componentId) => {
7+
return components.find((elem) => elem.id === componentId);
8+
};
629

10+
describe('Testing componentReducer functionality for stateManagement tab', () => {
6311
// TEST 'ADD STATE'
64-
describe('addState test', () => {
12+
describe('addState', () => {
6513
// setting canvas focus to root component (App)
6614
// state.canvasFocus.componentId = 1;
6715
// action dispatched to be tested
68-
const action = {
16+
const action1 = {
6917
type: 'appState/addState',
7018
payload: {
7119
newState: {
@@ -87,10 +35,10 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
8735
};
8836

8937
// setting test state
90-
state = reducer(state, action);
91-
const currComponent = findComponent(state.components, 1);
38+
state = reducer(state, action1);
39+
let currComponent = findComponent(state.components, 1);
9240

93-
it('should add state and its function modifier to the stateProps array of the current component', () => {
41+
it('should add state and its setter function to the stateProps array of the current component', () => {
9442
expect(currComponent.stateProps.length).toEqual(2);
9543
});
9644
it(`state id should be 'App-testAppState'`, () => {
@@ -102,7 +50,7 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
10250
it(`state value should be 1`, () => {
10351
expect(currComponent.stateProps[0].value).toEqual(1);
10452
});
105-
it(`state key should be number`, () => {
53+
it(`state value type should be 'number'`, () => {
10654
expect(currComponent.stateProps[0].type).toEqual('number');
10755
});
10856
it(`function state id should be 'App-setTestAppState'`, () => {
@@ -117,12 +65,45 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
11765
it(`function state key should be func`, () => {
11866
expect(currComponent.stateProps[1].type).toEqual('func');
11967
});
68+
69+
const action2 = {
70+
type: 'appState/addState',
71+
payload: {
72+
newState: {
73+
id: 'App-testAppState2',
74+
key: 'isLoggedIn',
75+
type: 'boolean',
76+
value: 'false'
77+
},
78+
setNewState: {
79+
id: 'App-setTestAppState2',
80+
key: 'setIsLoggedIn',
81+
type: 'func',
82+
value: ''
83+
},
84+
contextParam: {
85+
allContext: []
86+
}
87+
}
88+
};
89+
90+
state = reducer(state, action2);
91+
console.log(state.components[0].stateProps);
92+
93+
describe('should handle value with type of boolean', () => {
94+
it(`state key type should be boolean`, () => {
95+
expect(state.components[0].stateProps[2].type).toEqual('boolean');
96+
});
97+
it(`state value should be false`, () => {
98+
expect(state.components[0].stateProps[2].value).toEqual('false');
99+
});
100+
});
120101
});
121102

122103
// TEST 'ADD PASSEDINPROPS'
123-
describe('ADD PASSEDINPROPS test', () => {
124-
// setting canvas focus to the child component
104+
describe('addPassedInProps', () => {
125105
state = JSON.parse(JSON.stringify(state));
106+
// setting canvas focus to the child component
126107
state.canvasFocus.componentId = 2;
127108

128109
// action dispatched to be tested
@@ -159,7 +140,6 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
159140
it(`current component should have a state value type: 'number'`, () => {
160141
expect(currComponent.passedInProps[0].type).toEqual('number');
161142
});
162-
163143
//check parent children array to make sure it is being added here as well
164144
it(`parent component 'passedInProps' array length should be 1`, () => {
165145
expect(currComponent.passedInProps.length).toEqual(1);
@@ -185,7 +165,7 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
185165
});
186166

187167
// TEST 'DELETE PASSEDINPROPS'
188-
describe('DELETE PASSEDINPROPS test', () => {
168+
describe('deletePassedInProps', () => {
189169
it('should delete the state passed down from parent component in the child component', () => {
190170
// setting canvas focus to the child component
191171
state = JSON.parse(JSON.stringify(state));
@@ -213,7 +193,7 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
213193
});
214194

215195
// TEST 'DELETE STATE'
216-
describe('DELETE STATE test', () => {
196+
describe('deleteState', () => {
217197
it('should delete all instances of state from stateProps and passedInProps', () => {
218198
// setting canvas focus to root component
219199
state = JSON.parse(JSON.stringify(state));

__tests__/test_utils.jsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { initialState } from '../app/src/redux/reducers/slice/appStateSlice';
2+
3+
export const state = JSON.parse(JSON.stringify(initialState));
4+
state.components = [
5+
{
6+
id: 1,
7+
name: 'App',
8+
style: {},
9+
code: "import React, { useState, useEffect, useContext} from 'react';\n\n\n\nimport C1 from './C1'\nconst App = (props) => {\n\n\n const [appState, setAppState] = useState<number | undefined>(1);\n\n return(\n <>\n<C1 id = \"1\" />\n </>\n );\n}\n\nexport default App\n",
10+
children: [
11+
{
12+
type: 'HTML Element',
13+
typeId: 1000,
14+
name: 'separator',
15+
childId: 1000,
16+
style: { border: 'none' },
17+
attributes: {},
18+
children: []
19+
},
20+
{
21+
type: 'Component',
22+
typeId: 2,
23+
name: 'C1',
24+
childId: 1,
25+
style: {},
26+
attributes: {},
27+
children: [],
28+
stateProps: [],
29+
passedInProps: []
30+
}
31+
],
32+
isPage: true,
33+
past: [[]],
34+
future: [],
35+
stateProps: [],
36+
useStateCodes: [
37+
'const [appState, setAppState] = useState<number | undefined>(1)'
38+
]
39+
},
40+
{
41+
id: 2,
42+
name: 'C1',
43+
nextChildId: 1,
44+
style: {},
45+
attributes: {},
46+
code: "import React, { useState, useEffect, useContext} from 'react';\n\n\n\n\nconst C1 = (props) => {\n\n\n\n return(\n <>\n\n </>\n );\n}\n\nexport default C1\n",
47+
children: [],
48+
isPage: false,
49+
past: [],
50+
future: [],
51+
stateProps: [],
52+
useStateCodes: [],
53+
passedInProps: []
54+
}
55+
];

0 commit comments

Comments
 (0)