-
Notifications
You must be signed in to change notification settings - Fork 1
/
elastic-textarea.test.js
138 lines (113 loc) · 4.4 KB
/
elastic-textarea.test.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// If you want to use module imports you’ll need to configure
// Jest and Babel to use ESM
// See https://jestjs.io/docs/getting-started#using-babel
const { withBrowser } = require("pleasantest");
const initTextareaJS = (utils) => utils.runJS(`import './index.js'`);
test(
"Resizes correctly with no rows attribute",
withBrowser(async ({ utils, screen, user }) => {
await utils.injectHTML(`
<elastic-textarea>
<textarea></textarea>
</elastic-textarea>
`);
const textarea = await screen.getByRole("textbox");
await initTextareaJS(utils);
textarea.evaluate((el) => (el.style.width = "500px"));
// Default of 2 rows
await expect(textarea).toHaveAttribute("data-min-rows", "2");
// This wraps, so both lines should be full now
await user.type(
textarea,
"this is a very long sentence with a lot of words that make it wrap"
);
// 2 rows is the default, so we don't need a rows attribute
await expect(textarea).not.toHaveAttribute("rows");
// Enter is pressed, so now there should be 3 lines (this line doesn't wrap)
await user.type(textarea, "{enter}this is a very long sentence with a lot");
await expect(textarea).toHaveAttribute("rows", "3");
// After emptying it out, it should have 2 rows, since that is the default
await user.clear(textarea);
await expect(textarea).toHaveAttribute("rows", "2");
})
);
test(
"Allows you to override the minimum number of rows",
withBrowser(async ({ utils, screen, user }) => {
await utils.injectHTML(`
<elastic-textarea>
<textarea rows="1"></textarea>
</elastic-textarea>
`);
const textarea = await screen.getByRole("textbox");
await initTextareaJS(utils);
await textarea.evaluate((el) => (el.style.width = "500px"));
// Starts at 1 row since we set rows attribute
await expect(textarea).toHaveAttribute("rows", "1");
await user.type(textarea, "I have {enter}{enter}{enter} a long message");
await expect(textarea).toHaveAttribute("rows", "4");
// After emptying it out, it should have 1 row, since that is what we initialized `rows` to
await user.clear(textarea);
await expect(textarea).toHaveAttribute("rows", "1");
})
);
test(
"Resizes on initial load",
withBrowser(async ({ utils, screen, user }) => {
await utils.injectHTML(`
<elastic-textarea>
<textarea>
I have
a long message
</textarea>
</elastic-textarea>
`);
const textarea = await screen.getByRole("textbox");
textarea.evaluate((el) => (el.style.width = "500px"));
await initTextareaJS(utils);
await expect(textarea).toHaveAttribute("rows", "6");
})
);
test(
"Supports multiple textareas",
withBrowser(async ({ utils, screen, user }) => {
await utils.injectHTML(`
<elastic-textarea>
<textarea name="textarea-1" aria-label="textarea-1"></textarea>
<textarea name="textarea-2" aria-label="textarea-2"></textarea>
</elastic-textarea>
`);
const textarea1 = await screen.getByRole("textbox", { name: "textarea-1" });
const textarea2 = await screen.getByRole("textbox", { name: "textarea-2" });
textarea1.evaluate((el) => (el.style.width = "500px"));
textarea2.evaluate((el) => (el.style.width = "500px"));
await initTextareaJS(utils);
await user.type(textarea1, "I have {enter}{enter}{enter} a long message");
await expect(textarea1).toHaveAttribute("rows", "4");
await user.type(textarea2, "I have {enter}{enter} a medium message");
await expect(textarea2).toHaveAttribute("rows", "3");
})
);
test(
"Still shrinks when over 10 rows",
withBrowser(async ({ utils, screen, user }) => {
await utils.injectHTML(`
<elastic-textarea>
<textarea></textarea>
</elastic-textarea>
`);
const textarea = await screen.getByRole("textbox");
await initTextareaJS(utils);
textarea.evaluate((el) => (el.style.width = "500px"));
// This wraps, so both lines should be full now
await user.type(
textarea,
"{enter}{enter}{enter}{enter}{enter}{enter}{enter}{enter}{enter}{enter}"
);
// 2 rows is the default, so we don't need a rows attribute
await expect(textarea).toHaveAttribute("rows", "11");
// After emptying it out, it should have 2 rows, since that is the default
await user.clear(textarea);
await expect(textarea).toHaveAttribute("rows", "2");
})
);