|
8 | 8 |
|
9 | 9 | import {html} from 'lit';
|
10 | 10 |
|
| 11 | +import {Environment} from '../testing/environment.js'; |
11 | 12 | import {createFormTests} from '../testing/forms.js';
|
12 | 13 | import {createTokenTests} from '../testing/tokens.js';
|
13 | 14 |
|
| 15 | +import {SwitchHarness} from './harness.js'; |
14 | 16 | import {MdSwitch} from './switch.js';
|
15 | 17 |
|
16 | 18 | describe('<md-switch>', () => {
|
| 19 | + const env = new Environment(); |
| 20 | + |
17 | 21 | describe('.styles', () => {
|
18 | 22 | createTokenTests(MdSwitch.styles);
|
19 | 23 | });
|
@@ -117,4 +121,73 @@ describe('<md-switch>', () => {
|
117 | 121 | ],
|
118 | 122 | });
|
119 | 123 | });
|
| 124 | + |
| 125 | + describe('enter key activation', () => { |
| 126 | + // Don't use harness.clickWithKeyboard() since it simulates a click event. |
| 127 | + // The underlying `<input type="checkbox">` will not dispatch click events |
| 128 | + // in response to the Enter key. |
| 129 | + |
| 130 | + it('should toggle the switch on', async () => { |
| 131 | + // Arrange |
| 132 | + const root = env.render(html`<md-switch></md-switch>`); |
| 133 | + const harness = new SwitchHarness(root.querySelector('md-switch')!); |
| 134 | + |
| 135 | + // Act |
| 136 | + await harness.keypress('Enter'); |
| 137 | + await harness.element.updateComplete; |
| 138 | + |
| 139 | + // Assert |
| 140 | + expect(harness.element.selected) |
| 141 | + .withContext('switch is selected after Enter') |
| 142 | + .toBeTrue(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should toggle the switch off', async () => { |
| 146 | + // Arrange |
| 147 | + const root = env.render(html`<md-switch selected></md-switch>`); |
| 148 | + const harness = new SwitchHarness(root.querySelector('md-switch')!); |
| 149 | + |
| 150 | + // Act |
| 151 | + await harness.keypress('Enter'); |
| 152 | + await harness.element.updateComplete; |
| 153 | + |
| 154 | + // Assert |
| 155 | + expect(harness.element.selected) |
| 156 | + .withContext('switch is unselected after Enter') |
| 157 | + .toBeFalse(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should not toggle the switch when disabled', async () => { |
| 161 | + // Arrange |
| 162 | + const root = env.render(html`<md-switch disabled></md-switch>`); |
| 163 | + const harness = new SwitchHarness(root.querySelector('md-switch')!); |
| 164 | + |
| 165 | + // Act |
| 166 | + await harness.keypress('Enter'); |
| 167 | + await harness.element.updateComplete; |
| 168 | + |
| 169 | + // Assert |
| 170 | + expect(harness.element.selected) |
| 171 | + .withContext('disabled switch is not selected after Enter') |
| 172 | + .toBeFalse(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should not toggle the switch when keydown event is canceled', async () => { |
| 176 | + // Arrange |
| 177 | + const root = env.render(html`<md-switch></md-switch>`); |
| 178 | + const harness = new SwitchHarness(root.querySelector('md-switch')!); |
| 179 | + harness.element.addEventListener('keydown', (event) => { |
| 180 | + event.preventDefault(); |
| 181 | + }); |
| 182 | + |
| 183 | + // Act |
| 184 | + await harness.keypress('Enter'); |
| 185 | + await harness.element.updateComplete; |
| 186 | + |
| 187 | + // Assert |
| 188 | + expect(harness.element.selected) |
| 189 | + .withContext('switch is not selected when Enter is canceled') |
| 190 | + .toBeFalse(); |
| 191 | + }); |
| 192 | + }); |
120 | 193 | });
|
0 commit comments