forked from testing-library/user-event
/
deselect-options.js
120 lines (99 loc) 路 3.85 KB
/
deselect-options.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
import userEvent from '../'
import {addListeners, setupSelect, setup} from './helpers/utils'
test('fires correct events', () => {
const {form, select, options, getEventSnapshot} = setupSelect({
multiple: true,
})
options[0].selected = true
userEvent.deselectOptions(select, '1')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: select[name="select"][value=[]]
option[value="1"][selected=true] - pointerover
select[name="select"][value=["1"]] - pointerenter
option[value="1"][selected=true] - mouseover: Left (0)
select[name="select"][value=["1"]] - mouseenter: Left (0)
option[value="1"][selected=true] - pointermove
option[value="1"][selected=true] - mousemove: Left (0)
option[value="1"][selected=true] - pointerdown
option[value="1"][selected=true] - mousedown: Left (0)
select[name="select"][value=["1"]] - focus
select[name="select"][value=["1"]] - focusin
option[value="1"][selected=true] - pointerup
option[value="1"][selected=true] - mouseup: Left (0)
select[name="select"][value=[]] - input
select[name="select"][value=[]] - change
option[value="1"][selected=false] - click: Left (0)
`)
expect(form).toHaveFormValues({select: []})
})
test('blurs previously focused element', () => {
const {form, select} = setupSelect({multiple: true})
const button = document.createElement('button')
form.append(button)
const {getEventSnapshot, clearEventCalls} = addListeners(form)
button.focus()
clearEventCalls()
userEvent.deselectOptions(select, '1')
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: form
option[value="1"][selected=false] - pointerover
option[value="1"][selected=false] - mouseover: Left (0)
option[value="1"][selected=false] - pointermove
option[value="1"][selected=false] - mousemove: Left (0)
option[value="1"][selected=false] - pointerdown
option[value="1"][selected=false] - mousedown: Left (0)
select[name="select"][value=[]] - focusin
option[value="1"][selected=false] - pointerup
option[value="1"][selected=false] - mouseup: Left (0)
select[name="select"][value=[]] - input
select[name="select"][value=[]] - change
option[value="1"][selected=false] - click: Left (0)
`)
})
test('toggle options as expected', () => {
const {element} = setup(`
<form>
<select name="select" multiple>
<option value="1">One</option>
<optgroup label="Group Name">
<option value="2">Two</option>
<option value="3">Three</option>
</optgroup>
</select>
</form>
`)
const select = element.querySelector('select')
// select one
userEvent.selectOptions(select, ['1'])
expect(element).toHaveFormValues({select: ['1']})
// select two and three
userEvent.selectOptions(select, ['2', '3'])
expect(element).toHaveFormValues({select: ['1', '2', '3']})
// deselect one and three
userEvent.deselectOptions(select, ['1', '3'])
expect(element).toHaveFormValues({select: ['2']})
})
test('sets the selected prop on the selected option using option html elements', () => {
const {select, options} = setupSelect({multiple: true})
const [o1, o2, o3] = options
o2.selected = true
o3.selected = true
userEvent.deselectOptions(select, [o3, o2])
expect(o1.selected).toBe(false)
expect(o2.selected).toBe(false)
expect(o3.selected).toBe(false)
})
test('throws error when provided element is not a multiple select', () => {
const {element} = setup(`<select />`)
expect(() => userEvent.deselectOptions(element)).toThrowError(
/unable to deselect/i,
)
})
test('throws an error one selected option does not match', () => {
const {element} = setup(
`<select multiple><option value="a">A</option><option value="b">B</option></select>`,
)
expect(() =>
userEvent.deselectOptions(element, 'Matches nothing'),
).toThrowError(/not found/i)
})