Skip to content

Commit

Permalink
test: add unit test for submitAttemptsCount behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
s8n11c committed Dec 29, 2024
1 parent 54fcc21 commit e62415d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
17 changes: 17 additions & 0 deletions packages/core/src/useForm/useForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,23 @@ describe('form submit', () => {
await flush();
expect(formData.get('foo')).toBe('bar');
});

test('can compute the submit counts, attempts and resets', async () => {
const { submitAttemptsCount, handleSubmit, reset } = await renderSetup(() => {
return useForm({ initialValues: { foo: 'bar' } });
});

const cb = vi.fn();
const onSubmit = handleSubmit(v => cb(v.toObject()));

expect(submitAttemptsCount.value).toBe(0);
onSubmit(new Event('submit'));
expect(submitAttemptsCount.value).toBe(1);
onSubmit(new Event('submit'));
expect(submitAttemptsCount.value).toBe(2);
await reset();
expect(submitAttemptsCount.value).toBe(0);
});
});

describe('form dirty state', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/useForm/useFormActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ export function useFormActions<TForm extends FormObject = FormObject, TOutput ex

form.revertValues();
form.revertTouched();
submitAttemptsCount.value = 0;

if (state?.revalidate ?? true) {
await validate();
return;
}

form.clearErrors();

submitAttemptsCount.value = 0;

return Promise.resolve();
}

Expand Down

0 comments on commit e62415d

Please sign in to comment.