@@ -5,136 +5,131 @@ import ActionContainer from '../containers/ActionContainer';
55import TravelContainer from '../containers/TravelContainer' ;
66import { Provider } from 'react-redux' ;
77import { configureStore } from '@reduxjs/toolkit' ;
8- import { mainSlice } from '../slices/mainSlice'
8+ import { mainSlice } from '../slices/mainSlice' ;
99import { useDispatch } from 'react-redux' ;
1010//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.
1414//The decision to use jest.mock depends on the type of testing (unit or integration) and your specific testing goals.
1515
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 : { } ,
3744 children : [
3845 {
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 : { } ,
5663 children : [
5764 {
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 : { } ,
7582 children : [
7683 {
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 } ,
9587 } ,
9688 ] ,
89+ route : {
90+ id : 4 ,
91+ url : 'http://localhost:8080/test/' ,
92+ } ,
9793 } ,
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+ ] ,
12199 } ,
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+ } ;
123123
124124const customStore = configureStore ( {
125125 reducer : {
126126 main : mainSlice . reducer ,
127127 } ,
128128 preloadedState : customInitialState , // Provide custom initial state
129- middleware : ( getDefaultMiddleware ) =>
130- getDefaultMiddleware ( { serializableCheck : false } ) ,
129+ middleware : ( getDefaultMiddleware ) => getDefaultMiddleware ( { serializableCheck : false } ) ,
131130} ) ;
132131
133- const render = component => rtlRender (
134- < Provider store = { customStore } >
135- { component }
136- </ Provider >
137- ) ;
132+ const render = ( component ) => rtlRender ( < Provider store = { customStore } > { component } </ Provider > ) ;
138133
139134const MockRouteDescription = jest . fn ( ) ;
140135jest . mock ( '../components/RouteDescription' , ( ) => ( ) => {
@@ -149,7 +144,7 @@ jest.mock('../components/SwitchApp', () => () => {
149144} ) ;
150145//need to add this mockFunction for setActionView
151146//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()
153148//then we pass it into our actionContainer on render
154149const setActionViewMock = jest . fn ( ) ;
155150const toggleActionContainer = jest . fn ( ) ;
@@ -160,15 +155,21 @@ jest.mock('react-redux', () => ({
160155
161156// const dispatch = jest.fn();
162157
163- describe ( 'unit testing for ActionContainer' , ( ) => {
158+ describe ( 'unit testing for ActionContainer' , ( ) => {
164159 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+ ) ;
169170 } ) ;
170171
171- test ( 'expect top arrow to be rendered' , ( ) => {
172+ test ( 'expect top arrow to be rendered' , ( ) => {
172173 // render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
173174 expect ( screen . getByRole ( 'complementary' ) ) . toBeInTheDocument ( ) ;
174175 } ) ;
@@ -187,25 +188,34 @@ describe('unit testing for ActionContainer', ()=>{
187188 fireEvent . click ( screen . getAllByRole ( 'button' ) [ 0 ] ) ;
188189 expect ( dummyDispatch ) . toHaveBeenCalledTimes ( 1 ) ;
189190 } ) ;
190- } )
191+ } ) ;
191192
192193describe ( '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+ } ) ;
201208
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+ } ) ;
208221} ) ;
209-
210-
211-
0 commit comments