简体   繁体   English

开玩笑的多步表单测试 - 是否可以将测试拆分为多个测试但使用一个渲染?

[英]jest multistep form testing - is it possible to split the test into multiple tests but with one render?

I have a multi-step Sign Up form in my React Native application and I'm testing it with jest and RNTL.我的 React Native 应用程序中有一个多步骤注册表单,我正在使用 jest 和 RNTL 对其进行测试。 It works fine, but I just don't like how it looks.它工作正常,但我只是不喜欢它的外观。

Now it's just one big test function which renders the screen in the beginning and then goes through the registration process.现在它只是一个大test function,它在开始时渲染屏幕,然后完成注册过程。 It would be nice to render the screen once and then test each step in an individual test function while keeping the progress, but I can't find the proper way to do it.渲染一次屏幕然后在保持进度的情况下在单独test function 中测试每个步骤会很好,但我找不到正确的方法来做到这一点。

My approach was to set up a describe function in which i call render on the root component of the form, and then test each step individually, like how i would do in plain javascript:我的方法是设置一个describe function,其中我在表单的根组件上调用render ,然后单独test每个步骤,就像我在普通 javascript 中的做法一样:

describe('Registration process', () => {
  // initial screen render:
  const view = render(renderWithProviders(<RegisterNewUser />));

  // first step testing:
  test('Name Step', async () => {
    // trying to access the rendered view:
    const nameTitle = view.getByText('register.name.title');
    expect(nameTitle).toBeOnTheScreen();
    const nameInput = await view.findByPlaceholderText(
      'register.name.fieldPlaceholder',
    );
    await act(async () => {
      await fireEvent.changeText(nameInput, 'George');
    });
    fireEvent.press(view.getByTestId(nextStepTestId));
    expect(nameTitle).not.toBeOnTheScreen();
  });
  // ...tests for next steps
});

But as soon as jest reaches the first line in my first test, I get this error:但是一旦 jest 在我的第一次测试中到达第一行,我就会收到这个错误:

Unable to find node on an unmounted component.

      111 |   const view = render(renderWithProviders(<RegisterNewUser />));
      112 |   test('Name Step', async () => {
    > 113 |     const nameTitle = view.getByText('register.name.title');</sub>

It's better if you render the component in each test() so you can adjust props and another context for each test or you can render your component in beforeEach if you want the same props and configuration.如果你在每个test()中渲染组件会更好,这样你就可以为每个测试调整道具和另一个上下文,或者如果你想要相同的道具和配置,你可以在 beforeEach 中渲染你的组件。

I usually adopt the following way in the test file.我通常在测试文件中采用以下方式。

describe('Registration process', ()=> {
     const componentBuilder = (route) => (
          <Providers>
            <TestScreen route={route} />
          </Providers>
      );
    
test('Name Step', async () => {
    const wrapper = render(componentBuilder());
    ...rest of code.
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以等待组件渲染? 反应测试库/Jest - Is it possible to wait for a component to render? React Testing Library/Jest 使用refs和Form进行jest测试失败 - jest test fails with refs and Form 使用 React Native 测试库和 Jest 测试 setTimeout - Test setTimeout with React Native Testing Library and Jest React Native Jest:未定义测试(测试) - React Native Jest : Test is not defined (Testing) 如何在每次测试的基础上设置平台以进行玩笑测试? - How to set the platform on a per test basis for jest tests? 用玩笑测试 expo 应用程序,但由于实验性语法而导致测试崩溃 - Testing expo app with jest, but test crashes because of experimental syntax 是否可以同时使用带有多个预设的 Jest? - Is it possible to use Jest with multiple presets at the same time? 玩笑快照测试:如何在玩笑测试结果中忽略部分快照文件 - jest snapshot testing: how to ignore part of the snapshot file in jest test results 使用 jest 和 react 测试库进行单元和集成测试的 TRPC 测试包装器 - 客户端模拟 - TRPC Test Wrapper for unit and integration testing using jest and react testing library - client mock 测试运行完成后,Jest 没有退出一秒 - Jest did not exit one second after the test run has completed
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM