1
+ import React from 'react' ;
2
+ import { render as rtlRender , screen , fireEvent } from '@testing-library/react' ;
3
+ import TravelContainer from '../containers/TravelContainer' ;
4
+ import { Provider } from 'react-redux' ;
5
+ import { configureStore } from '@reduxjs/toolkit' ;
6
+ import { mainSlice } from '../RTKslices'
7
+ import { useDispatch } from 'react-redux' ;
8
+ import '@testing-library/jest-dom/extend-expect' ; // needed this to extend the jest-dom assertions (ex toHaveTextContent)
9
+
10
+ const customTabs = {
11
+ 87 : {
12
+ snapshots : [ 1 , 2 , 3 , 4 ] ,
13
+ hierarchy : {
14
+ index : 0 ,
15
+ name : 1 ,
16
+ branch : 0 ,
17
+ stateSnapshot : {
18
+ state : { } ,
19
+ children : [
20
+ {
21
+ state : { test : 'test' } ,
22
+ name : 'App' ,
23
+ componentData : { actualDuration : 3.5 } ,
24
+ } ,
25
+ ] ,
26
+ route : {
27
+ id : 1 ,
28
+ url : 'http://localhost:8080/' ,
29
+ } ,
30
+ } ,
31
+ children : [
32
+ {
33
+ index : 1 ,
34
+ name : 2 ,
35
+ branch : 0 ,
36
+ stateSnapshot : {
37
+ state : { } ,
38
+ children : [
39
+ {
40
+ state : { test : 'test' } ,
41
+ name : 'App' ,
42
+ componentData : { actualDuration : 3.5 } ,
43
+ } ,
44
+ ] ,
45
+ route : {
46
+ id : 2 ,
47
+ url : 'http://localhost:8080/' ,
48
+ } ,
49
+ } ,
50
+ children : [
51
+ {
52
+ index : 2 ,
53
+ name : 3 ,
54
+ branch : 0 ,
55
+ stateSnapshot : {
56
+ state : { } ,
57
+ children : [
58
+ {
59
+ state : { test : 'test' } ,
60
+ name : 'App' ,
61
+ componentData : { actualDuration : 3.5 } ,
62
+ } ,
63
+ ] ,
64
+ route : {
65
+ id : 3 ,
66
+ url : 'http://localhost:8080/' ,
67
+ } ,
68
+ } ,
69
+ children : [
70
+ {
71
+ index : 3 ,
72
+ name : 4 ,
73
+ branch : 0 ,
74
+ stateSnapshot : {
75
+ state : { } ,
76
+ children : [
77
+ {
78
+ state : { test : 'test' } ,
79
+ name : 'App' ,
80
+ componentData : { actualDuration : 3.5 } ,
81
+ } ,
82
+ ] ,
83
+ route : {
84
+ id : 4 ,
85
+ url : 'http://localhost:8080/test/' ,
86
+ } ,
87
+ } ,
88
+ children : [ ] ,
89
+ } ,
90
+ ] ,
91
+ } ,
92
+ ] ,
93
+ } ,
94
+ ] ,
95
+ } ,
96
+ currLocation : {
97
+ index : 0 ,
98
+ name : 1 ,
99
+ branch : 0 ,
100
+ } ,
101
+ sliderIndex : 3 , //updated to 3
102
+ viewIndex : - 1 ,
103
+ } ,
104
+ }
105
+
106
+ const customInitialState = {
107
+ main : {
108
+ port : null ,
109
+ currentTab : 87 , // Update with your desired value
110
+ currentTitle : null ,
111
+ tabs : customTabs , // Replace with the actual (testing) tab data
112
+ currentTabInApp : null ,
113
+ connectionStatus : false ,
114
+ connectRequested : true ,
115
+ } ,
116
+ } ;
117
+
118
+ const customStore = configureStore ( {
119
+ reducer : {
120
+ main : mainSlice . reducer ,
121
+ } ,
122
+ preloadedState : customInitialState , // Provide custom initial state
123
+ middleware : ( getDefaultMiddleware ) =>
124
+ getDefaultMiddleware ( { serializableCheck : false } ) ,
125
+ } ) ;
126
+
127
+ const render = component => rtlRender (
128
+ < Provider store = { customStore } >
129
+ { component }
130
+ </ Provider >
131
+ ) ;
132
+
133
+ const mockSlider = jest . fn ( ) ;
134
+ jest . mock ( '../components/MainSlider' , ( ) => ( props ) => {
135
+ mockSlider ( props ) ;
136
+ return < div > MockSlider</ div > ;
137
+ } ) ;
138
+
139
+ const mockDropDown = jest . fn ( ) ;
140
+ jest . mock ( '../components/Dropdown' , ( ) => ( props ) => {
141
+ mockDropDown ( props ) ;
142
+ return < div > mockDropDown</ div > ;
143
+ } ) ;
144
+
145
+ jest . mock ( 'react-redux' , ( ) => ( {
146
+ ...jest . requireActual ( 'react-redux' ) , // Use the actual react-redux module except for the functions you want to mock
147
+ useDispatch : jest . fn ( ) , // set up a mock function for useDispatch
148
+ } ) ) ;
149
+
150
+ describe ( 'All elements appear in the TravelContainer module' , ( ) => {
151
+ beforeEach ( ( ) => {
152
+ render ( < TravelContainer snapshotsLength = { 0 } /> ) ;
153
+ } ) ;
154
+
155
+ test ( 'first button contains text Play' , ( ) => {
156
+ let buttons = screen . getAllByRole ( 'button' ) ;
157
+ expect ( buttons [ 0 ] ) . toHaveTextContent ( 'Play' ) ;
158
+ } ) ;
159
+
160
+ test ( 'MainSlider exists in document' , ( ) => {
161
+ expect ( screen . getByText ( 'MockSlider' ) ) . toBeInTheDocument ( ) ;
162
+ } ) ;
163
+
164
+ test ( 'Dropdown exists in document' , ( ) => {
165
+ expect ( screen . getByText ( 'mockDropDown' ) ) . toBeInTheDocument ( ) ;
166
+ } ) ;
167
+
168
+ test ( 'Backward button exists in document' , ( ) => {
169
+ let buttons = screen . getAllByRole ( 'button' ) ;
170
+ expect ( buttons [ 1 ] ) . toHaveTextContent ( '<' ) ;
171
+ } ) ;
172
+
173
+ test ( 'Forward button exists in document' , ( ) => {
174
+ let buttons = screen . getAllByRole ( 'button' ) ;
175
+ expect ( buttons [ 2 ] ) . toHaveTextContent ( '>' ) ;
176
+ } ) ;
177
+ } ) ;
178
+
179
+ // describe('Testing backward and forward button', () => {
180
+ // beforeEach(() => {
181
+ // dispatch.mockClear();
182
+ // render(<TravelContainer snapshotsLength={0} />);
183
+ // });
184
+
185
+ // test('Clicking < Button button will trigger button', () => {
186
+ // let buttons = screen.getAllByRole('button');
187
+ // expect(buttons[1]).toHaveTextContent('<');
188
+ // fireEvent.click(buttons[1]);
189
+ // expect(dispatch).toHaveBeenCalledTimes(1);
190
+ // });
191
+
192
+ // test('Clicking > Button button will trigger button', () => {
193
+ // let buttons = screen.getAllByRole('button');
194
+ // expect(buttons[2]).toHaveTextContent('>');
195
+ // fireEvent.click(buttons[2]);
196
+ // expect(dispatch).toHaveBeenCalledTimes(1);
197
+ // });
198
+ // });
199
+
200
+ // describe('Testing play/pause button', () => {
201
+ // beforeEach(() => {
202
+ // dispatch.mockClear();
203
+ // });
204
+
205
+ // test('Play button is rendered', () => {
206
+ // render(<TravelContainer snapshotsLength={0} />);
207
+ // const playButton = screen.getByTestId('play-button-test');
208
+ // expect(playButton).toBeInTheDocument();
209
+ // });
210
+
211
+ // test('Play initially says Play', () => {
212
+ // render(<TravelContainer snapshotsLength={0} />);
213
+ // const playButton = screen.getByTestId('play-button-test');
214
+ // expect(playButton.textContent).toBe('Play');
215
+ // });
216
+
217
+ // test('Clicking Play button will trigger button', () => {
218
+ // render(<TravelContainer snapshotsLength={0} />);
219
+ // const playButton = screen.getByTestId('play-button-test');
220
+ // expect(playButton.textContent).toBe('Play');
221
+ // fireEvent.click(playButton);
222
+ // expect(dispatch).toHaveBeenCalledTimes(1);
223
+ // });
224
+
225
+ // test('Play says Pause when `Playing` is set to true', () => {
226
+ // state.tabs[87].playing = true;
227
+ // render(<TravelContainer snapshotsLength={0} />);
228
+ // const playButton = screen.getByTestId('play-button-test');
229
+ // expect(playButton.textContent).toBe('Pause');
230
+ // });
231
+
232
+ // test('Clicking Pause button will trigger button', () => {
233
+ // render(<TravelContainer snapshotsLength={0} />);
234
+ // const playButton = screen.getByTestId('play-button-test');
235
+ // expect(playButton.textContent).toBe('Pause');
236
+ // fireEvent.click(playButton);
237
+ // expect(dispatch).toHaveBeenCalledTimes(1);
238
+ // state.tabs[87].playing = false;
239
+ // });
240
+ // });
0 commit comments