|
| 1 | +import { newSpecPage } from '@stencil/core/testing'; |
| 2 | + |
| 3 | +import { Range } from '../../range'; |
| 4 | + |
| 5 | +describe('range: dual knobs focus management', () => { |
| 6 | + it('should properly manage initial focus with dual knobs', async () => { |
| 7 | + const page = await newSpecPage({ |
| 8 | + components: [Range], |
| 9 | + html: ` |
| 10 | + <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> |
| 11 | + </ion-range> |
| 12 | + `, |
| 13 | + }); |
| 14 | + |
| 15 | + const range = page.body.querySelector('ion-range'); |
| 16 | + expect(range).not.toBeNull(); |
| 17 | + |
| 18 | + await page.waitForChanges(); |
| 19 | + |
| 20 | + // Get the knob elements |
| 21 | + const knobA = range!.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; |
| 22 | + const knobB = range!.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; |
| 23 | + |
| 24 | + expect(knobA).not.toBeNull(); |
| 25 | + expect(knobB).not.toBeNull(); |
| 26 | + |
| 27 | + // Initially, neither knob should have the ion-focused class |
| 28 | + expect(knobA.classList.contains('ion-focused')).toBe(false); |
| 29 | + expect(knobB.classList.contains('ion-focused')).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should show focus on the correct knob when focused via keyboard navigation', async () => { |
| 33 | + const page = await newSpecPage({ |
| 34 | + components: [Range], |
| 35 | + html: ` |
| 36 | + <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> |
| 37 | + </ion-range> |
| 38 | + `, |
| 39 | + }); |
| 40 | + |
| 41 | + const range = page.body.querySelector('ion-range'); |
| 42 | + await page.waitForChanges(); |
| 43 | + |
| 44 | + const knobA = range!.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; |
| 45 | + const knobB = range!.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; |
| 46 | + |
| 47 | + // Focus knob A |
| 48 | + knobA.dispatchEvent(new Event('focus')); |
| 49 | + await page.waitForChanges(); |
| 50 | + |
| 51 | + // Only knob A should have the ion-focused class |
| 52 | + expect(knobA.classList.contains('ion-focused')).toBe(true); |
| 53 | + expect(knobB.classList.contains('ion-focused')).toBe(false); |
| 54 | + |
| 55 | + // Focus knob B |
| 56 | + knobB.dispatchEvent(new Event('focus')); |
| 57 | + await page.waitForChanges(); |
| 58 | + |
| 59 | + // Only knob B should have the ion-focused class |
| 60 | + expect(knobA.classList.contains('ion-focused')).toBe(false); |
| 61 | + expect(knobB.classList.contains('ion-focused')).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should remove focus from all knobs when focus leaves the range', async () => { |
| 65 | + const page = await newSpecPage({ |
| 66 | + components: [Range], |
| 67 | + html: ` |
| 68 | + <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> |
| 69 | + </ion-range> |
| 70 | + `, |
| 71 | + }); |
| 72 | + |
| 73 | + const range = page.body.querySelector('ion-range'); |
| 74 | + await page.waitForChanges(); |
| 75 | + |
| 76 | + const knobA = range!.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; |
| 77 | + const knobB = range!.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; |
| 78 | + |
| 79 | + // Focus knob A |
| 80 | + knobA.dispatchEvent(new Event('focus')); |
| 81 | + await page.waitForChanges(); |
| 82 | + |
| 83 | + expect(knobA.classList.contains('ion-focused')).toBe(true); |
| 84 | + |
| 85 | + // Blur the knob (focus leaves the range) |
| 86 | + knobA.dispatchEvent(new Event('blur')); |
| 87 | + await page.waitForChanges(); |
| 88 | + |
| 89 | + // Wait for the timeout in onKnobBlur to complete |
| 90 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 91 | + await page.waitForChanges(); |
| 92 | + |
| 93 | + // Neither knob should have the ion-focused class |
| 94 | + expect(knobA.classList.contains('ion-focused')).toBe(false); |
| 95 | + expect(knobB.classList.contains('ion-focused')).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should emit ionFocus when any knob receives focus', async () => { |
| 99 | + const page = await newSpecPage({ |
| 100 | + components: [Range], |
| 101 | + html: ` |
| 102 | + <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> |
| 103 | + </ion-range> |
| 104 | + `, |
| 105 | + }); |
| 106 | + |
| 107 | + const range = page.body.querySelector('ion-range')!; |
| 108 | + await page.waitForChanges(); |
| 109 | + |
| 110 | + let focusEventFired = false; |
| 111 | + range.addEventListener('ionFocus', () => { |
| 112 | + focusEventFired = true; |
| 113 | + }); |
| 114 | + |
| 115 | + const knobA = range.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; |
| 116 | + |
| 117 | + // Focus knob A |
| 118 | + knobA.dispatchEvent(new Event('focus')); |
| 119 | + await page.waitForChanges(); |
| 120 | + |
| 121 | + expect(focusEventFired).toBe(true); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should emit ionBlur when focus leaves the range completely', async () => { |
| 125 | + const page = await newSpecPage({ |
| 126 | + components: [Range], |
| 127 | + html: ` |
| 128 | + <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> |
| 129 | + </ion-range> |
| 130 | + `, |
| 131 | + }); |
| 132 | + |
| 133 | + const range = page.body.querySelector('ion-range')!; |
| 134 | + await page.waitForChanges(); |
| 135 | + |
| 136 | + let blurEventFired = false; |
| 137 | + range.addEventListener('ionBlur', () => { |
| 138 | + blurEventFired = true; |
| 139 | + }); |
| 140 | + |
| 141 | + const knobA = range.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; |
| 142 | + |
| 143 | + // Focus and then blur knob A |
| 144 | + knobA.dispatchEvent(new Event('focus')); |
| 145 | + await page.waitForChanges(); |
| 146 | + |
| 147 | + knobA.dispatchEvent(new Event('blur')); |
| 148 | + await page.waitForChanges(); |
| 149 | + |
| 150 | + // Wait for the timeout in onKnobBlur to complete |
| 151 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 152 | + await page.waitForChanges(); |
| 153 | + |
| 154 | + expect(blurEventFired).toBe(true); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should correctly handle Tab navigation between knobs', async () => { |
| 158 | + const page = await newSpecPage({ |
| 159 | + components: [Range], |
| 160 | + html: ` |
| 161 | + <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> |
| 162 | + </ion-range> |
| 163 | + `, |
| 164 | + }); |
| 165 | + |
| 166 | + const range = page.body.querySelector('ion-range')!; |
| 167 | + await page.waitForChanges(); |
| 168 | + |
| 169 | + const knobA = range.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; |
| 170 | + const knobB = range.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; |
| 171 | + |
| 172 | + // Simulate Tab to first knob |
| 173 | + knobA.dispatchEvent(new Event('focus')); |
| 174 | + await page.waitForChanges(); |
| 175 | + |
| 176 | + // First knob should be focused |
| 177 | + expect(knobA.classList.contains('ion-focused')).toBe(true); |
| 178 | + expect(knobB.classList.contains('ion-focused')).toBe(false); |
| 179 | + |
| 180 | + // Simulate Tab to second knob (blur first, focus second) |
| 181 | + knobA.dispatchEvent(new Event('blur')); |
| 182 | + knobB.dispatchEvent(new Event('focus')); |
| 183 | + await page.waitForChanges(); |
| 184 | + |
| 185 | + // Second knob should be focused, first should not |
| 186 | + expect(knobA.classList.contains('ion-focused')).toBe(false); |
| 187 | + expect(knobB.classList.contains('ion-focused')).toBe(true); |
| 188 | + |
| 189 | + // Verify Arrow key navigation still works on focused knob |
| 190 | + |
| 191 | + // Simulate Arrow Right key press on knob B |
| 192 | + const keyEvent = new KeyboardEvent('keydown', { key: 'ArrowRight' }); |
| 193 | + knobB.dispatchEvent(keyEvent); |
| 194 | + await page.waitForChanges(); |
| 195 | + |
| 196 | + // The knob that visually appears focused should be the one that responds to keyboard input |
| 197 | + expect(knobB.classList.contains('ion-focused')).toBe(true); |
| 198 | + }); |
| 199 | +}); |
0 commit comments