Advertisement
Thenlie

Untitled

Dec 18th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import '@testing-library/jest-dom';
  2. import { render, screen, waitFor } from '@testing-library/react';
  3. import { describe, expect, it, vi } from 'vitest';
  4. import userEvent from '@testing-library/user-event';
  5. import { routes } from '../routes';
  6. import { createMemoryRouter, RouterProvider } from 'react-router-dom';
  7. import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup';
  8.  
  9. describe('Screen Test Suite', async () => {
  10.     // set up variables to be used on each test
  11.     let user: UserEvent;
  12.     beforeEach(() => {
  13.         user = userEvent.setup();
  14.     });
  15.     it('navigates to search results page when search button clicked', async () => {
  16.         // create a new data router for the test
  17.         const router = createMemoryRouter(routes, {
  18.             initialEntries: ['/']
  19.         });
  20.         // render screens
  21.         render(<RouterProvider router={router}/>);
  22.         // mock MovieDB API calls
  23.         vi.mock('../../helpers/getMovieUtils', () => {
  24.             const sampleMovieData = vi.importActual('./assets/searchData.json');
  25.             const sampleMovieDetailsData = vi.importActual('./assets/movieData.json');
  26.             return {
  27.                 default: {},
  28.                 getMoviesByName: vi.fn().mockResolvedValue(sampleMovieData),
  29.                 getMovieDetails: vi.fn().mockResolvedValue(sampleMovieDetailsData),
  30.             };
  31.         });
  32.         // check landing page
  33.         await waitFor(() => screen.getByTestId('featured-search-heading'));
  34.         expect(screen.getByTestId('featured-search-button')).toBeInTheDocument();
  35.         // add search input
  36.         await user.type(screen.getByTestId('featured-search-input'), 'Iron Man');
  37.         expect(screen.getByTestId('featured-search-input')).toHaveValue('Iron Man');
  38.         expect(screen.getByTestId('featured-search-button')).toBeInTheDocument();
  39.         // navigate to search results page
  40.         await user.click(screen.getByTestId('featured-search-button'));
  41.         await waitFor(() => screen.getByTestId('search-results-heading'));
  42.         expect(screen.getByTestId('search-results-heading')).toBeInTheDocument();
  43.     });
  44. });
Tags: help Code Vitest
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement