Skip to content

Commit

Permalink
tests code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed Jan 7, 2021
1 parent 8970a22 commit 2f0a9f8
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 82 deletions.
10 changes: 3 additions & 7 deletions __tests__/Collection.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from "react";
import { act } from "react-dom/test-utils";
import { fireEvent, waitFor, cleanup, act } from "@testing-library/react";

import { render, fireEvent, waitFor, cleanup } from "@testing-library/react";

import { Form, Input, Collection } from "./../src";
import { Input, Collection } from "./../src";

import { CollectionDynamicCart } from "./helpers/components/CollectionDynamicField";
import CollectionDynamicAdded from "./helpers/components/CollectionDynamicAdded";
Expand All @@ -28,9 +26,7 @@ import CollectionObjectNested, {
import Reset from "./helpers/components/Reset";
import Submit from "./helpers/components/Submit";
import AgeRange from "./helpers/components/AgeRange";

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { mountForm } from "./helpers/utils/mountForm";

const dataTestid = "username";
const name = "user";
Expand Down
11 changes: 2 additions & 9 deletions __tests__/CollectionsStrictMode.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import { render, cleanup, fireEvent } from "@testing-library/react";
import { cleanup, fireEvent } from "@testing-library/react";

import Form from "./../src";
import {
CollectionDynamicField,
CollectionNestedDynamicField,
Expand All @@ -13,13 +12,7 @@ import {
import { CollectionObjectNestedRadios } from "./helpers/components/CollectionObjectNested";

import Reset from "./helpers/components/Reset";

const mountForm = ({ props = {}, children } = {}) =>
render(
<React.StrictMode>
<Form {...props}>{children}</Form>
</React.StrictMode>
);
import { mountForm } from "./helpers/utils/mountForm";

const onInit = jest.fn(state => state);
const onChange = jest.fn(state => state);
Expand Down
6 changes: 2 additions & 4 deletions __tests__/Form.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from "react";
import { render, cleanup, fireEvent, waitFor } from "@testing-library/react";

import Form, { Input } from "./../src";

import { SimpleFormTestSumbission } from "./helpers/components/SimpleFormTestSumbission";
import { CollectionDynamicCart } from "./helpers/components/CollectionDynamicField";
import SimpleForm from "./helpers/components/SimpleForm";
Expand All @@ -12,9 +10,9 @@ import {
ComplexFormInitValueAsProps,
initialState as initialStateComplexForm
} from "./helpers/components/ComplexForm";
import { mountForm } from "./helpers/utils/mountForm";

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { Input } from "./../src";

const dataTestid = "email";
const typeInput = "text";
Expand Down
21 changes: 11 additions & 10 deletions __tests__/Input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import {
act
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";

import { Form, Input } from "./../src";

import { Input } from "./../src";
import InputAsync from "./helpers/components/InputAsync";
import InputSyncValidation from "./helpers/components/InputSyncValidation";
import Submit from "./helpers/components/Submit";
import Reset from "./helpers/components/Reset";
import { SimpleFormDynamicField } from "./helpers/components/SimpleForm";

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { mountForm } from "./helpers/utils/mountForm";

const onInit = jest.fn();
const onChange = jest.fn();
Expand Down Expand Up @@ -431,8 +427,10 @@ describe("Component => Input", () => {
const reset = getByTestId("reset");
const asyncinput = getByTestId("asyncinput");

asyncinput.focus();
asyncinput.blur();
act(() => {
asyncinput.focus();
asyncinput.blur();
});

const asyncStart = await waitFor(() => getByTestId("asyncStart"));
expect(asyncStart).toBeDefined();
Expand All @@ -442,8 +440,11 @@ describe("Component => Input", () => {
expect(asyncError.textContent).toBe("Error");

fireEvent.change(asyncinput, { target: { value: "1234" } });
asyncinput.focus();
asyncinput.blur();

act(() => {
asyncinput.focus();
asyncinput.blur();
});

expect(asyncinput.value).toBe("1234");

Expand Down
9 changes: 3 additions & 6 deletions __tests__/Select.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React from "react";
import { act } from "react-dom/test-utils";
import { render, fireEvent, cleanup } from "@testing-library/react";
import { fireEvent, cleanup, act } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

import Reset from "./helpers/components/Reset";
import SelectSyncValidation from "./helpers/components/SelectSyncValidation";
import { mountForm } from "./helpers/utils/mountForm";

import Form, { Select } from "./../src";

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { Select } from "./../src";

const dataTestid = "select";
const name = "select";
Expand Down
9 changes: 3 additions & 6 deletions __tests__/TextArea.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from "react";
import { render, fireEvent, cleanup } from "@testing-library/react";

import Form, { TextArea } from "./../src";

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { fireEvent, cleanup } from "@testing-library/react";
import { mountForm } from "./helpers/utils/mountForm";
import { TextArea } from "./../src";

const dataTestid = "TextArea";
const name = "TextArea";
Expand Down
12 changes: 12 additions & 0 deletions __tests__/helpers/components/CustomField.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react";
import { useField, withIndex } from "./../../../src";

export const CustomField = withIndex(({ name, value, ...restAttr }) => {
const props = useField({ type: "custom", name, value });
const onChange = () => props.onChange({ target: { value: "5" } });
return (
<button type="button" onClick={onChange} {...restAttr}>
Change Value
</button>
);
});
9 changes: 9 additions & 0 deletions __tests__/helpers/components/InputCustom.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";
import { useField, withIndex } from "./../../../src";

export const InputCustom = withIndex(
({ type, name, value, index, ...restAttr }) => {
const props = useField({ type, name, value, index });
return <input {...restAttr} {...props}></input>;
}
);
13 changes: 13 additions & 0 deletions __tests__/helpers/components/InputCustomNoAutoIndex.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react";
import { useField } from "./../../../src";

export const InputCustomNoAutoIndex = ({
type,
name,
value,
index,
...restAttr
}) => {
const props = useField({ type, name, value, index });
return <input {...restAttr} {...props}></input>;
};
10 changes: 10 additions & 0 deletions __tests__/helpers/utils/mountForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { render } from "@testing-library/react";
import { Form } from "./../../../src";

export const mountForm = ({ props = {}, children } = {}) =>
render(
<React.StrictMode>
<Form {...props}>{children}</Form>
</React.StrictMode>
);
8 changes: 3 additions & 5 deletions __tests__/hooks/useCollection.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from "react";
import { render, fireEvent, cleanup } from "@testing-library/react";
import Form, { Collection } from "./../../src";
import { fireEvent, cleanup } from "@testing-library/react";
import { Collection } from "./../../src";

import CollectionWithHooks from "./../helpers/components/CollectionWithHooks";

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { mountForm } from "./../helpers/utils/mountForm";

const onInit = jest.fn();
const onChange = jest.fn();
Expand Down
31 changes: 6 additions & 25 deletions __tests__/hooks/useField.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
import React from "react";
import { render, fireEvent, cleanup, act } from "@testing-library/react";
import { Form, Collection, useField, withIndex } from "./../../src";

const InputCustom = withIndex(({ type, name, value, index, ...restAttr }) => {
const props = useField({ type, name, value, index });
return <input {...restAttr} {...props}></input>;
});

const CustomField = withIndex(({ name, value, ...restAttr }) => {
const props = useField({ type: "custom", name, value });
const onChange = () => props.onChange({ target: { value: "5" } });
return (
<button type="button" onClick={onChange} {...restAttr}>
Change Value
</button>
);
});

const InputCustomNoAutoIndex = ({ type, name, value, index, ...restAttr }) => {
const props = useField({ type, name, value, index });
return <input {...restAttr} {...props}></input>;
};

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { fireEvent, cleanup, act } from "@testing-library/react";
import { Collection } from "./../../src";
import { mountForm } from "./../helpers/utils/mountForm";
import { InputCustom } from "./../helpers/components/InputCustom";
import { CustomField } from "./../helpers/components/CustomField";
import { InputCustomNoAutoIndex } from "./../helpers/components/InputCustomNoAutoIndex";

const onInit = jest.fn();
const onChange = jest.fn();
Expand Down
14 changes: 4 additions & 10 deletions __tests__/hooks/useSelector.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import React from "react";
import { render, fireEvent, cleanup, act } from "@testing-library/react";
import { fireEvent, cleanup, act } from "@testing-library/react";
import { CmpWithSelectorToggle } from "./../helpers/components/CmpWithSelectorToggle";
import { Form, Input, withIndex, Collection, useField } from "./../../src";
import { Input, Collection } from "./../../src";
import Reset from "./../helpers/components/Reset";

const InputCustom = withIndex(({ type, name, value, index, ...restAttr }) => {
const props = useField({ type, name, value, index });
return <input {...restAttr} {...props}></input>;
});

const mountForm = ({ props = {}, children } = {}) =>
render(<Form {...props}>{children}</Form>);
import { mountForm } from "./../helpers/utils/mountForm";
import { InputCustom } from "./../helpers/components/InputCustom";

const onReset = jest.fn();
const onChange = jest.fn();
Expand Down

0 comments on commit 2f0a9f8

Please sign in to comment.