|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import { configs, test } from '@utils/test/playwright'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Functionality is the same across modes & directions |
| 6 | + */ |
| 7 | +configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => { |
| 8 | + test.describe(title('toggle: bottom content functionality'), () => { |
| 9 | + test('should not render bottom content if no hint is enabled', async ({ page }) => { |
| 10 | + await page.setContent(`<ion-toggle>Label</ion-toggle>`, config); |
| 11 | + |
| 12 | + const bottomEl = page.locator('ion-toggle .toggle-bottom'); |
| 13 | + await expect(bottomEl).toHaveCount(0); |
| 14 | + }); |
| 15 | + test('helper text should be visible initially', async ({ page }) => { |
| 16 | + await page.setContent(`<ion-toggle helper-text="Helper text" error-text="Error text">Label</ion-toggle>`, config); |
| 17 | + |
| 18 | + const helperText = page.locator('ion-toggle .helper-text'); |
| 19 | + const errorText = page.locator('ion-toggle .error-text'); |
| 20 | + await expect(helperText).toBeVisible(); |
| 21 | + await expect(helperText).toHaveText('Helper text'); |
| 22 | + await expect(errorText).toBeHidden(); |
| 23 | + }); |
| 24 | + test('toggle should have an aria-describedby attribute when helper text is present', async ({ page }) => { |
| 25 | + await page.setContent(`<ion-toggle helper-text="Helper text" error-text="Error text">Label</ion-toggle>`, config); |
| 26 | + |
| 27 | + const toggle = page.locator('ion-toggle'); |
| 28 | + const helperText = page.locator('ion-toggle .helper-text'); |
| 29 | + const helperTextId = await helperText.getAttribute('id'); |
| 30 | + const ariaDescribedBy = await toggle.getAttribute('aria-describedby'); |
| 31 | + |
| 32 | + expect(ariaDescribedBy).toBe(helperTextId); |
| 33 | + }); |
| 34 | + test('error text should be visible when toggle is invalid', async ({ page }) => { |
| 35 | + await page.setContent( |
| 36 | + `<ion-toggle class="ion-invalid ion-touched" helper-text="Helper text" error-text="Error text">Label</ion-toggle>`, |
| 37 | + config |
| 38 | + ); |
| 39 | + |
| 40 | + const helperText = page.locator('ion-toggle .helper-text'); |
| 41 | + const errorText = page.locator('ion-toggle .error-text'); |
| 42 | + await expect(helperText).toBeHidden(); |
| 43 | + await expect(errorText).toBeVisible(); |
| 44 | + await expect(errorText).toHaveText('Error text'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('toggle should have an aria-describedby attribute when error text is present', async ({ page }) => { |
| 48 | + await page.setContent( |
| 49 | + `<ion-toggle class="ion-invalid ion-touched" helper-text="Helper text" error-text="Error text">Label</ion-toggle>`, |
| 50 | + config |
| 51 | + ); |
| 52 | + |
| 53 | + const toggle = page.locator('ion-toggle'); |
| 54 | + const errorText = page.locator('ion-toggle .error-text'); |
| 55 | + const errorTextId = await errorText.getAttribute('id'); |
| 56 | + const ariaDescribedBy = await toggle.getAttribute('aria-describedby'); |
| 57 | + |
| 58 | + expect(ariaDescribedBy).toBe(errorTextId); |
| 59 | + }); |
| 60 | + test('toggle should have aria-invalid attribute when toggle is invalid', async ({ page }) => { |
| 61 | + await page.setContent( |
| 62 | + `<ion-toggle class="ion-invalid ion-touched" helper-text="Helper text" error-text="Error text">Label</ion-toggle>`, |
| 63 | + config |
| 64 | + ); |
| 65 | + |
| 66 | + const toggle = page.locator('ion-toggle'); |
| 67 | + |
| 68 | + await expect(toggle).toHaveAttribute('aria-invalid'); |
| 69 | + }); |
| 70 | + test('toggle should not have aria-invalid attribute when toggle is valid', async ({ page }) => { |
| 71 | + await page.setContent(`<ion-toggle helper-text="Helper text" error-text="Error text">Label</ion-toggle>`, config); |
| 72 | + |
| 73 | + const toggle = page.locator('ion-toggle'); |
| 74 | + |
| 75 | + await expect(toggle).not.toHaveAttribute('aria-invalid'); |
| 76 | + }); |
| 77 | + test('toggle should not have aria-describedby attribute when no hint or error text is present', async ({ |
| 78 | + page, |
| 79 | + }) => { |
| 80 | + await page.setContent(`<ion-toggle>Label</ion-toggle>`, config); |
| 81 | + |
| 82 | + const toggle = page.locator('ion-toggle'); |
| 83 | + |
| 84 | + await expect(toggle).not.toHaveAttribute('aria-describedby'); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +/** |
| 90 | + * Rendering is different across modes |
| 91 | + */ |
| 92 | +configs({ modes: ['ios', 'md'], directions: ['ltr'] }).forEach(({ title, screenshot, config }) => { |
| 93 | + test.describe(title('toggle: helper text rendering'), () => { |
| 94 | + // Check the default label placement, end, and stacked |
| 95 | + [undefined, 'end', 'stacked'].forEach((labelPlacement) => { |
| 96 | + test(`${ |
| 97 | + labelPlacement ? `${labelPlacement} label - ` : '' |
| 98 | + }should not have visual regressions when rendering helper text`, async ({ page }) => { |
| 99 | + await page.setContent( |
| 100 | + `<ion-toggle ${ |
| 101 | + labelPlacement ? `label-placement="${labelPlacement}"` : '' |
| 102 | + } helper-text="Helper text">Label</ion-toggle>`, |
| 103 | + config |
| 104 | + ); |
| 105 | + |
| 106 | + const bottomEl = page.locator('ion-toggle'); |
| 107 | + await expect(bottomEl).toHaveScreenshot( |
| 108 | + screenshot(`toggle-helper-text${labelPlacement ? `-${labelPlacement}` : ''}`) |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + test(`${ |
| 113 | + labelPlacement ? `${labelPlacement} label - ` : '' |
| 114 | + }should not have visual regressions when rendering helper text with wrapping text`, async ({ page }) => { |
| 115 | + await page.setContent( |
| 116 | + `<ion-toggle ${ |
| 117 | + labelPlacement ? `label-placement="${labelPlacement}"` : '' |
| 118 | + } helper-text="Helper text helper text helper text helper text helper text helper text helper text helper text helper text">Label</ion-toggle>`, |
| 119 | + config |
| 120 | + ); |
| 121 | + |
| 122 | + const bottomEl = page.locator('ion-toggle'); |
| 123 | + await expect(bottomEl).toHaveScreenshot( |
| 124 | + screenshot(`toggle-helper-text${labelPlacement ? `-${labelPlacement}` : ''}-wrapping`) |
| 125 | + ); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + test.describe(title('toggle: error text rendering'), () => { |
| 131 | + test('should not have visual regressions when rendering error text', async ({ page }) => { |
| 132 | + await page.setContent( |
| 133 | + `<ion-toggle class="ion-invalid ion-touched" error-text="Error text">Label</ion-toggle>`, |
| 134 | + config |
| 135 | + ); |
| 136 | + |
| 137 | + const bottomEl = page.locator('ion-toggle'); |
| 138 | + await expect(bottomEl).toHaveScreenshot(screenshot(`toggle-error-text`)); |
| 139 | + }); |
| 140 | + test('should not have visual regressions when rendering error text with a stacked label', async ({ page }) => { |
| 141 | + await page.setContent( |
| 142 | + `<ion-toggle class="ion-invalid ion-touched" error-text="Error text" label-placement="stacked">Label</ion-toggle>`, |
| 143 | + config |
| 144 | + ); |
| 145 | + |
| 146 | + const bottomEl = page.locator('ion-toggle'); |
| 147 | + await expect(bottomEl).toHaveScreenshot(screenshot(`toggle-error-text-stacked-label`)); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +/** |
| 153 | + * Customizing supporting text is the same across modes and directions |
| 154 | + */ |
| 155 | +configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, screenshot, config }) => { |
| 156 | + test.describe(title('toggle: supporting text customization'), () => { |
| 157 | + test('should not have visual regressions when rendering helper text with custom css', async ({ page }) => { |
| 158 | + await page.setContent( |
| 159 | + ` |
| 160 | + <style> |
| 161 | + ion-toggle::part(supporting-text) { |
| 162 | + font-size: 20px; |
| 163 | + } |
| 164 | +
|
| 165 | + ion-toggle::part(helper-text) { |
| 166 | + color: green; |
| 167 | + } |
| 168 | + </style> |
| 169 | + <ion-toggle helper-text="Helper text">Label</ion-toggle> |
| 170 | + `, |
| 171 | + config |
| 172 | + ); |
| 173 | + |
| 174 | + const helperText = page.locator('ion-toggle'); |
| 175 | + await expect(helperText).toHaveScreenshot(screenshot(`toggle-helper-text-custom-css`)); |
| 176 | + }); |
| 177 | + test('should not have visual regressions when rendering error text with custom css', async ({ page }) => { |
| 178 | + await page.setContent( |
| 179 | + ` |
| 180 | + <style> |
| 181 | + ion-toggle::part(supporting-text) { |
| 182 | + font-size: 20px; |
| 183 | + } |
| 184 | +
|
| 185 | + ion-toggle::part(error-text) { |
| 186 | + color: purple; |
| 187 | + } |
| 188 | + </style> |
| 189 | + <ion-toggle class="ion-invalid ion-touched" error-text="Error text">Label</ion-toggle> |
| 190 | + `, |
| 191 | + config |
| 192 | + ); |
| 193 | + |
| 194 | + const errorText = page.locator('ion-toggle'); |
| 195 | + await expect(errorText).toHaveScreenshot(screenshot(`toggle-error-text-custom-css`)); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments