S
SolidJS16mo ago
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);
}
}
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>
<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();
});
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.
1 Reply
Mobs
MobsOP16mo ago
I downgraded from [email protected] to [email protected] and everything works
Want results from more Discord servers?
Add your server