|
| 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 | +}); |
0 commit comments