Skip to content

Commit 36678c4

Browse files
committed
added travelContainer test and MainSlider test
1 parent fe70345 commit 36678c4

File tree

3 files changed

+308
-1
lines changed

3 files changed

+308
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"test": "jest --verbose --coverage",
1010
"test-backend": "jest --verbose --coverage src/backend",
1111
"test-frontend": "jest --verbose --coverage src/app",
12-
"test-newfront": "jest --verbose --coverage src/app/__tests__new",
12+
"test-jn": "jest --verbose --coverage src/app/__tests__jn",
1313
"test-zo": "jest --verbose --coverage src/app/__tests__zo",
1414
"test-on": "./node_modules/.bin/jest $1",
1515
"docker-test-lint": "eslint --ext .js --ext .jsx src",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react'
2+
import {render, screen, fireEvent} from '@testing-library/react'
3+
import userEvent from '@testing-library/user-event'
4+
import '@testing-library/jest-dom/extend-expect' // needed this to extend the jest-dom assertions (ex toHaveTextContent)
5+
import MainSlider from '../components/MainSlider';
6+
import { useStoreContext } from '../store';
7+
8+
9+
10+
jest.mock('../store');
11+
12+
describe('Unit testing for MainSlider.jsx', () => {
13+
14+
const props = {
15+
snapshotsLength: 1,
16+
};
17+
18+
const state = {
19+
tabs: {
20+
100: {
21+
sliderIndex: 1,
22+
},
23+
},
24+
currentTab: 100,
25+
};
26+
27+
const dispatch = jest.fn();
28+
useStoreContext.mockImplementation(() => [state, dispatch]);
29+
30+
31+
describe('When user only has one snapshot to view', () => {
32+
33+
test('Component should have min, max, value with correct values to indicate slider position for correct tab', () => {
34+
render(<MainSlider {...props}/>)
35+
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuemin','0')
36+
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuemax', '0')
37+
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuenow','0')
38+
});
39+
40+
});
41+
42+
describe('When there are multiple snapshots and we are looking in between', () => {
43+
const props = {
44+
snapshotsLength: 3,
45+
};
46+
47+
const state = {
48+
tabs: {
49+
100: {
50+
sliderIndex: 1,
51+
}
52+
},
53+
currentTab: 100,
54+
};
55+
56+
// currently not working :( likely needs to correctly handle understanding what tab it should currently be at
57+
test('Component should have min, max, value with correct values to indicate slider position when there are multiple snapshots', () => {
58+
render(<MainSlider {...props}/>)
59+
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuemax','2')
60+
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuemin','0')
61+
expect(screen.getByRole('slider')).toHaveAttribute('aria-valuenow','1')
62+
});
63+
})
64+
})
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
import React from 'react'
2+
import {render, screen, fireEvent} from '@testing-library/react'
3+
import userEvent from '@testing-library/user-event'
4+
import '@testing-library/jest-dom/extend-expect' // needed this to extend the jest-dom assertions (ex toHaveTextContent)
5+
import TravelContainer from '../containers/TravelContainer'
6+
import { useStoreContext } from '../store';
7+
import { TravelContainerProps } from '../components/FrontendTypes';
8+
9+
10+
// const state = {
11+
// tabs: {
12+
// 87: {
13+
// snapshots: [0, 1, 2, 3], // because snapshot index starts at 0
14+
// sliderIndex: 3,
15+
// playing: false,
16+
// },
17+
// },
18+
// currentTab: 87,
19+
// };
20+
21+
22+
// jest.fn() mocks a function - Create a new mock that can be used in place of `dispatch`.
23+
const dispatch = jest.fn();
24+
const play = jest.fn();
25+
26+
27+
28+
// jest.mock mocks a module - all exports of this module are basically set to jest.fn()
29+
jest.mock('../store');
30+
31+
// useStoreContext is a mocked function that accepts a function that should be used as the implmentaton of the mock
32+
// useStoreContext is being mocked and should use to function in arg -> return the state item and dispatch (mocked function)
33+
34+
describe('when user loads a page with only one snapshot included and user has not played',()=>{
35+
36+
const state = {
37+
tabs: {
38+
87: {
39+
snapshots: [0,1,2], // because snapshot index starts at 0
40+
sliderIndex: 1,
41+
playing: false,
42+
},
43+
},
44+
currentTab: 87,
45+
};
46+
47+
useStoreContext.mockImplementation(() => [state, dispatch]);
48+
49+
beforeEach(() => {
50+
useStoreContext.mockClear(); // need to think about if mockClear is the correct item here
51+
dispatch.mockClear();
52+
53+
})
54+
render(<TravelContainer snapshotsLength={0}/>);
55+
// @ts-ignore
56+
let slider1 = screen.getByRole<HTMLInputElement>('slider',{ value: {now:1, min:0, max:0}})
57+
58+
// @ts-ignore
59+
let slider0 = screen.getByRole<HTMLInputElement>('slider',{ value: {now:0, min:0, max:0}})
60+
let slider = screen.getByRole('slider')
61+
62+
screen.debug(screen.getAllByRole('slider'))
63+
64+
test('first button contains text Play',()=>{
65+
let buttons = screen.getAllByRole('button')
66+
expect(buttons[0]).toHaveTextContent('Play')
67+
})
68+
69+
test('slider icon is at the very left',()=>{
70+
render(<TravelContainer snapshotsLength={0}/>);
71+
// @ts-ignore
72+
// let slider = screen.getByRole<HTMLInputElement>('slider',{ value: {now:1, min:0, max:0}})
73+
74+
expect(slider1).not.toBeInTheDocument()
75+
expect(slider0).not.toBeInTheDocument()
76+
})
77+
78+
})
79+
80+
81+
// describe('when user as on hte second one',()=>{
82+
83+
// const state = {
84+
// tabs: {
85+
// 87: {
86+
// snapshots: [0,1,2], // because snapshot index starts at 0
87+
// sliderIndex: 1,
88+
// playing: false,
89+
// },
90+
// },
91+
// currentTab: 87,
92+
// };
93+
94+
// useStoreContext.mockImplementation(() => [state, dispatch]);
95+
96+
// beforeEach(() => {
97+
// useStoreContext.mockClear(); // need to think about if mockClear is the correct item here
98+
// dispatch.mockClear();
99+
100+
// })
101+
102+
// test('first button contains text Play',()=>{
103+
// render(<TravelContainer snapshotsLength={0}/>);
104+
// let buttons = screen.getAllByRole('button')
105+
// expect(buttons[0]).toHaveTextContent('Play')
106+
// })
107+
108+
// test('slider icon is at the very left',()=>{
109+
// render(<TravelContainer snapshotsLength={0}/>);
110+
// // @ts-ignore
111+
// let slider = screen.getByRole<HTMLInputElement>('slider',{ value: {now:0, min:0, max:0}}) as HTMLInputElement
112+
// expect(slider).toBeInTheDocument()
113+
// })
114+
115+
// })
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+
154+
// describe("Play and carrot buttons exist", () => {
155+
156+
// beforeEach(() => {
157+
// useStoreContext.mockClear(); // need to think about if mockClear is the correct item here
158+
// dispatch.mockClear();
159+
160+
// })
161+
162+
// render(<TravelContainer snapshotsLength={2} />);
163+
// // screen.debug()
164+
// let buttons = screen.getAllByRole('button')
165+
166+
// test('First should say Play', () => {
167+
// expect(buttons[0]).toHaveTextContent('Play')
168+
// });
169+
170+
// test('Second button should say <', () => {
171+
// expect(buttons[1]).toHaveTextContent('<')
172+
// });
173+
174+
// test('Third should say >', () => {
175+
// expect(buttons[2]).toHaveTextContent('>')
176+
// });
177+
178+
// })
179+
180+
181+
// / trying to make click happen
182+
183+
describe("Click on unlock", () => {
184+
185+
beforeEach(() => {
186+
useStoreContext.mockClear(); // need to think about if mockClear is the correct item here
187+
dispatch.mockClear();
188+
189+
})
190+
191+
// const user = userEvent.setup()
192+
render(<TravelContainer snapshotsLength={2} />);
193+
194+
// const { container } = render(<TravelContainer snapshotsLength={2} />);
195+
// screen.debug()
196+
197+
test('Click on < button', () => {
198+
// expect(dispatch).toHaveBeenCalledTimes(1)
199+
// user.click(screen.getAllByRole('button')[1])
200+
// expect(dispatch).toHaveBeenCalledTimes(1)
201+
// user.click(screen.getAllByRole('button')[1])
202+
fireEvent.click(screen.getAllByRole('button')[1])
203+
expect(dispatch).toHaveBeenCalledTimes(1)
204+
fireEvent.click(screen.getAllByRole('button')[1])
205+
206+
expect(dispatch).toHaveBeenCalledTimes(2)
207+
208+
// fireEvent.click(screen.getAllByRole('button')[0])
209+
// expect(screen.getAllByRole('button')[0]).toHaveTextContent('Pause')
210+
// screen.debug(screen.getAllByRole('button')[0])
211+
212+
});
213+
214+
})
215+
216+
217+
// desired test
218+
/*
219+
play button:
220+
- if playing = false, it shows play
221+
- if playing = true, it shows pauce
222+
- when clicked, it toggles behavior
223+
- slider reflects current state location
224+
225+
226+
227+
228+
if there's only one snapshot
229+
playing false
230+
TravelContainer.tsx:55 snapshotsLength 1
231+
TravelContainer.tsx:56 sliderIndex 0
232+
233+
current behavior is that play turns to pause, but returns back to play
234+
slider isnt able to go anywhere, stays on left side
235+
left and right dont do anything
236+
237+
238+
239+
240+
241+
242+
243+
*/

0 commit comments

Comments
 (0)