|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import { mount } from 'enzyme'; |
| 19 | +import { act } from 'react-dom/test-utils'; |
| 20 | + |
| 21 | +import AccessibleButton from '../../../../src/components/views/elements/AccessibleButton'; |
| 22 | +import { Key } from '../../../../src/Keyboard'; |
| 23 | +import { mockPlatformPeg, unmockPlatformPeg } from '../../../test-utils'; |
| 24 | + |
| 25 | +describe('<AccessibleButton />', () => { |
| 26 | + const defaultProps = { |
| 27 | + onClick: jest.fn(), |
| 28 | + children: 'i am a button', |
| 29 | + }; |
| 30 | + const getComponent = (props = {}) => |
| 31 | + mount(<AccessibleButton {...defaultProps} {...props} />); |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + mockPlatformPeg(); |
| 35 | + }); |
| 36 | + |
| 37 | + afterAll(() => { |
| 38 | + unmockPlatformPeg(); |
| 39 | + }); |
| 40 | + |
| 41 | + const makeKeyboardEvent = (key: string) => ({ |
| 42 | + key, |
| 43 | + stopPropagation: jest.fn(), |
| 44 | + preventDefault: jest.fn(), |
| 45 | + }) as unknown as KeyboardEvent; |
| 46 | + |
| 47 | + it('renders div with role button by default', () => { |
| 48 | + const component = getComponent(); |
| 49 | + expect(component).toMatchSnapshot(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('renders a button element', () => { |
| 53 | + const component = getComponent({ element: 'button' }); |
| 54 | + expect(component).toMatchSnapshot(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('renders with correct classes when button has kind', () => { |
| 58 | + const component = getComponent({ |
| 59 | + kind: 'primary', |
| 60 | + }); |
| 61 | + expect(component).toMatchSnapshot(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('disables button correctly', () => { |
| 65 | + const onClick = jest.fn(); |
| 66 | + const component = getComponent({ |
| 67 | + onClick, |
| 68 | + disabled: true, |
| 69 | + }); |
| 70 | + expect(component.find('.mx_AccessibleButton').props().disabled).toBeTruthy(); |
| 71 | + expect(component.find('.mx_AccessibleButton').props()['aria-disabled']).toBeTruthy(); |
| 72 | + |
| 73 | + act(() => { |
| 74 | + component.simulate('click'); |
| 75 | + }); |
| 76 | + |
| 77 | + expect(onClick).not.toHaveBeenCalled(); |
| 78 | + |
| 79 | + act(() => { |
| 80 | + const keydownEvent = makeKeyboardEvent(Key.ENTER); |
| 81 | + component.simulate('keydown', keydownEvent); |
| 82 | + }); |
| 83 | + |
| 84 | + expect(onClick).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('calls onClick handler on button click', () => { |
| 88 | + const onClick = jest.fn(); |
| 89 | + const component = getComponent({ |
| 90 | + onClick, |
| 91 | + }); |
| 92 | + |
| 93 | + act(() => { |
| 94 | + component.simulate('click'); |
| 95 | + }); |
| 96 | + |
| 97 | + expect(onClick).toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('calls onClick handler on button mousedown when triggerOnMousedown is passed', () => { |
| 101 | + const onClick = jest.fn(); |
| 102 | + const component = getComponent({ |
| 103 | + onClick, |
| 104 | + triggerOnMouseDown: true, |
| 105 | + }); |
| 106 | + |
| 107 | + act(() => { |
| 108 | + component.simulate('mousedown'); |
| 109 | + }); |
| 110 | + |
| 111 | + expect(onClick).toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('handling keyboard events', () => { |
| 115 | + it('calls onClick handler on enter keydown', () => { |
| 116 | + const onClick = jest.fn(); |
| 117 | + const component = getComponent({ |
| 118 | + onClick, |
| 119 | + }); |
| 120 | + |
| 121 | + const keyboardEvent = makeKeyboardEvent(Key.ENTER); |
| 122 | + act(() => { |
| 123 | + component.simulate('keydown', keyboardEvent); |
| 124 | + }); |
| 125 | + |
| 126 | + expect(onClick).toHaveBeenCalled(); |
| 127 | + |
| 128 | + act(() => { |
| 129 | + component.simulate('keyup', keyboardEvent); |
| 130 | + }); |
| 131 | + |
| 132 | + // handler only called once on keydown |
| 133 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 134 | + // called for both keyup and keydown |
| 135 | + expect(keyboardEvent.stopPropagation).toHaveBeenCalledTimes(2); |
| 136 | + expect(keyboardEvent.preventDefault).toHaveBeenCalledTimes(2); |
| 137 | + }); |
| 138 | + |
| 139 | + it('calls onClick handler on space keyup', () => { |
| 140 | + const onClick = jest.fn(); |
| 141 | + const component = getComponent({ |
| 142 | + onClick, |
| 143 | + }); |
| 144 | + |
| 145 | + const keyboardEvent = makeKeyboardEvent(Key.SPACE); |
| 146 | + act(() => { |
| 147 | + component.simulate('keydown', keyboardEvent); |
| 148 | + }); |
| 149 | + |
| 150 | + expect(onClick).not.toHaveBeenCalled(); |
| 151 | + |
| 152 | + act(() => { |
| 153 | + component.simulate('keyup', keyboardEvent); |
| 154 | + }); |
| 155 | + |
| 156 | + // handler only called once on keyup |
| 157 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 158 | + // called for both keyup and keydown |
| 159 | + expect(keyboardEvent.stopPropagation).toHaveBeenCalledTimes(2); |
| 160 | + expect(keyboardEvent.preventDefault).toHaveBeenCalledTimes(2); |
| 161 | + }); |
| 162 | + |
| 163 | + it('calls onKeydown/onKeyUp handlers for keys other than space and enter', () => { |
| 164 | + const onClick = jest.fn(); |
| 165 | + const onKeyDown = jest.fn(); |
| 166 | + const onKeyUp = jest.fn(); |
| 167 | + const component = getComponent({ |
| 168 | + onClick, |
| 169 | + onKeyDown, |
| 170 | + onKeyUp, |
| 171 | + }); |
| 172 | + |
| 173 | + const keyboardEvent = makeKeyboardEvent(Key.K); |
| 174 | + act(() => { |
| 175 | + component.simulate('keydown', keyboardEvent); |
| 176 | + component.simulate('keyup', keyboardEvent); |
| 177 | + }); |
| 178 | + |
| 179 | + expect(onClick).not.toHaveBeenCalled(); |
| 180 | + expect(onKeyDown).toHaveBeenCalled(); |
| 181 | + expect(onKeyUp).toHaveBeenCalled(); |
| 182 | + expect(keyboardEvent.stopPropagation).not.toHaveBeenCalled(); |
| 183 | + expect(keyboardEvent.preventDefault).not.toHaveBeenCalled(); |
| 184 | + }); |
| 185 | + |
| 186 | + it('does nothing on non space/enter key presses when no onKeydown/onKeyUp handlers provided', () => { |
| 187 | + const onClick = jest.fn(); |
| 188 | + const component = getComponent({ |
| 189 | + onClick, |
| 190 | + }); |
| 191 | + |
| 192 | + const keyboardEvent = makeKeyboardEvent(Key.K); |
| 193 | + act(() => { |
| 194 | + component.simulate('keydown', keyboardEvent); |
| 195 | + component.simulate('keyup', keyboardEvent); |
| 196 | + }); |
| 197 | + |
| 198 | + // no onClick call, no problems |
| 199 | + expect(onClick).not.toHaveBeenCalled(); |
| 200 | + expect(keyboardEvent.stopPropagation).not.toHaveBeenCalled(); |
| 201 | + expect(keyboardEvent.preventDefault).not.toHaveBeenCalled(); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments