SolidJSS
SolidJSโ€ข3y agoโ€ข
1 reply
Mobs

Testing state update after api call`

Hello I am trying to test a register user component and I want to show an error message if I get an error from the rest api.
Getting the element fails even after state is updated.

This is to submit the user register form
  async function handleSubmit(e) {
    e.preventDefault();
    (...)
    setLoading(true);
    try {
      const resp = await UserService.register(formData);
      (...)
    } catch (err) {
      setLoading(false);
      (...)
      setRegistrationError(true);
    }
  }


Where its supposed to show:
<Show when={registrationError()}>
  <AuthError message={errorMsg()} />
</Show>

Test:
  test("Register error", async () => {
    render(() => <Register mocki18n={/*@once*/ i18n} />);
     (...)
     const sign_up_btn = screen.getByRole("button");
     fireEvent.click(sign_up_btn);
     expect(
          // element not found
          screen.getByText(RegExp(`User with ${email} already exists!`))
     ).toBeInTheDocument();
  });

This works as expected when tested manually.
Was this page helpful?