|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {signal, WritableSignal} from '@angular/core'; |
| 10 | +import {RadioGroupInputs, RadioGroupPattern} from './radio-group'; |
| 11 | +import {RadioButtonPattern} from './radio'; |
| 12 | +import {createKeyboardEvent} from '@angular/cdk/testing/private'; |
| 13 | +import {ModifierKeys} from '@angular/cdk/testing'; |
| 14 | + |
| 15 | +type TestInputs = RadioGroupInputs<string>; |
| 16 | +type TestRadio = RadioButtonPattern<string> & { |
| 17 | + disabled: WritableSignal<boolean>; |
| 18 | +}; |
| 19 | +type TestRadioGroup = RadioGroupPattern<string>; |
| 20 | + |
| 21 | +const up = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 38, 'ArrowUp', mods); |
| 22 | +const down = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 40, 'ArrowDown', mods); |
| 23 | +const left = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 37, 'ArrowLeft', mods); |
| 24 | +const right = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 39, 'ArrowRight', mods); |
| 25 | +const home = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 36, 'Home', mods); |
| 26 | +const end = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 35, 'End', mods); |
| 27 | +const space = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 32, ' ', mods); |
| 28 | +const enter = (mods?: ModifierKeys) => createKeyboardEvent('keydown', 13, 'Enter', mods); |
| 29 | + |
| 30 | +describe('RadioGroup Pattern', () => { |
| 31 | + function getRadioGroup(inputs: Partial<TestInputs> & Pick<TestInputs, 'items'>) { |
| 32 | + return new RadioGroupPattern({ |
| 33 | + items: inputs.items, |
| 34 | + value: inputs.value ?? signal([]), |
| 35 | + activeIndex: inputs.activeIndex ?? signal(0), |
| 36 | + wrap: inputs.wrap ?? signal(true), |
| 37 | + readonly: inputs.readonly ?? signal(false), |
| 38 | + disabled: inputs.disabled ?? signal(false), |
| 39 | + skipDisabled: inputs.skipDisabled ?? signal(true), |
| 40 | + focusMode: inputs.focusMode ?? signal('roving'), |
| 41 | + textDirection: inputs.textDirection ?? signal('ltr'), |
| 42 | + orientation: inputs.orientation ?? signal('vertical'), |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + function getRadios(radioGroup: TestRadioGroup, values: string[]): TestRadio[] { |
| 47 | + return values.map((value, index) => { |
| 48 | + const element = document.createElement('div'); |
| 49 | + element.role = 'radio'; |
| 50 | + return new RadioButtonPattern({ |
| 51 | + value: signal(value), |
| 52 | + id: signal(`radio-${index}`), |
| 53 | + disabled: signal(false), |
| 54 | + group: signal(radioGroup), |
| 55 | + element: signal(element), |
| 56 | + }); |
| 57 | + }) as TestRadio[]; |
| 58 | + } |
| 59 | + |
| 60 | + function getPatterns(values: string[], inputs: Partial<TestInputs> = {}) { |
| 61 | + const radioButtons = signal<TestRadio[]>([]); |
| 62 | + const radioGroup = getRadioGroup({...inputs, items: radioButtons}); |
| 63 | + radioButtons.set(getRadios(radioGroup, values)); |
| 64 | + return {radioGroup, radioButtons: radioButtons()}; |
| 65 | + } |
| 66 | + |
| 67 | + function getDefaultPatterns(inputs: Partial<TestInputs> = {}) { |
| 68 | + return getPatterns(['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'], inputs); |
| 69 | + } |
| 70 | + |
| 71 | + describe('Keyboard Navigation', () => { |
| 72 | + it('should navigate next on ArrowDown', () => { |
| 73 | + const {radioGroup} = getDefaultPatterns(); |
| 74 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 75 | + radioGroup.onKeydown(down()); |
| 76 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should navigate prev on ArrowUp', () => { |
| 80 | + const {radioGroup} = getDefaultPatterns({activeIndex: signal(1)}); |
| 81 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 82 | + radioGroup.onKeydown(up()); |
| 83 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should navigate next on ArrowRight (horizontal)', () => { |
| 87 | + const {radioGroup} = getDefaultPatterns({orientation: signal('horizontal')}); |
| 88 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 89 | + radioGroup.onKeydown(right()); |
| 90 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should navigate prev on ArrowLeft (horizontal)', () => { |
| 94 | + const {radioGroup} = getDefaultPatterns({ |
| 95 | + activeIndex: signal(1), |
| 96 | + orientation: signal('horizontal'), |
| 97 | + }); |
| 98 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 99 | + radioGroup.onKeydown(left()); |
| 100 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should navigate next on ArrowLeft (horizontal & rtl)', () => { |
| 104 | + const {radioGroup} = getDefaultPatterns({ |
| 105 | + textDirection: signal('rtl'), |
| 106 | + orientation: signal('horizontal'), |
| 107 | + }); |
| 108 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 109 | + radioGroup.onKeydown(left()); |
| 110 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should navigate prev on ArrowRight (horizontal & rtl)', () => { |
| 114 | + const {radioGroup} = getDefaultPatterns({ |
| 115 | + activeIndex: signal(1), |
| 116 | + textDirection: signal('rtl'), |
| 117 | + orientation: signal('horizontal'), |
| 118 | + }); |
| 119 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 120 | + radioGroup.onKeydown(right()); |
| 121 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should navigate to the first radio on Home', () => { |
| 125 | + const {radioGroup} = getDefaultPatterns({ |
| 126 | + activeIndex: signal(4), |
| 127 | + }); |
| 128 | + expect(radioGroup.inputs.activeIndex()).toBe(4); |
| 129 | + radioGroup.onKeydown(home()); |
| 130 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should navigate to the last radio on End', () => { |
| 134 | + const {radioGroup} = getDefaultPatterns(); |
| 135 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 136 | + radioGroup.onKeydown(end()); |
| 137 | + expect(radioGroup.inputs.activeIndex()).toBe(4); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should wrap navigation when wrap is true', () => { |
| 141 | + const {radioGroup} = getDefaultPatterns({wrap: signal(true)}); |
| 142 | + radioGroup.onKeydown(up()); |
| 143 | + expect(radioGroup.inputs.activeIndex()).toBe(4); |
| 144 | + radioGroup.onKeydown(down()); |
| 145 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should not wrap navigation when wrap is false', () => { |
| 149 | + const {radioGroup} = getDefaultPatterns({wrap: signal(false)}); |
| 150 | + radioGroup.onKeydown(up()); |
| 151 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 152 | + radioGroup.onKeydown(end()); |
| 153 | + radioGroup.onKeydown(down()); |
| 154 | + expect(radioGroup.inputs.activeIndex()).toBe(4); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should skip disabled radios when skipDisabled is true', () => { |
| 158 | + const {radioGroup, radioButtons} = getDefaultPatterns({skipDisabled: signal(true)}); |
| 159 | + radioButtons[1].disabled.set(true); |
| 160 | + radioGroup.onKeydown(down()); |
| 161 | + expect(radioGroup.inputs.activeIndex()).toBe(2); |
| 162 | + radioGroup.onKeydown(up()); |
| 163 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should not skip disabled radios when skipDisabled is false', () => { |
| 167 | + const {radioGroup, radioButtons} = getDefaultPatterns({skipDisabled: signal(false)}); |
| 168 | + radioButtons[1].disabled.set(true); |
| 169 | + radioGroup.onKeydown(down()); |
| 170 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 171 | + radioGroup.onKeydown(up()); |
| 172 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should be able to navigate in readonly mode', () => { |
| 176 | + const {radioGroup} = getDefaultPatterns({readonly: signal(true)}); |
| 177 | + radioGroup.onKeydown(down()); |
| 178 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 179 | + radioGroup.onKeydown(up()); |
| 180 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 181 | + radioGroup.onKeydown(end()); |
| 182 | + expect(radioGroup.inputs.activeIndex()).toBe(4); |
| 183 | + radioGroup.onKeydown(home()); |
| 184 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('Keyboard Selection', () => { |
| 189 | + let radioGroup: TestRadioGroup; |
| 190 | + |
| 191 | + beforeEach(() => { |
| 192 | + radioGroup = getDefaultPatterns({value: signal([])}).radioGroup; |
| 193 | + }); |
| 194 | + |
| 195 | + it('should select a radio on Space', () => { |
| 196 | + radioGroup.onKeydown(space()); |
| 197 | + expect(radioGroup.inputs.value()).toEqual(['Apple']); |
| 198 | + }); |
| 199 | + |
| 200 | + it('should select a radio on Enter', () => { |
| 201 | + radioGroup.onKeydown(enter()); |
| 202 | + expect(radioGroup.inputs.value()).toEqual(['Apple']); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should select the focused radio on navigation (implicit selection)', () => { |
| 206 | + radioGroup.onKeydown(down()); |
| 207 | + expect(radioGroup.inputs.value()).toEqual(['Banana']); |
| 208 | + radioGroup.onKeydown(up()); |
| 209 | + expect(radioGroup.inputs.value()).toEqual(['Apple']); |
| 210 | + radioGroup.onKeydown(end()); |
| 211 | + expect(radioGroup.inputs.value()).toEqual(['Elderberry']); |
| 212 | + radioGroup.onKeydown(home()); |
| 213 | + expect(radioGroup.inputs.value()).toEqual(['Apple']); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should not be able to change selection when in readonly mode', () => { |
| 217 | + const readonly = radioGroup.inputs.readonly as WritableSignal<boolean>; |
| 218 | + readonly.set(true); |
| 219 | + radioGroup.onKeydown(space()); |
| 220 | + expect(radioGroup.inputs.value()).toEqual([]); |
| 221 | + |
| 222 | + radioGroup.onKeydown(down()); // Navigation still works |
| 223 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 224 | + expect(radioGroup.inputs.value()).toEqual([]); // Selection doesn't change |
| 225 | + |
| 226 | + radioGroup.onKeydown(enter()); |
| 227 | + expect(radioGroup.inputs.value()).toEqual([]); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should not select a disabled radio via keyboard', () => { |
| 231 | + const {radioGroup, radioButtons} = getPatterns(['A', 'B', 'C'], { |
| 232 | + skipDisabled: signal(false), |
| 233 | + }); |
| 234 | + radioButtons[1].disabled.set(true); |
| 235 | + |
| 236 | + radioGroup.onKeydown(down()); // Focus B (disabled) |
| 237 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 238 | + expect(radioGroup.inputs.value()).toEqual([]); // Should not select B |
| 239 | + |
| 240 | + radioGroup.onKeydown(space()); // Try selecting B with space |
| 241 | + expect(radioGroup.inputs.value()).toEqual([]); |
| 242 | + |
| 243 | + radioGroup.onKeydown(enter()); // Try selecting B with enter |
| 244 | + expect(radioGroup.inputs.value()).toEqual([]); |
| 245 | + |
| 246 | + radioGroup.onKeydown(down()); // Focus C |
| 247 | + expect(radioGroup.inputs.activeIndex()).toBe(2); |
| 248 | + expect(radioGroup.inputs.value()).toEqual(['C']); // Selects C on navigation |
| 249 | + }); |
| 250 | + }); |
| 251 | + |
| 252 | + describe('Pointer Events', () => { |
| 253 | + function click(radios: TestRadio[], index: number) { |
| 254 | + return { |
| 255 | + target: radios[index].element(), |
| 256 | + } as unknown as PointerEvent; |
| 257 | + } |
| 258 | + |
| 259 | + it('should select a radio on click', () => { |
| 260 | + const {radioGroup, radioButtons} = getDefaultPatterns(); |
| 261 | + radioGroup.onPointerdown(click(radioButtons, 1)); |
| 262 | + expect(radioGroup.inputs.value()).toEqual(['Banana']); |
| 263 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 264 | + }); |
| 265 | + |
| 266 | + it('should not select a disabled radio on click', () => { |
| 267 | + const {radioGroup, radioButtons} = getDefaultPatterns(); |
| 268 | + radioButtons[1].disabled.set(true); |
| 269 | + radioGroup.onPointerdown(click(radioButtons, 1)); |
| 270 | + expect(radioGroup.inputs.value()).toEqual([]); |
| 271 | + expect(radioGroup.inputs.activeIndex()).toBe(0); // Active index shouldn't change |
| 272 | + }); |
| 273 | + |
| 274 | + it('should only update active index when readonly', () => { |
| 275 | + const {radioGroup, radioButtons} = getDefaultPatterns({readonly: signal(true)}); |
| 276 | + radioGroup.onPointerdown(click(radioButtons, 1)); |
| 277 | + expect(radioGroup.inputs.value()).toEqual([]); |
| 278 | + expect(radioGroup.inputs.activeIndex()).toBe(1); // Active index should update |
| 279 | + }); |
| 280 | + }); |
| 281 | + |
| 282 | + describe('#setDefaultState', () => { |
| 283 | + it('should set the active index to the first radio', () => { |
| 284 | + const {radioGroup} = getDefaultPatterns({activeIndex: signal(-1)}); |
| 285 | + radioGroup.setDefaultState(); |
| 286 | + expect(radioGroup.inputs.activeIndex()).toBe(0); |
| 287 | + }); |
| 288 | + |
| 289 | + it('should set the active index to the first focusable radio', () => { |
| 290 | + const {radioGroup, radioButtons} = getDefaultPatterns({ |
| 291 | + skipDisabled: signal(true), |
| 292 | + activeIndex: signal(-1), |
| 293 | + }); |
| 294 | + radioButtons[0].disabled.set(true); |
| 295 | + radioGroup.setDefaultState(); |
| 296 | + expect(radioGroup.inputs.activeIndex()).toBe(1); |
| 297 | + }); |
| 298 | + |
| 299 | + it('should set the active index to the selected radio', () => { |
| 300 | + const {radioGroup} = getDefaultPatterns({ |
| 301 | + value: signal(['Cherry']), |
| 302 | + activeIndex: signal(-1), |
| 303 | + }); |
| 304 | + radioGroup.setDefaultState(); |
| 305 | + expect(radioGroup.inputs.activeIndex()).toBe(2); |
| 306 | + }); |
| 307 | + |
| 308 | + it('should set the active index to the first focusable radio if selected is disabled', () => { |
| 309 | + const {radioGroup, radioButtons} = getDefaultPatterns({ |
| 310 | + value: signal(['Cherry']), |
| 311 | + skipDisabled: signal(true), |
| 312 | + activeIndex: signal(-1), |
| 313 | + }); |
| 314 | + radioButtons[2].disabled.set(true); // Disable Cherry |
| 315 | + radioGroup.setDefaultState(); |
| 316 | + expect(radioGroup.inputs.activeIndex()).toBe(0); // Defaults to first focusable |
| 317 | + }); |
| 318 | + }); |
| 319 | +}); |
0 commit comments