Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes bug with isValid & isSubmitted states #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions src/hook/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("useRemixForm", () => {
isSubmitSuccessful: false,
isSubmitted: false,
isSubmitting: false,
isValid: false,
isValid: true,
isValidating: false,
touchedFields: {},
submitCount: 0,
Expand All @@ -56,7 +56,7 @@ describe("useRemixForm", () => {
onValid,
onInvalid,
},
})
}),
);

act(() => {
Expand All @@ -67,14 +67,51 @@ describe("useRemixForm", () => {
});
});

it("should call onInvalid function when the form is invalid and isValid should be false", async () => {
const onValid = vi.fn();
const onInvalid = vi.fn();
const errors = { name: { message: "Name is required" } };
const values = { name: "" };

const { result } = renderHook(() =>
useRemixForm({
resolver: () => ({ values, errors }),
submitHandlers: {
onValid,
onInvalid,
},
}),
);

act(() => {
result.current.handleSubmit();
});

await waitFor(() => {
expect(result.current.formState).toEqual({
dirtyFields: {},
isDirty: false,
isSubmitSuccessful: false,
isSubmitted: false,
isSubmitting: false,
isValid: false,
isValidating: false,
touchedFields: {},
submitCount: 1,
isLoading: false,
errors,
});
});
});

it("should submit the form data to the server when the form is valid", async () => {
const { result } = renderHook(() =>
useRemixForm({
resolver: () => ({ values: {}, errors: {} }),
submitConfig: {
action: "/submit",
},
})
}),
);

act(() => {
Expand All @@ -99,7 +136,7 @@ describe("useRemixForm", () => {
submitConfig: {
action: "/submit",
},
})
}),
);

act(() => {
Expand All @@ -124,7 +161,7 @@ describe("RemixFormProvider", () => {
submitConfig: {
action: "/submit",
},
})
}),
);
const spy = vi.spyOn(result.current, "handleSubmit");

Expand All @@ -136,7 +173,7 @@ describe("RemixFormProvider", () => {
const { getByTestId } = render(
<RemixFormProvider {...result.current}>
<TestComponent />
</RemixFormProvider>
</RemixFormProvider>,
);

const form = getByTestId("test") as HTMLFormElement;
Expand Down
7 changes: 4 additions & 3 deletions src/hook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ export const useRemixForm = <T extends FieldValues>({
const {
dirtyFields,
isDirty,
isSubmitSuccessful,
isSubmitted,
isSubmitting,
isValid,
isSubmitted,
isValidating,
touchedFields,
submitCount,
Expand All @@ -81,6 +79,9 @@ export const useRemixForm = <T extends FieldValues>({
validKeys,
);

const isValid = Object.keys(formErrors.errors ?? {}).length < 1;
const isSubmitSuccessful = Object.keys(data ?? {}).length > 0 && isValid;

return {
...methods,
handleSubmit: methods.handleSubmit(
Expand Down