/
clear.test.ts
48 lines (44 loc) · 1.6 KB
/
clear.test.ts
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
import { withBrowser } from 'test-mule';
test(
'clears input element',
withBrowser(async ({ user, utils, screen }) => {
await utils.injectHTML(`<input />`);
const input = await screen.getByRole('textbox');
await user.type(input, 'hiiiiiiii');
await expect(input).toHaveValue('hiiiiiiii');
await user.clear(input);
await expect(input).toHaveValue('');
}),
);
test(
'clears textarea element',
withBrowser(async ({ user, utils, screen }) => {
await utils.injectHTML(`<textarea>some text</textarea>`);
const input = await screen.getByRole('textbox');
await expect(input).toHaveValue('some text');
await user.type(input, ' asdf{enter}hi');
await expect(input).toHaveValue('some text asdf\nhi');
await user.clear(input);
await expect(input).toHaveValue('');
}),
);
test(
'throws for contenteditable elements',
withBrowser(async ({ user, utils, screen }) => {
await utils.injectHTML(`<div contenteditable>text</div>`);
const div = await screen.getByText(/text/);
await expect(user.clear(div)).rejects.toThrowErrorMatchingInlineSnapshot(
`"user.clear is only available for <input> and <textarea> elements, received: <div contenteditable=\\"\\">text</div>"`,
);
}),
);
test(
'throws for non-input elements',
withBrowser(async ({ user, utils, screen }) => {
await utils.injectHTML(`<div>text</div>`);
const div = await screen.getByText(/text/);
await expect(user.clear(div)).rejects.toThrowErrorMatchingInlineSnapshot(
`"user.clear is only available for <input> and <textarea> elements, received: <div>text</div>"`,
);
}),
);