Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import '@testing-library/jest-dom';
- import { render, screen, waitFor } from '@testing-library/react';
- import { describe, expect, it, vi } from 'vitest';
- import userEvent from '@testing-library/user-event';
- import { routes } from '../routes';
- import { createMemoryRouter, RouterProvider } from 'react-router-dom';
- import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup';
- describe('Screen Test Suite', async () => {
- // set up variables to be used on each test
- let user: UserEvent;
- beforeEach(() => {
- user = userEvent.setup();
- });
- it('navigates to search results page when search button clicked', async () => {
- // create a new data router for the test
- const router = createMemoryRouter(routes, {
- initialEntries: ['/']
- });
- // render screens
- render(<RouterProvider router={router}/>);
- // mock MovieDB API calls
- vi.mock('../../helpers/getMovieUtils', () => {
- const sampleMovieData = vi.importActual('./assets/searchData.json');
- const sampleMovieDetailsData = vi.importActual('./assets/movieData.json');
- return {
- default: {},
- getMoviesByName: vi.fn().mockResolvedValue(sampleMovieData),
- getMovieDetails: vi.fn().mockResolvedValue(sampleMovieDetailsData),
- };
- });
- // check landing page
- await waitFor(() => screen.getByTestId('featured-search-heading'));
- expect(screen.getByTestId('featured-search-button')).toBeInTheDocument();
- // add search input
- await user.type(screen.getByTestId('featured-search-input'), 'Iron Man');
- expect(screen.getByTestId('featured-search-input')).toHaveValue('Iron Man');
- expect(screen.getByTestId('featured-search-button')).toBeInTheDocument();
- // navigate to search results page
- await user.click(screen.getByTestId('featured-search-button'));
- await waitFor(() => screen.getByTestId('search-results-heading'));
- expect(screen.getByTestId('search-results-heading')).toBeInTheDocument();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement