|
2 | 2 | import React from 'react';
|
3 | 3 | import { render, screen, fireEvent } from '@testing-library/react';
|
4 | 4 | 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'; |
5 | 6 | import Header from './ZoTest';
|
6 | 7 | import MainContainer from '../containers/MainContainer';
|
7 | 8 | import App from '../components/App';
|
8 | 9 | import SwitchAppDropdown from '../components/SwitchApp';
|
9 | 10 | import { useStoreContext } from '../store';
|
10 | 11 | import Dropdown from '../components/Dropdown';
|
11 | 12 |
|
| 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 | + |
12 | 84 | // notes: beforeAll? can render in here before then testing
|
13 | 85 | // after render, can check if the element that has been rendered has props of testing data.
|
14 | 86 | // issues with inrtinsic values, possibly typescript issue
|
|
0 commit comments