Skip to content

Commit 4c4841c

Browse files
committed
added testing to dropdown, travelcontainer, edited travelcontainer component
1 parent 55aef96 commit 4c4841c

File tree

3 files changed

+170
-1
lines changed

3 files changed

+170
-1
lines changed

src/app/__tests__zo/Dropdown.test.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// purpose of this file is to start testing front end/components using RTL
2+
import React from 'react';
3+
import { render, screen, fireEvent } from '@testing-library/react';
4+
import '@testing-library/jest-dom/extend-expect'; // needed this to extend the jest-dom assertions (ex toHaveTextContent)
5+
6+
import Dropdown from '../components/Dropdown';
7+
8+
// interface DropdownProps {
9+
// selectedSpeed: { value: number; label: string };
10+
// speeds: { value: number; label: string }[];
11+
// setSpeed: () => void;
12+
// }
13+
14+
describe('Dropdown test', () => {
15+
const speeds = [
16+
{ value: 1, label: 'test1' },
17+
{ value: 2, label: 'test2' },
18+
{ value: 3, label: 'test3' },
19+
];
20+
// make a hook call into a jest mock
21+
const setSpeed = jest.fn();
22+
23+
// test 1: test to see if it renders the label correctly
24+
test('renders Dropdown with correct info', () => {
25+
// find a specific speed from array. could be testing each? one for now.
26+
const currSpeed = speeds[0];
27+
// you pass in what is expected from TS, passed into Dropdown NOT the return statement.
28+
// in this case, the dropdownprops interface.
29+
// assign it to a container which is the document i guess?
30+
const { container } = render(
31+
<Dropdown speeds={speeds} setSpeed={setSpeed} selectedSpeed={currSpeed} />,
32+
);
33+
// then we can query the container like a normal dom, with query selector
34+
expect(container.querySelector('.react-select__single-value')).toHaveTextContent('test1');
35+
// want to test for speeds but not showing up?
36+
// screen.debug is only what is rendered, so no functions
37+
// test to see if setSpeed is called
38+
//
39+
// screen.debug();
40+
});
41+
42+
// test 2: test to see if functions are called properly on changes.
43+
// test('setSpeed mock function is called correctly when speed is changed', () => {
44+
// const currSpeed = speeds[0];
45+
// const { container } = render(
46+
// <Dropdown speeds={speeds} setSpeed={setSpeed} selectedSpeed={currSpeed} />,
47+
// );
48+
// // grab the dropdown
49+
// const dropdown = container.querySelector('.react-select__input');
50+
// // console.log(dropdown);
51+
// // select new speed from speeds arr
52+
// fireEvent.mouseDown(dropdown);
53+
// // const newSpeed = speeds[1];
54+
// const newSpeed = container.querySelector('[data-value="2"]');
55+
// // console.log('new speed', newSpeed);
56+
// fireEvent.click(newSpeed);
57+
// // check to see that setspeeds has been called with the new speed.
58+
// expect(setSpeed).toHaveBeenCalled();
59+
// expect(setSpeed).toHaveBeenCalledWith(speeds[1]);
60+
61+
// // check to see if new speed is rendered on the dom or something
62+
// });
63+
});
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/// in progress
2+
import React from 'react';
3+
import { render, screen, fireEvent } from '@testing-library/react';
4+
import userEvent from '@testing-library/user-event';
5+
import '@testing-library/jest-dom/extend-expect'; // needed this to extend the jest-dom assertions (ex toHaveTextContent)
6+
import TravelContainer from '../containers/TravelContainer';
7+
import MainSlider from '../components/MainSlider';
8+
// import {mockMainSlider} from '../components/__mocks__/MainSlider'
9+
import { useStoreContext } from '../store';
10+
import { TravelContainerProps } from '../components/FrontendTypes';
11+
import MainContainer from '../containers/MainContainer';
12+
import Dropdown from '../components/Dropdown';
13+
14+
const state = {
15+
tabs: {
16+
87: {
17+
snapshots: [0, 1, 2, 3], // because snapshot index starts at 0
18+
sliderIndex: 3,
19+
playing: false,
20+
},
21+
},
22+
currentTab: 87,
23+
};
24+
25+
// mock play?
26+
const play = jest.fn(() => console.log('legit'));
27+
// play.mockImplementation(() => console.log('legit'));
28+
29+
const dispatch = jest.fn();
30+
useStoreContext.mockImplementation(() => [state, dispatch]);
31+
32+
// jest.mock mocks a module - all exports of this module are basically set to jest.fn()
33+
jest.mock('../store');
34+
35+
// mocking the Mainslider component to return div with MockSlider in it
36+
const mockSlider = jest.fn();
37+
jest.mock('../components/MainSlider', () => (props) => {
38+
mockSlider(props);
39+
return <div>MockSlider</div>;
40+
});
41+
const mockDropDown = jest.fn();
42+
43+
// mocking the dropdown component to return div with mockDropDown in it
44+
jest.mock('../components/Dropdown', () => (props) => {
45+
mockDropDown(props);
46+
return <div>mockDropDown</div>;
47+
});
48+
49+
describe('testing the TravelContainer module', () => {
50+
// nested blocks.
51+
// high level block, test that the play button exists, slider gets renders, dropdown gets rendered.
52+
// nested: play button. when paused vs not. starts at a condition, then pressed is changed into another condition.
53+
54+
// before each, clear usestorcontext and dispatch
55+
beforeEach(() => {
56+
useStoreContext.mockClear(); // need to think about if mockClear is the correct item here
57+
dispatch.mockClear();
58+
render(<TravelContainer snapshotsLength={0} />);
59+
});
60+
61+
// screen.debug();
62+
63+
test('first button contains text Play', () => {
64+
let buttons = screen.getAllByRole('button');
65+
// console.log('this is buttons', buttons);
66+
expect(buttons[0]).toHaveTextContent('Play');
67+
// screen.debug();
68+
});
69+
70+
test('MainSlider exists in document', () => {
71+
expect(screen.getByText('MockSlider')).toBeInTheDocument();
72+
});
73+
test('Dropdown exists in document', () => {
74+
expect(screen.getByText('mockDropDown')).toBeInTheDocument();
75+
});
76+
});
77+
78+
describe('Testing play/pause button', () => {
79+
test('Play button is rendered', () => {
80+
render(<TravelContainer snapshotsLength={0} />);
81+
const playButton = screen.getByTestId('play-button-test');
82+
expect(playButton).toBeInTheDocument();
83+
});
84+
test('Play initially says Play', () => {
85+
render(<TravelContainer snapshotsLength={0} />);
86+
const playButton = screen.getByTestId('play-button-test');
87+
expect(playButton.textContent).toBe('Play');
88+
});
89+
test('Play says Pause when `Playing` is set to False', () => {
90+
state.tabs[87].playing = true;
91+
render(<TravelContainer snapshotsLength={0} />);
92+
const playButton = screen.getByTestId('play-button-test');
93+
expect(playButton.textContent).toBe('Pause');
94+
// manually reset to default test state?
95+
state.tabs[87].playing = false;
96+
});
97+
// make an onclick that changes state?
98+
// test('clicking button changes state', () => {
99+
// render(<TravelContainer snapshotsLength={0} />);
100+
// const playButton = screen.getByTestId('play-button-test');
101+
// fireEvent.click(playButton);
102+
// // render(<TravelContainer snapshotsLength={0} />);
103+
// expect(state.tabs[87].playing).toBe(true);
104+
// });
105+
});

src/app/containers/TravelContainer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const speeds: {
2020
{ value: 2000, label: '0.5x' },
2121
{ value: 1000, label: '1.0x' },
2222
{ value: 500, label: '2.0x' },
23-
];
23+
];
2424

2525
// start slider movement
2626
function play(
@@ -62,6 +62,7 @@ function TravelContainer(props: TravelContainerProps): JSX.Element {
6262
<button
6363
className='play-button'
6464
type='button'
65+
data-testid='play-button-test'
6566
onClick={() => play(selectedSpeed.value, playing, dispatch, snapshotsLength, sliderIndex)}
6667
>
6768
{playing ? 'Pause' : 'Play'}

0 commit comments

Comments
 (0)