Skip to content

Commit c2d6f0b

Browse files
committed
added TravelForwardBackward test file to isolate certain tests
1 parent 12c48fb commit c2d6f0b

File tree

3 files changed

+195
-58
lines changed

3 files changed

+195
-58
lines changed

src/app/__tests__/ActionContainerV2.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ describe('unit testing for ActionContainer', ()=>{
166166
const dummyDispatch = jest.fn(); //separate mock function created because we need to explicitly define on line 30 what
167167
useDispatchMock.mockReturnValue(dummyDispatch);//exactly useDispatchMock returns (which is a jest.fn())
168168
beforeEach(()=>{
169-
render(<ActionContainer actionView = {true} setActionView={setActionViewMock}/>)
169+
render(<ActionContainer actionView={true} setActionView={setActionViewMock}/>)
170170
});
171171

172172
test('expect top arrow to be rendered', ()=>{

src/app/__tests__/TravelContainerV2.test.tsx

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -183,65 +183,37 @@ describe('All elements appear in the TravelContainer module', () => {
183183
expect(forwardButton).toBeInTheDocument();
184184
});
185185
});
186-
187-
// describe('Testing backward and forward button', () => {
188-
// beforeEach(() => {
189-
// render(<TravelContainer snapshotsLength={0} />);
190-
// });
191186

192-
// test('Clicking < Button button will trigger button', () => {
193-
// let buttons = screen.getAllByRole('button');
194-
// expect(buttons[1]).toHaveTextContent('<');
195-
// fireEvent.click(buttons[1]);
196-
// expect(dispatch).toHaveBeenCalledTimes(1);
197-
// });
198-
199-
// test('Clicking > Button button will trigger button', () => {
200-
// let buttons = screen.getAllByRole('button');
201-
// expect(buttons[2]).toHaveTextContent('>');
202-
// fireEvent.click(buttons[2]);
203-
// expect(dispatch).toHaveBeenCalledTimes(1);
204-
// });
205-
// });
206-
207-
// describe('Testing play/pause button', () => {
208-
// beforeEach(() => {
209-
// dispatch.mockClear();
210-
// });
211-
212-
// test('Play button is rendered', () => {
213-
// render(<TravelContainer snapshotsLength={0} />);
214-
// const playButton = screen.getByTestId('play-button-test');
215-
// expect(playButton).toBeInTheDocument();
216-
// });
187+
describe('Testing play/pause button', () => {
188+
const useDispatchMock = useDispatch as jest.Mock; //getting a reference to the mock function you setup during jest.mock configuration on line 154
189+
const dummyDispatch = jest.fn();
190+
useDispatchMock.mockReturnValue(dummyDispatch);
191+
beforeEach(() => {
192+
renderWithTheme(<TravelContainer snapshotsLength={0}/>);
193+
dummyDispatch.mockClear();
194+
});
217195

218-
// test('Play initially says Play', () => {
219-
// render(<TravelContainer snapshotsLength={0} />);
220-
// const playButton = screen.getByTestId('play-button-test');
221-
// expect(playButton.textContent).toBe('Play');
222-
// });
196+
test('Play button is rendered', () => {
197+
const playButton = screen.getByTestId('play-button-test');
198+
expect(playButton).toBeInTheDocument();
199+
});
223200

224-
// test('Clicking Play button will trigger button', () => {
225-
// render(<TravelContainer snapshotsLength={0} />);
226-
// const playButton = screen.getByTestId('play-button-test');
227-
// expect(playButton.textContent).toBe('Play');
228-
// fireEvent.click(playButton);
229-
// expect(dispatch).toHaveBeenCalledTimes(1);
230-
// });
201+
test('Play initially says Play', () => {
202+
const playButton = screen.getByTestId('play-button-test');
203+
expect(playButton.textContent).toBe('Play');
204+
});
231205

232-
// test('Play says Pause when `Playing` is set to true', () => {
233-
// state.tabs[87].playing = true;
234-
// render(<TravelContainer snapshotsLength={0} />);
235-
// const playButton = screen.getByTestId('play-button-test');
236-
// expect(playButton.textContent).toBe('Pause');
237-
// });
206+
test('Clicking Play button will trigger dispatch', () => {
207+
const playButton = screen.getByTestId('play-button-test');
208+
expect(playButton.textContent).toBe('Play');
209+
fireEvent.click(playButton);
210+
expect(dummyDispatch).toHaveBeenCalledTimes(1);
211+
});
238212

239-
// test('Clicking Pause button will trigger button', () => {
240-
// render(<TravelContainer snapshotsLength={0} />);
241-
// const playButton = screen.getByTestId('play-button-test');
242-
// expect(playButton.textContent).toBe('Pause');
243-
// fireEvent.click(playButton);
244-
// expect(dispatch).toHaveBeenCalledTimes(1);
245-
// state.tabs[87].playing = false;
246-
// });
247-
// });
213+
test('Clicking Pause button will trigger dispatch', () => {
214+
customInitialState.main.tabs[87].playing = true;
215+
const playButton = screen.getByTestId('play-button-test');
216+
fireEvent.click(playButton);
217+
expect(dummyDispatch).toHaveBeenCalledTimes(1);
218+
});
219+
});
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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: [0, 1, 2, 3],
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+
playing: false,
104+
},
105+
}
106+
107+
const customInitialState = {
108+
main: {
109+
port: null,
110+
currentTab: 87, // Update with your desired value
111+
currentTitle: null,
112+
tabs: customTabs, // Replace with the actual (testing) tab data
113+
currentTabInApp: null,
114+
connectionStatus: false,
115+
connectRequested: true,
116+
},
117+
};
118+
119+
const customStore = configureStore({
120+
reducer: {
121+
main: mainSlice.reducer,
122+
},
123+
preloadedState: customInitialState, // Provide custom initial state
124+
middleware: (getDefaultMiddleware) =>
125+
getDefaultMiddleware({ serializableCheck: false }),
126+
});
127+
128+
const render = (component) => {
129+
return rtlRender(
130+
<Provider store={customStore}>
131+
{component}
132+
</Provider>
133+
);
134+
};
135+
136+
137+
jest.mock('react-redux', () => ({
138+
...jest.requireActual('react-redux'), // Use the actual react-redux module except for the functions you want to mock
139+
useDispatch: jest.fn(), // set up a mock function for useDispatch
140+
}));
141+
142+
//needed to isolate the testing of the forward and backward buttons as behavior was affected when within the travelContainer file
143+
144+
describe('Testing backward and forward button', () => {
145+
const useDispatchMock = useDispatch as jest.Mock; //getting a reference to the mock function you setup during jest.mock configuration on line 154
146+
const dummyDispatch = jest.fn();
147+
useDispatchMock.mockReturnValue(dummyDispatch);
148+
beforeEach(() => {
149+
render(<TravelContainer snapshotsLength={0} />);
150+
dummyDispatch.mockClear();
151+
});
152+
153+
test('Clicking < Button button will trigger button', () => {
154+
let buttons = screen.getAllByRole('button');
155+
console.log(buttons);
156+
fireEvent.click(buttons[1]);
157+
expect(dummyDispatch).toHaveBeenCalledTimes(1);
158+
});
159+
160+
test('Clicking > Button button will trigger button', () => {
161+
let buttons = screen.getAllByRole('button');
162+
fireEvent.click(buttons[2]);
163+
expect(dummyDispatch).toHaveBeenCalledTimes(1);
164+
});
165+
});

0 commit comments

Comments
 (0)