|
1 | 1 | import { expect } from '@playwright/test'; |
2 | 2 | import { configs, test } from '@utils/test/playwright'; |
3 | 3 |
|
4 | | -configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => { |
| 4 | +configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, screenshot, config }) => { |
| 5 | + test.describe(title('toggle: ionChange'), () => { |
| 6 | + test('should fire ionChange when interacting with toggle', async ({ page }) => { |
| 7 | + await page.setContent( |
| 8 | + ` |
| 9 | + <ion-toggle aria-label="toggle" value="my-toggle"></ion-toggle> |
| 10 | + `, |
| 11 | + config |
| 12 | + ); |
| 13 | + |
| 14 | + const ionChange = await page.spyOnEvent('ionChange'); |
| 15 | + const toggle = page.locator('ion-toggle'); |
| 16 | + |
| 17 | + await toggle.click(); |
| 18 | + expect(ionChange).toHaveReceivedEventDetail({ value: 'my-toggle', checked: true }); |
| 19 | + |
| 20 | + await toggle.click(); |
| 21 | + expect(ionChange).toHaveReceivedEventDetail({ value: 'my-toggle', checked: false }); |
| 22 | + }); |
| 23 | + |
| 24 | + test('should fire ionChange when interacting with toggle in item', async ({ page }) => { |
| 25 | + await page.setContent( |
| 26 | + ` |
| 27 | + <ion-item> |
| 28 | + <ion-toggle aria-label="toggle" value="my-toggle"></ion-toggle> |
| 29 | + </ion-item> |
| 30 | + `, |
| 31 | + config |
| 32 | + ); |
| 33 | + |
| 34 | + const ionChange = await page.spyOnEvent('ionChange'); |
| 35 | + const item = page.locator('ion-item'); |
| 36 | + |
| 37 | + await item.click(); |
| 38 | + expect(ionChange).toHaveReceivedEventDetail({ value: 'my-toggle', checked: true }); |
| 39 | + |
| 40 | + await item.click(); |
| 41 | + expect(ionChange).toHaveReceivedEventDetail({ value: 'my-toggle', checked: false }); |
| 42 | + }); |
| 43 | + |
| 44 | + test('should not fire when programmatically setting a value', async ({ page }) => { |
| 45 | + await page.setContent( |
| 46 | + ` |
| 47 | + <ion-toggle aria-label="toggle" value="my-toggle"></ion-toggle> |
| 48 | + `, |
| 49 | + config |
| 50 | + ); |
| 51 | + |
| 52 | + const ionChange = await page.spyOnEvent('ionChange'); |
| 53 | + const toggle = page.locator('ion-toggle'); |
| 54 | + |
| 55 | + await toggle.evaluate((el: HTMLIonToggleElement) => (el.checked = true)); |
| 56 | + expect(ionChange).not.toHaveReceivedEvent(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
5 | 60 | test.describe(title('toggle: click'), () => { |
6 | 61 | test('should trigger onclick only once when clicking the label', async ({ page }, testInfo) => { |
7 | 62 | testInfo.annotations.push({ |
@@ -35,4 +90,108 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => |
35 | 90 | expect(clickCount).toBe(1); |
36 | 91 | }); |
37 | 92 | }); |
| 93 | + |
| 94 | + test.describe(title('toggle: ionFocus'), () => { |
| 95 | + test('should fire ionFocus when toggle is focused', async ({ page, pageUtils }) => { |
| 96 | + await page.setContent( |
| 97 | + ` |
| 98 | + <ion-toggle aria-label="toggle" value="my-toggle"></ion-toggle> |
| 99 | + `, |
| 100 | + config |
| 101 | + ); |
| 102 | + |
| 103 | + const ionFocus = await page.spyOnEvent('ionFocus'); |
| 104 | + |
| 105 | + // Test focus with keyboard navigation. |
| 106 | + await pageUtils.pressKeys('Tab'); |
| 107 | + |
| 108 | + expect(ionFocus).toHaveReceivedEventTimes(1); |
| 109 | + |
| 110 | + // Reset focus. |
| 111 | + const toggle = page.locator('ion-toggle'); |
| 112 | + const toggleBoundingBox = (await toggle.boundingBox())!; |
| 113 | + await page.mouse.click(0, toggleBoundingBox.height + 1); |
| 114 | + |
| 115 | + // Test focus with click. |
| 116 | + await toggle.click(); |
| 117 | + |
| 118 | + expect(ionFocus).toHaveReceivedEventTimes(2); |
| 119 | + }); |
| 120 | + |
| 121 | + test('should fire ionFocus when interacting with toggle in item', async ({ page, pageUtils }) => { |
| 122 | + await page.setContent( |
| 123 | + ` |
| 124 | + <ion-item> |
| 125 | + <ion-toggle aria-label="toggle" value="my-toggle"></ion-toggle> |
| 126 | + </ion-item> |
| 127 | + `, |
| 128 | + config |
| 129 | + ); |
| 130 | + |
| 131 | + const ionFocus = await page.spyOnEvent('ionFocus'); |
| 132 | + |
| 133 | + // Test focus with keyboard navigation. |
| 134 | + await pageUtils.pressKeys('Tab'); |
| 135 | + |
| 136 | + expect(ionFocus).toHaveReceivedEventTimes(1); |
| 137 | + |
| 138 | + // Verify that the event target is the toggle and not the item. |
| 139 | + const eventByKeyboard = ionFocus.events[0]; |
| 140 | + expect((eventByKeyboard.target as HTMLElement).tagName.toLowerCase()).toBe('ion-toggle'); |
| 141 | + |
| 142 | + // Reset focus. |
| 143 | + const toggle = page.locator('ion-toggle'); |
| 144 | + const toggleBoundingBox = (await toggle.boundingBox())!; |
| 145 | + await page.mouse.click(0, toggleBoundingBox.height + 1); |
| 146 | + |
| 147 | + // Test focus with click. |
| 148 | + const item = page.locator('ion-item'); |
| 149 | + await item.click(); |
| 150 | + |
| 151 | + expect(ionFocus).toHaveReceivedEventTimes(2); |
| 152 | + |
| 153 | + // Verify that the event target is the toggle and not the item. |
| 154 | + const eventByClick = ionFocus.events[0]; |
| 155 | + expect((eventByClick.target as HTMLElement).tagName.toLowerCase()).toBe('ion-toggle'); |
| 156 | + }); |
| 157 | + |
| 158 | + test('should not fire when programmatically setting a value', async ({ page }) => { |
| 159 | + await page.setContent( |
| 160 | + ` |
| 161 | + <ion-toggle aria-label="toggle" value="my-toggle"></ion-toggle> |
| 162 | + `, |
| 163 | + config |
| 164 | + ); |
| 165 | + |
| 166 | + const ionFocus = await page.spyOnEvent('ionFocus'); |
| 167 | + const toggle = page.locator('ion-toggle'); |
| 168 | + |
| 169 | + await toggle.evaluate((el: HTMLIonToggleElement) => (el.checked = true)); |
| 170 | + expect(ionFocus).not.toHaveReceivedEvent(); |
| 171 | + }); |
| 172 | + |
| 173 | + test('should not have visual regressions', async ({ page, pageUtils }) => { |
| 174 | + await page.setContent( |
| 175 | + ` |
| 176 | + <style> |
| 177 | + #container { |
| 178 | + display: inline-block; |
| 179 | + padding: 10px; |
| 180 | + } |
| 181 | + </style> |
| 182 | +
|
| 183 | + <div id="container"> |
| 184 | + <ion-toggle>Unchecked</ion-toggle> |
| 185 | + </div> |
| 186 | + `, |
| 187 | + config |
| 188 | + ); |
| 189 | + |
| 190 | + await pageUtils.pressKeys('Tab'); |
| 191 | + |
| 192 | + const container = page.locator('#container'); |
| 193 | + |
| 194 | + await expect(container).toHaveScreenshot(screenshot(`toggle-focus`)); |
| 195 | + }); |
| 196 | + }); |
38 | 197 | }); |
0 commit comments