-
Notifications
You must be signed in to change notification settings - Fork 150
/
index.test.tsx
79 lines (68 loc) · 2.49 KB
/
index.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as React from "react";
import { render, screen, act } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import Textarea from "..";
import SPACINGS_AFTER from "../../common/getSpacingToken/consts";
describe("Textarea", () => {
it("should have expected DOM output", async () => {
const name = "name";
const label = "Label";
const value = "value";
const placeholder = "placeholder";
const dataTest = "test";
const maxLength = 200;
const fullHeight = true;
const onChange = jest.fn();
const spaceAfter = SPACINGS_AFTER.NORMAL;
render(
<Textarea
name={name}
label={label}
value={value}
rows={4}
fullHeight={fullHeight}
placeholder={placeholder}
maxLength={maxLength}
help={<div>Something useful.</div>}
onChange={onChange}
dataTest={dataTest}
spaceAfter={spaceAfter}
/>,
);
const textarea = screen.getByRole("textbox");
expect(screen.getByTestId(dataTest)).toBeInTheDocument();
expect(screen.getByDisplayValue(value)).toBeInTheDocument();
expect(screen.getByPlaceholderText(placeholder)).toBeInTheDocument();
userEvent.tab();
expect(screen.getByText("Something useful.")).toBeInTheDocument();
expect(textarea).toHaveAttribute("maxlength", maxLength.toString());
expect(textarea).toHaveAttribute("rows", "4");
expect(textarea).toHaveAttribute("name", name);
expect(textarea).not.toBeInvalid();
expect(textarea.parentElement).toHaveStyle({ marginBottom: "12px" });
expect(textarea).toHaveStyle({ padding: "12px" });
userEvent.type(textarea, "kek");
expect(onChange).toHaveBeenCalled();
});
it("should have focus and blur", () => {
const onFocus = jest.fn();
const onBlur = jest.fn();
render(<Textarea onFocus={onFocus} onBlur={onBlur} />);
userEvent.tab();
expect(onFocus).toHaveBeenCalled();
userEvent.tab();
expect(onBlur).toHaveBeenCalled();
});
it("should have error", async () => {
render(<Textarea error="error" size="small" />);
const textarea = screen.getByRole("textbox");
userEvent.tab();
expect(screen.getByText("error")).toBeInTheDocument();
expect(textarea).toHaveStyle({ padding: "8px 12px" });
expect(textarea).toBeInvalid();
expect(textarea).toHaveDescription("error");
// Needs to flush async `floating-ui` hooks
// https://github.com/floating-ui/floating-ui/issues/1520
await act(async () => {});
});
});