-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…37) * keep state persistent when collection and fields are unmounted * create the PersistStateOnUnmount component to persist the state on onmounting the component * Persistent state on onmount fixes (#38) * keep state persistent when collection and fields are unmounted * create the PersistStateOnUnmount component to persist the state on onmounting the component * State preseverd on unmount (#39) * keep state persistent when collection and fields are unmounted * create the PersistStateOnUnmount component to persist the state on onmounting the component * update persistent state * remove comment * v3.4.1-alpha.0 * v3.4.1-alpha.1 * PERSISTENTSTATEONUNMOUNT unit test PersistentStateOnUnmount * fix sync validation * 3.4.1-alpha.2 * fix <PersistStateOnUnmount /> * 3.4.1-alpha.3 * fix radio, checkbox validation message on submit Co-authored-by: Simone <[email protected]> Co-authored-by: Antonio <[email protected]> Co-authored-by: Antonio Pangallo <[email protected]>
- Loading branch information
1 parent
1f0d93b
commit 8c571ab
Showing
18 changed files
with
520 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import React from "react"; | ||
import { cleanup, fireEvent, act } from "@testing-library/react"; | ||
import { cleanup, fireEvent, act, render } from "@testing-library/react"; | ||
|
||
import { FormWithValidationAfterMounted } from "./helpers/components/FormWithValidationAfterMounted"; | ||
import Reset from "./helpers/components/Reset"; | ||
import { mountForm } from "./helpers/utils/mountForm"; | ||
|
||
|
@@ -38,6 +39,18 @@ describe("Component => Form (sync validation)", () => { | |
expect(errorLabel.innerHTML).toBe("Mail not Valid"); | ||
}); | ||
|
||
it("should synchronously validate a Form if Fields are added after Form is mounted", () => { | ||
const { getByTestId } = render(<FormWithValidationAfterMounted />); | ||
const addBtn = getByTestId("addBtn"); | ||
|
||
act(() => { | ||
fireEvent.click(addBtn); | ||
}); | ||
|
||
const isValid = getByTestId("isValid"); | ||
expect(isValid.innerHTML).toBe("false"); | ||
}); | ||
|
||
it("should synchronously validate a Form with touched prop true", () => { | ||
const name = "email"; | ||
const value = "[email protected]"; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import React from "react"; | ||
import { cleanup, fireEvent, act, render } from "@testing-library/react"; | ||
|
||
import { | ||
PersistStateOnUnmountHelpers, | ||
initialState | ||
} from "./helpers/components/PersistStateOnUnmountHelpers"; | ||
|
||
const onInit = jest.fn(); | ||
const onChange = jest.fn(); | ||
const onReset = jest.fn(); | ||
const onSubmit = jest.fn(); | ||
|
||
afterEach(cleanup); | ||
|
||
describe("Component => PersistStateOnUnmount", () => { | ||
beforeEach(() => { | ||
onInit.mockClear(); | ||
onChange.mockClear(); | ||
onReset.mockClear(); | ||
onSubmit.mockClear(); | ||
}); | ||
|
||
it("should Collection state persist on unmonuted", () => { | ||
const stateExpected = { | ||
...initialState, | ||
user: { ...initialState.user, lastname: "hero" } | ||
}; | ||
const props = { onInit, onChange }; | ||
const { getByTestId } = render(<PersistStateOnUnmountHelpers {...props} />); | ||
|
||
const toggleCollection = getByTestId("toggleCollection"); | ||
const lastName = getByTestId("lastname"); | ||
expect(onInit).toHaveBeenCalledWith(initialState, true); | ||
|
||
act(() => { | ||
fireEvent.change(lastName, { target: { value: "hero" } }); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.click(toggleCollection); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith(stateExpected, true); | ||
|
||
const togglekeepValue = getByTestId("togglekeepValue"); | ||
|
||
act(() => { | ||
fireEvent.click(togglekeepValue); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith(stateExpected, true); | ||
}); | ||
|
||
it("should Checkbox state persist on unmonuted", () => { | ||
const stateExpected = { ...initialState, other: [, "3"] }; | ||
const props = { onInit, onChange }; | ||
const { getByTestId } = render(<PersistStateOnUnmountHelpers {...props} />); | ||
|
||
const toggleNestedInput = getByTestId("toggleNestedInput"); | ||
const other1 = getByTestId("other1"); | ||
expect(onInit).toHaveBeenCalledWith(initialState, true); | ||
|
||
act(() => { | ||
fireEvent.click(other1); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith(stateExpected, true); | ||
|
||
act(() => { | ||
fireEvent.click(other1); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.click(toggleNestedInput); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith(initialState, true); | ||
}); | ||
|
||
it("should Radio state persist on unmonuted", () => { | ||
const stateExpected = { ...initialState, gender: "M" }; | ||
const props = { onInit, onChange }; | ||
const { getByTestId } = render(<PersistStateOnUnmountHelpers {...props} />); | ||
|
||
const toggleNestedRadio = getByTestId("toggleNestedRadio"); | ||
const genderm = getByTestId("genderm"); | ||
expect(onInit).toHaveBeenCalledWith(initialState, true); | ||
|
||
act(() => { | ||
fireEvent.click(genderm); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.click(toggleNestedRadio); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith(stateExpected, true); | ||
}); | ||
|
||
it("should Select state persist on unmonuted", () => { | ||
const stateExpected = { ...initialState, select: "2" }; | ||
const props = { onInit, onChange }; | ||
const { getByTestId } = render(<PersistStateOnUnmountHelpers {...props} />); | ||
|
||
const toggleSelect = getByTestId("toggleSelect"); | ||
const select = getByTestId("select"); | ||
expect(onInit).toHaveBeenCalledWith(initialState, true); | ||
|
||
act(() => { | ||
fireEvent.change(select, { target: { value: "2" } }); | ||
}); | ||
|
||
act(() => { | ||
fireEvent.click(toggleSelect); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith(stateExpected, true); | ||
}); | ||
}); |
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,21 @@ | ||
import React from "react"; | ||
import { Input, useValidation } from "./../../../src"; | ||
|
||
const required = value => (value ? undefined : "Required"); | ||
|
||
export default function CheckBox({ name = "checkbox", value }) { | ||
const [status, validation] = useValidation([required]); | ||
return ( | ||
<div> | ||
<label>CheckBox: </label> | ||
<Input | ||
name={name} | ||
{...validation} | ||
type="checkbox" | ||
data-testid="checkbox" | ||
value={value} | ||
/> | ||
{status.error && <label data-testid="errorLabel">{status.error}</label>} | ||
</div> | ||
); | ||
} |
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
29 changes: 29 additions & 0 deletions
29
__tests__/helpers/components/FormWithValidationAfterMounted.jsx
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,29 @@ | ||
import React, { useState } from "react"; | ||
import Submit from "./Submit"; | ||
import { Form, Input, useValidation } from "./../../../src"; | ||
|
||
const required = value => | ||
value && value.trim() !== "" ? undefined : "Required"; | ||
|
||
export const FormWithValidationAfterMounted = () => { | ||
const [input, setInput] = useState(() => null); | ||
|
||
const [status, inputValidation] = useValidation([required]); | ||
|
||
const onClick = () => { | ||
setInput(<Input key="1" type="text" name="text" {...inputValidation} />); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Form> | ||
{input} | ||
<Submit /> | ||
</Form> | ||
<button onClick={onClick} type="button" data-testid="addBtn"> | ||
addBtn | ||
</button> | ||
{status.error && <label data-testid="errorLabel">{status.error}</label>} | ||
</> | ||
); | ||
}; |
Oops, something went wrong.