|
| 1 | +import {userEvent} from '../../' |
| 2 | +import {addListeners, setupSelect, setup} from './helpers/utils' |
| 3 | + |
| 4 | +test('fires correct events', async () => { |
| 5 | + const {form, select, options, getEventSnapshot} = setupSelect({ |
| 6 | + multiple: true, |
| 7 | + }) |
| 8 | + options[0].selected = true |
| 9 | + |
| 10 | + await userEvent.deselectOptions(select, '1') |
| 11 | + |
| 12 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 13 | + Events fired on: select[name="select"][value=[]] |
| 14 | +
|
| 15 | + option[value="1"][selected=true] - pointerover |
| 16 | + select[name="select"][value=["1"]] - pointerenter |
| 17 | + option[value="1"][selected=true] - mouseover: Left (0) |
| 18 | + select[name="select"][value=["1"]] - mouseenter: Left (0) |
| 19 | + option[value="1"][selected=true] - pointermove |
| 20 | + option[value="1"][selected=true] - mousemove: Left (0) |
| 21 | + option[value="1"][selected=true] - pointerdown |
| 22 | + option[value="1"][selected=true] - mousedown: Left (0) |
| 23 | + select[name="select"][value=["1"]] - focus |
| 24 | + select[name="select"][value=["1"]] - focusin |
| 25 | + option[value="1"][selected=true] - pointerup |
| 26 | + option[value="1"][selected=true] - mouseup: Left (0) |
| 27 | + select[name="select"][value=[]] - input |
| 28 | + select[name="select"][value=[]] - change |
| 29 | + option[value="1"][selected=false] - click: Left (0) |
| 30 | + `) |
| 31 | + |
| 32 | + expect(form).toHaveFormValues({select: []}) |
| 33 | +}) |
| 34 | + |
| 35 | +test('blurs previously focused element', async () => { |
| 36 | + const {form, select} = setupSelect({multiple: true}) |
| 37 | + const button = document.createElement('button') |
| 38 | + form.append(button) |
| 39 | + |
| 40 | + const {getEventSnapshot, clearEventCalls} = addListeners(form) |
| 41 | + button.focus() |
| 42 | + |
| 43 | + clearEventCalls() |
| 44 | + await userEvent.deselectOptions(select, '1') |
| 45 | + |
| 46 | + expect(getEventSnapshot()).toMatchInlineSnapshot(` |
| 47 | + Events fired on: form |
| 48 | +
|
| 49 | + option[value="1"][selected=false] - pointerover |
| 50 | + option[value="1"][selected=false] - mouseover: Left (0) |
| 51 | + option[value="1"][selected=false] - pointermove |
| 52 | + option[value="1"][selected=false] - mousemove: Left (0) |
| 53 | + option[value="1"][selected=false] - pointerdown |
| 54 | + option[value="1"][selected=false] - mousedown: Left (0) |
| 55 | + select[name="select"][value=[]] - focusin |
| 56 | + option[value="1"][selected=false] - pointerup |
| 57 | + option[value="1"][selected=false] - mouseup: Left (0) |
| 58 | + option[value="1"][selected=false] - click: Left (0) |
| 59 | + `) |
| 60 | +}) |
| 61 | + |
| 62 | +test('toggle options as expected', async () => { |
| 63 | + const {element} = setup(` |
| 64 | + <form> |
| 65 | + <select name="select" multiple> |
| 66 | + <option value="1">One</option> |
| 67 | + <optgroup label="Group Name"> |
| 68 | + <option value="2">Two</option> |
| 69 | + <option value="3">Three</option> |
| 70 | + </optgroup> |
| 71 | + </select> |
| 72 | + </form> |
| 73 | + `) |
| 74 | + |
| 75 | + const select = element.querySelector('select') |
| 76 | + |
| 77 | + // select one |
| 78 | + await userEvent.selectOptions(select, ['1']) |
| 79 | + |
| 80 | + expect(element).toHaveFormValues({select: ['1']}) |
| 81 | + |
| 82 | + // select two and three |
| 83 | + await userEvent.selectOptions(select, ['2', '3']) |
| 84 | + expect(element).toHaveFormValues({select: ['1', '2', '3']}) |
| 85 | + |
| 86 | + // deselect one and three |
| 87 | + await userEvent.deselectOptions(select, ['1', '3']) |
| 88 | + expect(element).toHaveFormValues({select: ['2']}) |
| 89 | +}) |
| 90 | + |
| 91 | +test('sets the selected prop on the selected option using option html elements', async () => { |
| 92 | + const {select, options} = setupSelect({multiple: true}) |
| 93 | + const [o1, o2, o3] = options |
| 94 | + o2.selected = true |
| 95 | + o3.selected = true |
| 96 | + |
| 97 | + await userEvent.deselectOptions(select, [o3, o2]) |
| 98 | + expect(o1.selected).toBe(false) |
| 99 | + expect(o2.selected).toBe(false) |
| 100 | + expect(o3.selected).toBe(false) |
| 101 | +}) |
| 102 | + |
| 103 | +test('throws error when provided element is not a multiple select', async () => { |
| 104 | + const {element} = setup(`<select />`) |
| 105 | + |
| 106 | + const error = await userEvent.deselectOptions(element).catch(e => e) |
| 107 | + expect(error.message).toMatchInlineSnapshot(` |
| 108 | + "Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead. |
| 109 | +
|
| 110 | + <select />" |
| 111 | + `) |
| 112 | +}) |
| 113 | + |
| 114 | +test('throws an error one selected option does not match', async () => { |
| 115 | + const {element} = setup( |
| 116 | + `<select multiple><option value="a">A</option><option value="b">B</option></select>`, |
| 117 | + ) |
| 118 | + const error = await userEvent |
| 119 | + .deselectOptions(element, 'Matches nothing') |
| 120 | + .catch(e => e) |
| 121 | + expect(error.message).toMatchInlineSnapshot(` |
| 122 | + "Value "Matches nothing" not found in options |
| 123 | +
|
| 124 | + <select |
| 125 | + multiple="" |
| 126 | + > |
| 127 | + <option |
| 128 | + value="a" |
| 129 | + > |
| 130 | + A |
| 131 | + </option> |
| 132 | + <option |
| 133 | + value="b" |
| 134 | + > |
| 135 | + B |
| 136 | + </option> |
| 137 | + </select>" |
| 138 | + `) |
| 139 | +}) |
0 commit comments