@@ -5,136 +5,131 @@ import ActionContainer from '../containers/ActionContainer';
5
5
import TravelContainer from '../containers/TravelContainer' ;
6
6
import { Provider } from 'react-redux' ;
7
7
import { configureStore } from '@reduxjs/toolkit' ;
8
- import { mainSlice } from '../slices/mainSlice'
8
+ import { mainSlice } from '../slices/mainSlice' ;
9
9
import { useDispatch } from 'react-redux' ;
10
10
//Note for testing:
11
- //typically, jest.mock is commonly used in unit testing to isolate the code under test.
12
- //In contrast, when performing integration testing of components with a real Redux store,
13
- //you typically don't need to use jest.mock because you're interested in testing how the real components interact with the actual store.
11
+ //typically, jest.mock is commonly used in unit testing to isolate the code under test.
12
+ //In contrast, when performing integration testing of components with a real Redux store,
13
+ //you typically don't need to use jest.mock because you're interested in testing how the real components interact with the actual store.
14
14
//The decision to use jest.mock depends on the type of testing (unit or integration) and your specific testing goals.
15
15
16
- const customTabs = {
17
- 87 : {
18
- snapshots : [ 1 , 2 , 3 , 4 ] ,
19
- hierarchy : {
20
- index : 0 ,
21
- name : 1 ,
22
- branch : 0 ,
23
- stateSnapshot : {
24
- state : { } ,
25
- children : [
26
- {
27
- state : { test : 'test' } ,
28
- name : 'App' ,
29
- componentData : { actualDuration : 3.5 } ,
30
- } ,
31
- ] ,
32
- route : {
33
- id : 1 ,
34
- url : 'http://localhost:8080/' ,
35
- } ,
36
- } ,
16
+ const customTabs = {
17
+ 87 : {
18
+ snapshots : [ 1 , 2 , 3 , 4 ] ,
19
+ hierarchy : {
20
+ index : 0 ,
21
+ name : 1 ,
22
+ branch : 0 ,
23
+ stateSnapshot : {
24
+ state : { } ,
25
+ children : [
26
+ {
27
+ state : { test : 'test' } ,
28
+ name : 'App' ,
29
+ componentData : { actualDuration : 3.5 } ,
30
+ } ,
31
+ ] ,
32
+ route : {
33
+ id : 1 ,
34
+ url : 'http://localhost:8080/' ,
35
+ } ,
36
+ } ,
37
+ children : [
38
+ {
39
+ index : 1 ,
40
+ name : 2 ,
41
+ branch : 0 ,
42
+ stateSnapshot : {
43
+ state : { } ,
37
44
children : [
38
45
{
39
- index : 1 ,
40
- name : 2 ,
41
- branch : 0 ,
42
- stateSnapshot : {
43
- state : { } ,
44
- children : [
45
- {
46
- state : { test : 'test' } ,
47
- name : 'App' ,
48
- componentData : { actualDuration : 3.5 } ,
49
- } ,
50
- ] ,
51
- route : {
52
- id : 2 ,
53
- url : 'http://localhost:8080/' ,
54
- } ,
55
- } ,
46
+ state : { test : 'test' } ,
47
+ name : 'App' ,
48
+ componentData : { actualDuration : 3.5 } ,
49
+ } ,
50
+ ] ,
51
+ route : {
52
+ id : 2 ,
53
+ url : 'http://localhost:8080/' ,
54
+ } ,
55
+ } ,
56
+ children : [
57
+ {
58
+ index : 2 ,
59
+ name : 3 ,
60
+ branch : 0 ,
61
+ stateSnapshot : {
62
+ state : { } ,
56
63
children : [
57
64
{
58
- index : 2 ,
59
- name : 3 ,
60
- branch : 0 ,
61
- stateSnapshot : {
62
- state : { } ,
63
- children : [
64
- {
65
- state : { test : 'test' } ,
66
- name : 'App' ,
67
- componentData : { actualDuration : 3.5 } ,
68
- } ,
69
- ] ,
70
- route : {
71
- id : 3 ,
72
- url : 'http://localhost:8080/' ,
73
- } ,
74
- } ,
65
+ state : { test : 'test' } ,
66
+ name : 'App' ,
67
+ componentData : { actualDuration : 3.5 } ,
68
+ } ,
69
+ ] ,
70
+ route : {
71
+ id : 3 ,
72
+ url : 'http://localhost:8080/' ,
73
+ } ,
74
+ } ,
75
+ children : [
76
+ {
77
+ index : 3 ,
78
+ name : 4 ,
79
+ branch : 0 ,
80
+ stateSnapshot : {
81
+ state : { } ,
75
82
children : [
76
83
{
77
- index : 3 ,
78
- name : 4 ,
79
- branch : 0 ,
80
- stateSnapshot : {
81
- state : { } ,
82
- children : [
83
- {
84
- state : { test : 'test' } ,
85
- name : 'App' ,
86
- componentData : { actualDuration : 3.5 } ,
87
- } ,
88
- ] ,
89
- route : {
90
- id : 4 ,
91
- url : 'http://localhost:8080/test/' ,
92
- } ,
93
- } ,
94
- children : [ ] ,
84
+ state : { test : 'test' } ,
85
+ name : 'App' ,
86
+ componentData : { actualDuration : 3.5 } ,
95
87
} ,
96
88
] ,
89
+ route : {
90
+ id : 4 ,
91
+ url : 'http://localhost:8080/test/' ,
92
+ } ,
97
93
} ,
98
- ] ,
99
- } ,
100
- ] ,
101
- } ,
102
- currLocation : {
103
- index : 0 ,
104
- name : 1 ,
105
- branch : 0 ,
106
- } ,
107
- sliderIndex : 0 ,
108
- viewIndex : - 1 ,
109
- } ,
110
- }
111
-
112
- const customInitialState = {
113
- main : {
114
- port : null ,
115
- currentTab : 87 , // Update with your desired value
116
- currentTitle : 'test string' ,
117
- tabs : customTabs , // Replace with the actual (testing) tab data
118
- currentTabInApp : 'test string' ,
119
- connectionStatus : false ,
120
- connectRequested : true ,
94
+ children : [ ] ,
95
+ } ,
96
+ ] ,
97
+ } ,
98
+ ] ,
121
99
} ,
122
- } ;
100
+ ] ,
101
+ } ,
102
+ currLocation : {
103
+ index : 0 ,
104
+ name : 1 ,
105
+ branch : 0 ,
106
+ } ,
107
+ sliderIndex : 0 ,
108
+ viewIndex : - 1 ,
109
+ } ,
110
+ } ;
111
+
112
+ const customInitialState = {
113
+ main : {
114
+ port : null ,
115
+ currentTab : 87 , // Update with your desired value
116
+ currentTitle : 'test string' ,
117
+ tabs : customTabs , // Replace with the actual (testing) tab data
118
+ currentTabInApp : 'test string' ,
119
+ connectionStatus : false ,
120
+ connectRequested : true ,
121
+ } ,
122
+ } ;
123
123
124
124
const customStore = configureStore ( {
125
125
reducer : {
126
126
main : mainSlice . reducer ,
127
127
} ,
128
128
preloadedState : customInitialState , // Provide custom initial state
129
- middleware : ( getDefaultMiddleware ) =>
130
- getDefaultMiddleware ( { serializableCheck : false } ) ,
129
+ middleware : ( getDefaultMiddleware ) => getDefaultMiddleware ( { serializableCheck : false } ) ,
131
130
} ) ;
132
131
133
- const render = component => rtlRender (
134
- < Provider store = { customStore } >
135
- { component }
136
- </ Provider >
137
- ) ;
132
+ const render = ( component ) => rtlRender ( < Provider store = { customStore } > { component } </ Provider > ) ;
138
133
139
134
const MockRouteDescription = jest . fn ( ) ;
140
135
jest . mock ( '../components/RouteDescription' , ( ) => ( ) => {
@@ -149,7 +144,7 @@ jest.mock('../components/SwitchApp', () => () => {
149
144
} ) ;
150
145
//need to add this mockFunction for setActionView
151
146
//because in actual actioncontainer componenent, it is prop drilled down from maincontainer
152
- //here we set it as a jest.fn()
147
+ //here we set it as a jest.fn()
153
148
//then we pass it into our actionContainer on render
154
149
const setActionViewMock = jest . fn ( ) ;
155
150
const toggleActionContainer = jest . fn ( ) ;
@@ -160,15 +155,21 @@ jest.mock('react-redux', () => ({
160
155
161
156
// const dispatch = jest.fn();
162
157
163
- describe ( 'unit testing for ActionContainer' , ( ) => {
158
+ describe ( 'unit testing for ActionContainer' , ( ) => {
164
159
const useDispatchMock = useDispatch as jest . Mock ; //getting a reference to the mock function you setup during jest.mock configuration on line 18
165
- const dummyDispatch = jest . fn ( ) ; //separate mock function created because we need to explicitly define on line 30 what
166
- useDispatchMock . mockReturnValue ( dummyDispatch ) ; //exactly useDispatchMock returns (which is a jest.fn())
167
- beforeEach ( ( ) => {
168
- render ( < ActionContainer actionView = { true } setActionView = { setActionViewMock } toggleActionContainer = { toggleActionContainer } /> )
160
+ const dummyDispatch = jest . fn ( ) ; //separate mock function created because we need to explicitly define on line 30 what
161
+ useDispatchMock . mockReturnValue ( dummyDispatch ) ; //exactly useDispatchMock returns (which is a jest.fn())
162
+ beforeEach ( ( ) => {
163
+ render (
164
+ < ActionContainer
165
+ actionView = { true }
166
+ setActionView = { setActionViewMock }
167
+ toggleActionContainer = { toggleActionContainer }
168
+ /> ,
169
+ ) ;
169
170
} ) ;
170
171
171
- test ( 'expect top arrow to be rendered' , ( ) => {
172
+ test ( 'expect top arrow to be rendered' , ( ) => {
172
173
// render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
173
174
expect ( screen . getByRole ( 'complementary' ) ) . toBeInTheDocument ( ) ;
174
175
} ) ;
@@ -187,25 +188,34 @@ describe('unit testing for ActionContainer', ()=>{
187
188
fireEvent . click ( screen . getAllByRole ( 'button' ) [ 0 ] ) ;
188
189
expect ( dummyDispatch ) . toHaveBeenCalledTimes ( 1 ) ;
189
190
} ) ;
190
- } )
191
+ } ) ;
191
192
192
193
describe ( 'Integration testing for ActionContainer.tsx' , ( ) => {
193
- test ( 'renders the ActionContainer component' , ( ) => {
194
- //tests that the clearButton is rendered by testing if we can get "Clear"
195
- //need to set actionView to true to correctly render clearbutton
196
- render ( < ActionContainer setActionView = { setActionViewMock } actionView = { true } toggleActionContainer = { toggleActionContainer } /> ) ;
197
- const clearButton = screen . getByText ( 'Clear' ) ; // Use an existing element
198
- expect ( setActionViewMock ) . toHaveBeenCalledWith ( true ) ;
199
- expect ( clearButton ) . toBeInTheDocument ( ) ;
200
- } ) ;
194
+ test ( 'renders the ActionContainer component' , ( ) => {
195
+ //tests that the clearButton is rendered by testing if we can get "Clear"
196
+ //need to set actionView to true to correctly render clearbutton
197
+ render (
198
+ < ActionContainer
199
+ setActionView = { setActionViewMock }
200
+ actionView = { true }
201
+ toggleActionContainer = { toggleActionContainer }
202
+ /> ,
203
+ ) ;
204
+ const clearButton = screen . getByText ( 'Clear' ) ; // Use an existing element
205
+ expect ( setActionViewMock ) . toHaveBeenCalledWith ( true ) ;
206
+ expect ( clearButton ) . toBeInTheDocument ( ) ;
207
+ } ) ;
201
208
202
- test ( 'Slider resets on clear button' , ( ) => {
203
- render ( < ActionContainer actionView = { true } setActionView = { setActionViewMock } toggleActionContainer = { toggleActionContainer } /> )
204
- render ( < TravelContainer snapshotsLength = { 0 } /> )
205
- fireEvent . click ( screen . getAllByRole ( 'button' ) [ 0 ] ) ;
206
- expect ( screen . getByRole ( 'slider' ) ) . toHaveStyle ( 'left: 0' ) ;
207
- } ) ;
209
+ test ( 'Slider resets on clear button' , ( ) => {
210
+ render (
211
+ < ActionContainer
212
+ actionView = { true }
213
+ setActionView = { setActionViewMock }
214
+ toggleActionContainer = { toggleActionContainer }
215
+ /> ,
216
+ ) ;
217
+ render ( < TravelContainer snapshotsLength = { 0 } /> ) ;
218
+ fireEvent . click ( screen . getAllByRole ( 'button' ) [ 0 ] ) ;
219
+ expect ( screen . getByRole ( 'slider' ) ) . toHaveStyle ( 'left: 0' ) ;
220
+ } ) ;
208
221
} ) ;
209
-
210
-
211
-
0 commit comments