[英]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.