-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dep-wrapper-update): update depdency wrapper logic and testing (#…
…691) * working through logic for array fields * add logic to correct setValue * test file layout * Update depedencyWrapper.test.tsx * Update depedencyWrapper.test.tsx * Update dependencyWrapper.tsx * Update SlotField.tsx
- Loading branch information
Showing
4 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
react-app/src/components/RHF/tests/depedencyWrapper.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { describe, test, expect } from "vitest"; | ||
import { render } from "@testing-library/react"; | ||
import { useForm, FormProvider } from "react-hook-form"; | ||
import { DependencyWrapper } from "../dependencyWrapper"; | ||
import { PropsWithChildren } from "react"; | ||
import { DependencyRule } from "shared-types"; | ||
|
||
const TestComp = ({ | ||
name, | ||
dependency, | ||
parentValue, | ||
changeMethod, | ||
children, | ||
}: PropsWithChildren<{ | ||
name: string; | ||
dependency: DependencyRule; | ||
parentValue: string[]; | ||
changeMethod: (values: string[]) => void; | ||
}>) => { | ||
const methods = useForm({ | ||
defaultValues: { | ||
[name]: parentValue, | ||
field1: "test", | ||
field2: "", | ||
}, | ||
}); | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<DependencyWrapper | ||
name={name} | ||
dependency={dependency} | ||
parentValue={parentValue} | ||
changeMethod={changeMethod} | ||
> | ||
{children} | ||
</DependencyWrapper> | ||
</FormProvider> | ||
); | ||
}; | ||
|
||
describe("DependencyWrapper Tests", () => { | ||
test("test show effect", () => { | ||
const dependency: DependencyRule = { | ||
conditions: [ | ||
{ type: "expectedValue", expectedValue: "test", name: "field1" }, | ||
], | ||
effect: { type: "show" }, | ||
}; | ||
const { getByText } = render( | ||
<TestComp | ||
name="testField" | ||
dependency={dependency} | ||
parentValue={["testField"]} | ||
changeMethod={(value) => { | ||
console.log(value); | ||
}} | ||
> | ||
<div>Child Component</div> | ||
</TestComp>, | ||
); | ||
|
||
expect(getByText("Child Component")).toBeTruthy(); | ||
}); | ||
|
||
test("test hide effect", () => { | ||
const dependency: DependencyRule = { | ||
conditions: [ | ||
{ type: "expectedValue", expectedValue: "test", name: "field1" }, | ||
], | ||
effect: { type: "hide" }, | ||
}; | ||
const { queryByText } = render( | ||
<TestComp | ||
name="testField" | ||
dependency={dependency} | ||
parentValue={["testField"]} | ||
changeMethod={(value) => { | ||
console.log(value); | ||
}} | ||
> | ||
<div>Child Component</div> | ||
</TestComp>, | ||
); | ||
|
||
expect(queryByText("Child Component")).toBeNull(); | ||
}); | ||
|
||
test("test set value effect", () => { | ||
const dependency: DependencyRule = { | ||
conditions: [ | ||
{ type: "expectedValue", expectedValue: "test", name: "field1" }, | ||
], | ||
effect: { type: "setValue", fieldName: "field2", newValue: "newValue" }, | ||
}; | ||
|
||
let methods: any; | ||
|
||
const TestCompWithMethods = ( | ||
props: PropsWithChildren<{ | ||
name: string; | ||
dependency: DependencyRule; | ||
parentValue: string[]; | ||
changeMethod: (values: string[]) => void; | ||
}>, | ||
) => { | ||
methods = useForm({ | ||
defaultValues: { | ||
[props.name]: props.parentValue, | ||
field1: "test", | ||
field2: "", | ||
}, | ||
}); | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<DependencyWrapper {...props}>{props.children}</DependencyWrapper> | ||
</FormProvider> | ||
); | ||
}; | ||
|
||
render( | ||
<TestCompWithMethods | ||
name="testField" | ||
dependency={dependency} | ||
parentValue={["testField"]} | ||
changeMethod={(value) => { | ||
console.log(value); | ||
}} | ||
> | ||
<div>Child Component</div> | ||
</TestCompWithMethods>, | ||
); | ||
|
||
expect(methods.getValues("field2")).toBe("newValue"); | ||
}); | ||
}); |