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 1 commit
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
6 changes: 4 additions & 2 deletions src/hook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export const useRemixForm = <T extends FieldValues>({
dirtyFields,
isDirty,
isSubmitSuccessful,
isSubmitted,
isSubmitting,
isValid,
isValidating,
touchedFields,
submitCount,
Expand All @@ -81,6 +79,10 @@ export const useRemixForm = <T extends FieldValues>({
validKeys,
);

const isValid = !(Object.keys(errors).length > 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be formErrors?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should make a difference but I'm updated it.

const isSubmitted =
data && Object.keys(data).length > 0 && isValid ? true : false;
Copy link
Contributor

@AlemTuzlak AlemTuzlak Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just do Object.keys(data??{}).length > 0 && isValid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also when is isSubmitted set again? After the submission regardless if its valid or not?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is submited is set as soon as the form is submitted. It will stay submitted until reset is called. So this should be isSubmittedSuccessfully instead.


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