Skip to content

Commit

Permalink
test & callback wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Dec 27, 2023
1 parent 3c4a5cc commit 35cb94a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/atoms/list-atom/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./listBuilder";
export * from "./listAtom";
export * from "./listBuilder";
14 changes: 13 additions & 1 deletion src/atoms/list-atom/listAtom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
FieldAtomConfig,
FormAtom,
Validate,
ValidateOn,
ValidateStatus,
formAtom,
Expand Down Expand Up @@ -206,6 +207,17 @@ export function listAtom<
},
);

const validateCallback: Validate<Value> = (state) => {
// TODO: aggregate errors
const formLists = state.get(_formListAtom).map((formAtom) => {
const form = state.get(formAtom);

// state.set(form.validate, state.event);
});

return config.validate?.(state);
};

const listAtoms = {
name: nameAtom,
value: valueAtom,
Expand All @@ -219,7 +231,7 @@ export function listAtom<
ref: refAtom,
buildItem,
_validateCount: validateCountAtom,
_validateCallback: config.validate,
_validateCallback: validateCallback,
/**
* List private atoms
*/
Expand Down
17 changes: 17 additions & 0 deletions src/fields/list-field/listField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,21 @@ describe("listField()", () => {
expect(screen.queryByTestId("input-iban")).not.toBeInTheDocument();
});
});

describe("nested validation", () => {
it.only("can't be submitted with invalid item's field", async () => {
const field = listField({
value: [undefined], // empty value for number
builder: (value) => numberField({ value }),
});

const form = formAtom({ field });

const { result: submit } = renderHook(() => useFormSubmit(form));
const onSubmit = vi.fn();
await act(async () => submit.current(onSubmit)());

expect(onSubmit).not.toHaveBeenCalled();
});
});
});

0 comments on commit 35cb94a

Please sign in to comment.