Skip to content

Commit c4b4f4d

Browse files
committed
jest/package config with messing around
1 parent 7e44532 commit c4b4f4d

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/app/__tests__zo/ZoTest.test.tsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,85 @@
22
import React from 'react';
33
import { render, screen, fireEvent } from '@testing-library/react';
44
import '@testing-library/jest-dom/extend-expect'; // needed this to extend the jest-dom assertions (ex toHaveTextContent)
5+
import userEvent from '@testing-library/user-event';
56
import Header from './ZoTest';
67
import MainContainer from '../containers/MainContainer';
78
import App from '../components/App';
89
import SwitchAppDropdown from '../components/SwitchApp';
910
import { useStoreContext } from '../store';
1011
import Dropdown from '../components/Dropdown';
1112

13+
interface DropdownProps {
14+
selectedSpeed: { value: number; label: string };
15+
speeds: { value: number; label: string }[];
16+
setSpeed: () => void;
17+
}
18+
19+
describe('Dropdown test', () => {
20+
const speeds = [
21+
{ value: 1, label: 'test1' },
22+
{ value: 2, label: 'test2' },
23+
{ value: 3, label: 'test3' },
24+
];
25+
// make a hook call into a jest mock
26+
const setSpeed = jest.fn();
27+
28+
// test 1: test to see if it renders the label correctly
29+
test('renders Dropdown with correct info', () => {
30+
// find a specific speed from array. could be testing each? one for now.
31+
const currSpeed = speeds[0];
32+
// you pass in what is expected from TS, passed into Dropdown NOT the return statement.
33+
// in this case, the dropdownprops interface.
34+
// assign it to a container which is the document i guess?
35+
const { container } = render(
36+
<Dropdown speeds={speeds} setSpeed={setSpeed} selectedSpeed={currSpeed} />,
37+
);
38+
// then we can query the container like a normal dom, with query selector
39+
expect(container.querySelector('.react-select__single-value')).toHaveTextContent('test1');
40+
// want to test for speeds but not showing up?
41+
// screen.debug is only what is rendered, so no functions
42+
// test to see if setSpeed is called
43+
//
44+
screen.debug();
45+
});
46+
47+
// test 2: test to see if functions are called properly on changes.
48+
test('setSpeed mock function is called correctly when speed is changed', () => {
49+
const currSpeed = speeds[0];
50+
const { container } = render(
51+
<Dropdown speeds={speeds} setSpeed={setSpeed} selectedSpeed={currSpeed} />,
52+
);
53+
// grab the dropdown
54+
const dropdown = container.querySelector('.react-select__input');
55+
// console.log(dropdown);
56+
// select new speed from speeds arr
57+
fireEvent.mouseDown(dropdown);
58+
// const newSpeed = speeds[1];
59+
const newSpeed = container.querySelector('[data-value="2"]');
60+
// console.log('new speed', newSpeed);
61+
fireEvent.click(newSpeed);
62+
// check to see that setspeeds has been called with the new speed.
63+
expect(setSpeed).toHaveBeenCalled();
64+
expect(setSpeed).toHaveBeenCalledWith(speeds[1]);
65+
66+
// check to see if new speed is rendered on the dom or something
67+
});
68+
});
69+
70+
// const Dropdown = (props: DropdownProps): JSX.Element => {
71+
// const { speeds, setSpeed, selectedSpeed } = props;
72+
// return (
73+
// <Select
74+
// className='react-select-container'
75+
// classNamePrefix='react-select'
76+
// value={selectedSpeed}
77+
// onChange={setSpeed}
78+
// options={speeds}
79+
// menuPlacement='top'
80+
// />
81+
// );
82+
// };
83+
1284
// notes: beforeAll? can render in here before then testing
1385
// after render, can check if the element that has been rendered has props of testing data.
1486
// issues with inrtinsic values, possibly typescript issue

0 commit comments

Comments
 (0)