Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Add failing tests for removing FormControl children
Browse files Browse the repository at this point in the history
  • Loading branch information
justinanastos committed Mar 2, 2021
1 parent 4cc01e7 commit 5510fc4
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/FormControl/FormControl.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { FormLabel } from "../FormLabel";
import { FormHelperText } from "../FormHelperText";
import { Input } from "../Input";
import { FormErrorMessage } from "../FormErrorMessage";
import { FormEndAdornment } from "../FormEndAdornment";
import { FormStartAdornment } from "../FormStartAdornment";
import { FormDescription } from "../FormDescription";

test("when passed a label, renders it", () => {
render(
Expand Down Expand Up @@ -88,24 +91,6 @@ test("when passed `<FormErrorMessage />`, renders error and svg", () => {
expect(container.querySelector("svg")).toBeInTheDocument();
});

test("when passed `<FormErrorMessage />` that is removed, removes the error message", () => {
const Component: React.FC<{ errorText?: string }> = ({ errorText }) => (
<FormControl id="test">
<Input />
{errorText && <FormErrorMessage>{errorText}</FormErrorMessage>}
</FormControl>
);

const { container, rerender } = render(<Component errorText="error text" />);

expect(screen.getByText("error text")).toBeInTheDocument();
expect(container.querySelector("svg")).toBeInTheDocument();

rerender(<Component />);

expect(screen.queryByText("error text")).not.toBeInTheDocument();
});

test("when passed `<HelperText>` witout `showIcon` prop, renders no svg", () => {
const { container } = render(
<FormControl id="test">
Expand Down Expand Up @@ -143,3 +128,29 @@ test("when not passed `autoFocus` prop, should not have focus after mounting", (

expect(formField).not.toHaveFocus();
});

test.each([
["FormDescription", FormDescription],
["FormEndAdornment", FormEndAdornment],
["FormErrorMessage", FormErrorMessage],
["FormHelperText", FormHelperText],
["FormLabel", FormLabel],
["FormStartAdornment", FormStartAdornment],
])("%s can be removed on subsequent renders", (_, Component) => {
const { rerender } = render(
<FormControl id="test">
<Component>text</Component>
<Input />
</FormControl>,
);

expect(screen.getByText("text")).toBeInTheDocument();

rerender(
<FormControl id="test">
<Input />
</FormControl>,
);

expect(screen.queryByText("text")).not.toBeInTheDocument();
});

0 comments on commit 5510fc4

Please sign in to comment.