1
1
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 ' ;
3
3
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
57
5
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
+ } ;
62
9
10
+ describe ( 'Testing componentReducer functionality for stateManagement tab' , ( ) => {
63
11
// TEST 'ADD STATE'
64
- describe ( 'addState test ' , ( ) => {
12
+ describe ( 'addState' , ( ) => {
65
13
// setting canvas focus to root component (App)
66
14
// state.canvasFocus.componentId = 1;
67
15
// action dispatched to be tested
68
- const action = {
16
+ const action1 = {
69
17
type : 'appState/addState' ,
70
18
payload : {
71
19
newState : {
@@ -87,10 +35,10 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
87
35
} ;
88
36
89
37
// 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 ) ;
92
40
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' , ( ) => {
94
42
expect ( currComponent . stateProps . length ) . toEqual ( 2 ) ;
95
43
} ) ;
96
44
it ( `state id should be 'App-testAppState'` , ( ) => {
@@ -102,7 +50,7 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
102
50
it ( `state value should be 1` , ( ) => {
103
51
expect ( currComponent . stateProps [ 0 ] . value ) . toEqual ( 1 ) ;
104
52
} ) ;
105
- it ( `state key should be number` , ( ) => {
53
+ it ( `state value type should be ' number' ` , ( ) => {
106
54
expect ( currComponent . stateProps [ 0 ] . type ) . toEqual ( 'number' ) ;
107
55
} ) ;
108
56
it ( `function state id should be 'App-setTestAppState'` , ( ) => {
@@ -117,12 +65,45 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
117
65
it ( `function state key should be func` , ( ) => {
118
66
expect ( currComponent . stateProps [ 1 ] . type ) . toEqual ( 'func' ) ;
119
67
} ) ;
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
+ } ) ;
120
101
} ) ;
121
102
122
103
// TEST 'ADD PASSEDINPROPS'
123
- describe ( 'ADD PASSEDINPROPS test' , ( ) => {
124
- // setting canvas focus to the child component
104
+ describe ( 'addPassedInProps' , ( ) => {
125
105
state = JSON . parse ( JSON . stringify ( state ) ) ;
106
+ // setting canvas focus to the child component
126
107
state . canvasFocus . componentId = 2 ;
127
108
128
109
// action dispatched to be tested
@@ -159,7 +140,6 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
159
140
it ( `current component should have a state value type: 'number'` , ( ) => {
160
141
expect ( currComponent . passedInProps [ 0 ] . type ) . toEqual ( 'number' ) ;
161
142
} ) ;
162
-
163
143
//check parent children array to make sure it is being added here as well
164
144
it ( `parent component 'passedInProps' array length should be 1` , ( ) => {
165
145
expect ( currComponent . passedInProps . length ) . toEqual ( 1 ) ;
@@ -185,7 +165,7 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
185
165
} ) ;
186
166
187
167
// TEST 'DELETE PASSEDINPROPS'
188
- describe ( 'DELETE PASSEDINPROPS test ' , ( ) => {
168
+ describe ( 'deletePassedInProps ' , ( ) => {
189
169
it ( 'should delete the state passed down from parent component in the child component' , ( ) => {
190
170
// setting canvas focus to the child component
191
171
state = JSON . parse ( JSON . stringify ( state ) ) ;
@@ -213,7 +193,7 @@ describe('Testing componentReducer functionality for stateManagement tab', () =>
213
193
} ) ;
214
194
215
195
// TEST 'DELETE STATE'
216
- describe ( 'DELETE STATE test ' , ( ) => {
196
+ describe ( 'deleteState ' , ( ) => {
217
197
it ( 'should delete all instances of state from stateProps and passedInProps' , ( ) => {
218
198
// setting canvas focus to root component
219
199
state = JSON . parse ( JSON . stringify ( state ) ) ;
0 commit comments