|
1 |
| -import { test, expect } from '@playwright/test'; |
2 |
| -import { selectTestDriver } from './select.driver'; |
3 |
| - |
4 |
| -test.beforeEach(async ({ page }) => { |
| 1 | +import { test, expect, type Page } from '@playwright/test'; |
| 2 | +import { createTestDriver } from './select.driver'; |
| 3 | +async function setup(page: Page, selector: string) { |
5 | 4 | await page.goto('/docs/headless/select');
|
6 |
| -}); |
| 5 | + |
| 6 | + const driver = createTestDriver(page.getByTestId(selector)); |
| 7 | + |
| 8 | + const { getListbox, getTrigger, getOptions } = driver; |
| 9 | + |
| 10 | + return { |
| 11 | + driver, |
| 12 | + getListbox, |
| 13 | + getTrigger, |
| 14 | + getOptions, |
| 15 | + }; |
| 16 | +} |
7 | 17 |
|
8 | 18 | test.describe('critical functionality', () => {
|
9 |
| - test(`GIVEN a basic select |
| 19 | + test(`GIVEN the hero select |
10 | 20 | WHEN clicking on the trigger
|
11 |
| - THEN open up the listbox |
12 |
| - AND aria-expanded should be true`, async ({ page }) => { |
13 |
| - const testDriver = selectTestDriver(page.getByTestId('select-hero-test')); |
14 |
| - |
15 |
| - const { getListbox, getTrigger } = testDriver; |
| 21 | + THEN open up the listbox AND aria-expanded should be true`, async ({ page }) => { |
| 22 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
16 | 23 |
|
17 | 24 | await getTrigger().click();
|
18 | 25 |
|
19 | 26 | await expect(getListbox()).toBeVisible();
|
20 | 27 | await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true');
|
21 | 28 | });
|
22 | 29 |
|
23 |
| - test(`GIVEN a basic select |
| 30 | + test(`GIVEN the hero select |
| 31 | + WHEN focusing the trigger and hitting enter |
| 32 | + THEN open up the listbox AND aria-expanded should be true`, async ({ page }) => { |
| 33 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
| 34 | + |
| 35 | + await getTrigger().focus(); |
| 36 | + await getTrigger().press('Enter'); |
| 37 | + |
| 38 | + await expect(getListbox()).toBeVisible(); |
| 39 | + await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true'); |
| 40 | + }); |
| 41 | + |
| 42 | + test(`GIVEN a hero select with an open listbox |
| 43 | + WHEN the trigger is clicked |
| 44 | + THEN close the listbox AND aria-expanded should be false`, async ({ page }) => { |
| 45 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
| 46 | + |
| 47 | + await getTrigger().click(); |
| 48 | + // should be open initially |
| 49 | + await expect(getListbox()).toBeVisible(); |
| 50 | + |
| 51 | + await getTrigger().click(); |
| 52 | + await expect(getListbox()).toBeHidden(); |
| 53 | + await expect(getTrigger()).toHaveAttribute('aria-expanded', 'false'); |
| 54 | + }); |
| 55 | + |
| 56 | + test(`GIVEN a hero select with an open listbox |
24 | 57 | WHEN focusing the trigger and hitting enter
|
25 |
| - THEN open up the listbox |
26 |
| - AND aria-expanded should be true`, async ({ page }) => { |
27 |
| - const testDriver = selectTestDriver(page.getByTestId('select-hero-test')); |
| 58 | + THEN close the listbox AND aria-expanded should be false`, async ({ page }) => { |
| 59 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
| 60 | + |
| 61 | + await getTrigger().click(); |
| 62 | + // should be open initially |
| 63 | + await expect(getListbox()).toBeVisible(); |
28 | 64 |
|
29 |
| - const { getListbox, getTrigger } = testDriver; |
| 65 | + await getTrigger().focus(); |
| 66 | + await getTrigger().press('Enter'); |
| 67 | + await expect(getListbox()).toBeHidden(); |
| 68 | + await expect(getTrigger()).toHaveAttribute('aria-expanded', 'false'); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +test.describe('Keyboard Behavior', () => { |
| 73 | + test(`GIVEN a hero select |
| 74 | + WHEN pressing the space key |
| 75 | + THEN open up the listbox AND aria-expanded should be true`, async ({ page }) => { |
| 76 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
30 | 77 |
|
31 | 78 | await getTrigger().focus();
|
32 |
| - await page.keyboard.press('Enter'); |
| 79 | + await getTrigger().press('Space'); |
33 | 80 |
|
34 | 81 | await expect(getListbox()).toBeVisible();
|
35 | 82 | await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true');
|
36 | 83 | });
|
| 84 | + |
| 85 | + test(`GIVEN a hero select with an open listbox |
| 86 | + WHEN pressing the space key |
| 87 | + THEN close listbox AND aria-expanded should be false`, async ({ page }) => { |
| 88 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
| 89 | + |
| 90 | + await getTrigger().focus(); |
| 91 | + await getTrigger().press('Space'); |
| 92 | + // should be open initially |
| 93 | + await expect(getListbox()).toBeVisible(); |
| 94 | + |
| 95 | + await getTrigger().focus(); |
| 96 | + await getTrigger().press('Space'); |
| 97 | + await expect(getListbox()).toBeHidden(); |
| 98 | + await expect(getTrigger()).toHaveAttribute('aria-expanded', 'false'); |
| 99 | + }); |
| 100 | + |
| 101 | + test(`GIVEN a hero select |
| 102 | + WHEN pressing the down arrow key |
| 103 | + THEN open up the listbox AND aria-expanded should be true`, async ({ page }) => { |
| 104 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
| 105 | + |
| 106 | + await getTrigger().focus(); |
| 107 | + await getTrigger().press('ArrowDown'); |
| 108 | + await expect(getListbox()).toBeVisible(); |
| 109 | + }); |
| 110 | + |
| 111 | + test(`GIVEN a hero select with an opened listbox |
| 112 | + WHEN pressing the escape key |
| 113 | + THEN the listbox should close |
| 114 | + AND aria-expanded should be false`, async ({ page }) => { |
| 115 | + const { getTrigger, getListbox } = await setup(page, 'select-hero-test'); |
| 116 | + |
| 117 | + await getTrigger().click(); |
| 118 | + // should be open initially |
| 119 | + await expect(getListbox()).toBeVisible(); |
| 120 | + |
| 121 | + await getTrigger().focus(); |
| 122 | + await getTrigger().press('Escape'); |
| 123 | + await expect(getListbox()).toBeHidden(); |
| 124 | + await expect(getTrigger()).toHaveAttribute('aria-expanded', 'false'); |
| 125 | + }); |
| 126 | + |
| 127 | + test(`GIVEN a hero select |
| 128 | + WHEN pressing the down arrow key |
| 129 | + THEN open up the listbox |
| 130 | + AND the first option should have data-highlighted`, async ({ page }) => { |
| 131 | + const { getTrigger, getListbox, getOptions } = await setup(page, 'select-hero-test'); |
| 132 | + |
| 133 | + await getTrigger().focus(); |
| 134 | + await getTrigger().press('ArrowDown'); |
| 135 | + await expect(getListbox()).toBeVisible(); |
| 136 | + |
| 137 | + const options = await getOptions(); |
| 138 | + await expect(options[0]).toHaveAttribute('data-highlighted'); |
| 139 | + }); |
| 140 | + |
| 141 | + test(`GIVEN a hero select |
| 142 | + WHEN pressing the enter key |
| 143 | + THEN open up the listbox |
| 144 | + AND the first option should have data-highlighted`, async ({ page }) => { |
| 145 | + const { getTrigger, getListbox, getOptions } = await setup(page, 'select-hero-test'); |
| 146 | + |
| 147 | + await getTrigger().focus(); |
| 148 | + await getTrigger().press('Enter'); |
| 149 | + await expect(getListbox()).toBeVisible(); |
| 150 | + |
| 151 | + const options = await getOptions(); |
| 152 | + await expect(options[0]).toHaveAttribute('data-highlighted'); |
| 153 | + }); |
| 154 | + |
| 155 | + test(`GIVEN a hero select |
| 156 | + WHEN pressing the space key |
| 157 | + THEN open up the listbox |
| 158 | + AND the first option should have data-highlighted`, async ({ page }) => { |
| 159 | + const { getTrigger, getListbox, getOptions } = await setup(page, 'select-hero-test'); |
| 160 | + |
| 161 | + await getTrigger().focus(); |
| 162 | + await getTrigger().press('Space'); |
| 163 | + await expect(getListbox()).toBeVisible(); |
| 164 | + |
| 165 | + const options = await getOptions(); |
| 166 | + await expect(options[0]).toHaveAttribute('data-highlighted'); |
| 167 | + }); |
| 168 | + |
| 169 | + test(`GIVEN a hero select |
| 170 | + WHEN pressing the up arrow |
| 171 | + THEN open up the listbox |
| 172 | + AND the first option should have data-highlighted`, async ({ page }) => { |
| 173 | + const { getTrigger, getListbox, getOptions } = await setup(page, 'select-hero-test'); |
| 174 | + |
| 175 | + await getTrigger().focus(); |
| 176 | + await getTrigger().press('ArrowUp'); |
| 177 | + await expect(getListbox()).toBeVisible(); |
| 178 | + |
| 179 | + const options = await getOptions(); |
| 180 | + await expect(options[0]).toHaveAttribute('data-highlighted'); |
| 181 | + }); |
| 182 | + |
| 183 | + test(`GIVEN an open hero select |
| 184 | + WHEN pressing the end key |
| 185 | + THEN the last option should have data-highlighted`, async ({ page }) => { |
| 186 | + const { getTrigger, getListbox, getOptions } = await setup(page, 'select-hero-test'); |
| 187 | + |
| 188 | + await getTrigger().click(); |
| 189 | + // should be open initially |
| 190 | + await expect(getListbox()).toBeVisible(); |
| 191 | + |
| 192 | + await getTrigger().focus(); |
| 193 | + await getTrigger().press('End'); |
| 194 | + |
| 195 | + const options = await getOptions(); |
| 196 | + const lastIndex = options.length - 1; |
| 197 | + await expect(options[lastIndex]).toHaveAttribute('data-highlighted'); |
| 198 | + }); |
| 199 | + |
| 200 | + test(`GIVEN an open hero select |
| 201 | + WHEN pressing the home key after the end key |
| 202 | + THEN the first option should have data-highlighted`, async ({ page }) => { |
| 203 | + const { getTrigger, getListbox, getOptions } = await setup(page, 'select-hero-test'); |
| 204 | + |
| 205 | + await getTrigger().click(); |
| 206 | + // should be open initially |
| 207 | + await expect(getListbox()).toBeVisible(); |
| 208 | + |
| 209 | + // to last index |
| 210 | + await getTrigger().focus(); |
| 211 | + await getTrigger().press('End'); |
| 212 | + const options = await getOptions(); |
| 213 | + const lastIndex = options.length - 1; |
| 214 | + await expect(options[lastIndex]).toHaveAttribute('data-highlighted'); |
| 215 | + |
| 216 | + // to first index |
| 217 | + await getTrigger().press('Home'); |
| 218 | + await expect(options[0]).toHaveAttribute('data-highlighted'); |
| 219 | + }); |
| 220 | + |
| 221 | + test(`GIVEN an open hero select |
| 222 | + WHEN the first option is highlighted and the down arrow key is pressed |
| 223 | + THEN the second option should have data-highlighted`, async ({ page }) => { |
| 224 | + const { getTrigger, getListbox, getOptions } = await setup(page, 'select-hero-test'); |
| 225 | + |
| 226 | + await getTrigger().focus(); |
| 227 | + await getTrigger().press('Enter'); |
| 228 | + // should be open initially |
| 229 | + await expect(getListbox()).toBeVisible(); |
| 230 | + |
| 231 | + // first index highlighted |
| 232 | + const options = await getOptions(); |
| 233 | + await expect(options[0]).toHaveAttribute('data-highlighted'); |
| 234 | + |
| 235 | + await getTrigger().focus(); |
| 236 | + await getTrigger().press('ArrowDown'); |
| 237 | + await expect(options[1]).toHaveAttribute('data-highlighted'); |
| 238 | + }); |
37 | 239 | });
|
0 commit comments