-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabling sync and async validation at form level (#29)
Co-authored-by: Antonio <[email protected]>
- Loading branch information
1 parent
fc5179c
commit c22fda5
Showing
52 changed files
with
4,261 additions
and
2,065 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 |
---|---|---|
|
@@ -33,5 +33,10 @@ | |
"browser": true, | ||
"node": true, | ||
"jest": true | ||
}, | ||
"settings": { | ||
"react": { | ||
"version": "latest" | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -17,3 +17,5 @@ deploy: | |
github_token: $GITHUB_TOKEN | ||
on: | ||
branch: master | ||
after_success: | ||
- npm run coveralls |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import React from "react"; | ||
import { fireEvent, cleanup, act } from "@testing-library/react"; | ||
|
||
import { CollectionArrayIndexHandledManually } from "./helpers/components/CollectionArrayIndexHandledManually"; | ||
|
||
import Reset from "./helpers/components/Reset"; | ||
import Submit from "./helpers/components/Submit"; | ||
import { mountForm } from "./helpers/utils/mountForm"; | ||
|
||
const onInit = jest.fn(); | ||
const onChange = jest.fn(); | ||
const onReset = jest.fn(); | ||
const onSubmit = jest.fn(); | ||
|
||
afterEach(cleanup); | ||
|
||
describe("Component => Collection (Array with indexes handled manually)", () => { | ||
beforeEach(() => { | ||
onInit.mockClear(); | ||
onChange.mockClear(); | ||
onReset.mockClear(); | ||
onSubmit.mockClear(); | ||
}); | ||
|
||
it("should correctly render an array Collection with indexes handled manually", () => { | ||
const props = { onInit, onChange, onReset, onSubmit }; | ||
const myself = { current: null }; | ||
|
||
const children = [ | ||
<CollectionArrayIndexHandledManually key="1" ref={myself} />, | ||
<Reset key="2" />, | ||
<Submit key="3" /> | ||
]; | ||
const { getByTestId } = mountForm({ props, children }); | ||
const addInput = getByTestId("addInput"); | ||
const addCollection = getByTestId("addCollection"); | ||
const removeCollection = getByTestId("removeCollection"); | ||
|
||
const removeInput = getByTestId("removeInput"); | ||
const reset = getByTestId("reset"); | ||
const submit = getByTestId("submit"); | ||
|
||
expect(onInit).toHaveBeenCalledWith({}, true); | ||
|
||
for (let i = 1; i <= 10; i++) { | ||
act(() => { | ||
fireEvent.click(addInput); | ||
}); | ||
} | ||
|
||
let stateExpected = myself.current.getInnerState(); | ||
expect(onChange).toHaveBeenCalledWith({ indexManual: stateExpected }, true); | ||
|
||
for (let i = 1; i <= 5; i++) { | ||
act(() => { | ||
fireEvent.click(removeInput); | ||
}); | ||
} | ||
|
||
stateExpected = myself.current.getInnerState(); | ||
expect(onChange).toHaveBeenCalledWith({ indexManual: stateExpected }, true); | ||
|
||
const newExpected = []; | ||
stateExpected[0].forEach(val => { | ||
const input = getByTestId(`input_${val}`); | ||
const newValue = Math.random() * 10000; | ||
newExpected.push(`${newValue}`); | ||
act(() => { | ||
fireEvent.change(input, { target: { value: `${newValue}` } }); | ||
}); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith({ indexManual: [newExpected] }, true); | ||
|
||
act(() => { | ||
fireEvent.click(submit); | ||
}); | ||
|
||
expect(onSubmit).toHaveBeenCalledWith({ indexManual: [newExpected] }, true); | ||
|
||
act(() => { | ||
fireEvent.click(reset); | ||
}); | ||
|
||
expect(onReset).toHaveBeenCalledWith({ indexManual: stateExpected }, true); | ||
|
||
for (let i = 1; i <= 10; i++) { | ||
act(() => { | ||
fireEvent.click(addCollection); | ||
}); | ||
} | ||
|
||
stateExpected = myself.current.getInnerState(); | ||
expect(onChange).toHaveBeenCalledWith({ indexManual: stateExpected }, true); | ||
|
||
const newCollectionExpected = []; | ||
stateExpected[1].forEach(val => { | ||
const input = getByTestId(`text_${val[0]}`); | ||
const newValue = Math.random() * 10000; | ||
newCollectionExpected.push([`${newValue}`]); | ||
act(() => { | ||
fireEvent.change(input, { target: { value: `${newValue}` } }); | ||
}); | ||
}); | ||
|
||
const nextStateExpected = [stateExpected[0], newCollectionExpected]; | ||
expect(onChange).toHaveBeenCalledWith( | ||
{ indexManual: nextStateExpected }, | ||
true | ||
); | ||
|
||
stateExpected = myself.current.getInnerState(); | ||
act(() => { | ||
fireEvent.click(reset); | ||
}); | ||
|
||
expect(onReset).toHaveBeenCalledWith({ indexManual: stateExpected }, true); | ||
|
||
for (let i = 1; i <= 5; i++) { | ||
act(() => { | ||
fireEvent.click(removeCollection); | ||
}); | ||
} | ||
|
||
stateExpected = myself.current.getInnerState(); | ||
expect(onChange).toHaveBeenCalledWith({ indexManual: stateExpected }, true); | ||
}); | ||
}); |
Oops, something went wrong.