• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

안녕하세요! 로딩 상태 테스트에 관련된 질문입니다.

23.09.19 17:14 작성 23.09.19 17:14 수정 조회수 210

0

제 나름대로 강의를 응용해서 테스트를 짜보려고 강의 시점과 다른 userEvent 버전을 쓰고 있기는 하는데요. 로딩 상태를 테스트할때 마치 로딩 상태를 건너뛰고 바로 리스폰스를 받은 듯이 작동하고 있습니다.... 혹시 userEvent 버전이 달라서 동작이 달라진걸까요? 강의와 코드 내용이 조금 달라진 것에 대한 질문이라 죄송합니다만 아무리 찾아도 이유를 모르곘네요..ㅠㅠㅠ

import { screen, render } from '@testing-library/react';
import App from './App';
import userEvent from '@testing-library/user-event';

describe('MainPage', () => {
  it('should change page when click buttons', async () => {
    const user = userEvent.setup();
    render(<App />);

    const americaInput = await screen.findByRole('spinbutton', {
      name: /america/i,
    });

    await user.clear(americaInput);
    await user.type(americaInput, '2');

    const englandInput = await screen.findByRole('spinbutton', {
      name: /england/i,
    });

    await user.clear(englandInput);
    await user.type(englandInput, '1');

    const dinnerCheckbox = await screen.findByText(/dinner/i);

    await user.click(dinnerCheckbox);

    const orderButton = screen.getByRole('button', { name: '주문하기' });

    await user.click(orderButton);

    const summeryHeading = screen.getByRole('heading', { name: '주문 확인' });
    expect(summeryHeading).toBeInTheDocument();

    const productHeading = screen.getByRole('heading', {
      name: '여행상품: 3000',
    });

    expect(productHeading).toBeInTheDocument();

    const americaLi = screen.getByText('2 America');
    const englandLi = screen.getByText('1 England');

    expect(americaLi).toBeInTheDocument();
    expect(englandLi).toBeInTheDocument();

    const optionHeading = screen.getByRole('heading', { name: '옵션: 500' });
    const dinnerLi = screen.getByText('Dinner');

    expect(optionHeading).toBeInTheDocument();
    expect(dinnerLi).toBeInTheDocument();

    const confirmCheckbox = screen.getByRole('checkbox');
    const submitButton = screen.getByRole('button');

    await user.click(confirmCheckbox);
    await user.click(submitButton);

    const loading = screen.getByText(/loading/i);
    expect(loading).toBeInTheDocument();

    const completeDiv = await screen.findByRole('heading', {
      name: '주문이 성공했습니다!',
    });

    const subHeadingDiv = await screen.findByRole('heading', {
      name: '지금까지 모든 주문',
    });

    const loadingAfterResponse = screen.queryByText(/loading/i);

    expect(completeDiv).toBeInTheDocument();
    expect(subHeadingDiv).toBeInTheDocument();
    expect(loadingAfterResponse).not.toBeInTheDocument();

    const priceDiv = await screen.findByText('3500', { exact: false });

    expect(priceDiv).toBeInTheDocument();

    const backBtn = await screen.findByRole('button');

    await user.click(backBtn);

    const newAmericaInput = await screen.findByRole('spinbutton', {
      name: /america/i,
    });

    expect(newAmericaInput).toBeInTheDocument();
    expect(newAmericaInput).toHaveValue(0);
  });
});

스크린샷 2023-09-19 오후 5.12.10.png

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.09.21

안녕하세요, 인프런 AI 인턴이에요. userEvent 버전이 다르면 동작이 달라질 수 있습니다. 버전에 따라 API나 기능이 업데이트되거나 변경될 수 있으므로, 버전의 차이점을 확인해 보는 것이 좋아요. 코드 내용과 강의 시점이 조금 달라진 것이 원인이 될 수도 있습니다.

이럴 경우, 테스트를 실행하기 전에 문제가 생길 가능성이 있는 코드들을 확인해 보시는 것이 좋아요. 예를 들어, userEvent.setup()으로 설정한 부분이나, 각각의 입력 요소들을 가져온 후에 어떻게 상호작용하는지 등을 확인해 보세요.

이후에도 문제가 해결되지 않는다면, 강의에 대한 질문이라면 강의를 제작한 강사나 강의 커뮤니티에서 도움을 받을 수 있을 거에요. 프로그래밍 관련 질문이라면 해당 기능을 사용하고 있는 다른 개발자들이나 온라인 커뮤니티에서 도움을 얻을 수도 있어요.