@@ -7,7 +7,7 @@ import { Provider } from 'react-redux';
7
7
import { configureStore } from '@reduxjs/toolkit' ;
8
8
import { store } from '../RTKstore' ;
9
9
import { mainSlice } from '../RTKslices'
10
-
10
+ import { useDispatch } from 'react-redux' ;
11
11
//Note for testing:
12
12
//typically, jest.mock is commonly used in unit testing to isolate the code under test.
13
13
//In contrast, when performing integration testing of components with a real Redux store,
@@ -137,12 +137,59 @@ const render = component => rtlRender(
137
137
</ Provider >
138
138
) ;
139
139
140
+ const MockRouteDescription = jest . fn ( ) ;
141
+ jest . mock ( '../components/RouteDescription' , ( ) => ( ) => {
142
+ MockRouteDescription ( ) ;
143
+ return < div > MockRouteDescription</ div > ;
144
+ } ) ;
145
+
146
+ const MockSwitchApp = jest . fn ( ) ;
147
+ jest . mock ( '../components/SwitchApp' , ( ) => ( ) => {
148
+ MockSwitchApp ( ) ;
149
+ return < div > MockSwitchApp</ div > ;
150
+ } ) ;
140
151
//need to add this mockFunction for setActionView
141
152
//because in actual actioncontainer componenent, it is prop drilled down from maincontainer
142
153
//here we set it as a jest.fn()
143
154
//then we pass it into our actionContainer on render
144
155
const setActionViewMock = jest . fn ( ) ;
145
156
157
+ jest . mock ( 'react-redux' , ( ) => ( {
158
+ ...jest . requireActual ( 'react-redux' ) , // Use the actual react-redux module except for the functions you want to mock
159
+ useDispatch : jest . fn ( ) , // set up a mock function for useDispatch
160
+ } ) ) ;
161
+
162
+ // const dispatch = jest.fn();
163
+
164
+ describe ( 'unit testing for ActionContainer' , ( ) => {
165
+ const useDispatchMock = useDispatch as jest . Mock ; //getting a reference to the mock function you setup during jest.mock configuration on line 18
166
+ const dummyDispatch = jest . fn ( ) ; //separate mock function created because we need to explicitly define on line 30 what
167
+ useDispatchMock . mockReturnValue ( dummyDispatch ) ; //exactly useDispatchMock returns (which is a jest.fn())
168
+ beforeEach ( ( ) => {
169
+ render ( < ActionContainer actionView = { true } setActionView = { setActionViewMock } /> )
170
+ } ) ;
171
+
172
+ test ( 'expect top arrow to be rendered' , ( ) => {
173
+ // render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
174
+ expect ( screen . getByRole ( 'complementary' ) ) . toBeInTheDocument ( ) ;
175
+ } ) ;
176
+
177
+ test ( 'Expect RouteDescription to be rendered' , ( ) => {
178
+ // render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
179
+ expect ( screen . getAllByText ( 'MockRouteDescription' ) ) . toHaveLength ( 2 ) ;
180
+ } ) ;
181
+
182
+ test ( 'Expect SwitchApp to be rendered' , ( ) => {
183
+ // render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
184
+ expect ( screen . getByText ( 'MockSwitchApp' ) ) . toBeInTheDocument ( ) ;
185
+ } ) ;
186
+
187
+ test ( 'Click works on clear button' , ( ) => {
188
+ fireEvent . click ( screen . getAllByRole ( 'button' ) [ 0 ] ) ;
189
+ expect ( dummyDispatch ) . toHaveBeenCalledTimes ( 1 ) ;
190
+ } ) ;
191
+ } )
192
+
146
193
describe ( 'Integration testing for ActionContainer.tsx' , ( ) => {
147
194
test ( 'renders the ActionContainer component' , ( ) => {
148
195
//tests that the clearButton is rendered by testing if we can get "Clear"
@@ -152,6 +199,13 @@ describe('Integration testing for ActionContainer.tsx', () => {
152
199
expect ( setActionViewMock ) . toHaveBeenCalledWith ( true ) ;
153
200
expect ( clearButton ) . toBeInTheDocument ( ) ;
154
201
} ) ;
202
+
203
+ test ( 'Slider resets on clear button' , ( ) => {
204
+ render ( < ActionContainer actionView = { true } setActionView = { setActionViewMock } /> )
205
+ render ( < TravelContainer snapshotsLength = { 0 } /> )
206
+ fireEvent . click ( screen . getAllByRole ( 'button' ) [ 0 ] ) ;
207
+ expect ( screen . getByRole ( 'slider' ) ) . toHaveStyle ( 'left: 0' ) ;
208
+ } ) ;
155
209
} ) ;
156
210
157
211
0 commit comments