|
1 | 1 | import { expect, Page, test } from '~/tests/fixtures'; |
2 | 2 |
|
3 | | -// Test constants |
4 | 3 | const SHOP_ALL_URL = '/shop-all/'; |
5 | | -const SHOP_ALL_HEADING_PATTERN = /Shop All/; |
6 | 4 |
|
7 | | -// Filter names |
8 | | -const FILTER_COLOR = 'Color'; |
9 | | -const FILTER_BRAND = 'Brand'; |
10 | | - |
11 | | -// Filter values |
12 | | -const COLOR_BLUE = 'Blue'; |
13 | | -const BRAND_OFS = 'OFS'; |
14 | | - |
15 | | -// Product names |
16 | 5 | const PRODUCT_LE_PARFAIT_JAR = '[Sample] 1 L Le Parfait Jar'; |
17 | 6 | const PRODUCT_DUSTPAN_BRUSH = '[Sample] Dustpan & Brush'; |
18 | 7 | const PRODUCT_UTILITY_CADDY = '[Sample] Utility Caddy'; |
19 | 8 |
|
20 | | -// Button labels |
21 | | -const BUTTON_RESET_FILTERS = 'Reset filters'; |
22 | | - |
23 | | -// URL parameter names |
24 | | -const URL_PARAM_ATTR_COLOR = 'attr_Color'; |
25 | | -const URL_PARAM_BRAND = 'brand'; |
26 | | - |
27 | | -// Expected counts |
28 | | -const EXPECTED_OFS_BRAND_COUNT = 5; |
29 | | -const EXPECTED_INITIAL_PRODUCT_COUNT = 13; |
30 | | - |
31 | | -function extractNumber(text: string | null): number { |
32 | | - if (!text) { |
33 | | - return 0; |
34 | | - } |
35 | | - |
36 | | - const regex = /\d+/; |
37 | | - const match = regex.exec(text); |
38 | | - |
39 | | - return match ? parseInt(match[0] || '0', 10) : 0; |
40 | | -} |
41 | | - |
42 | 9 | async function expandFilterIfNeeded(page: Page, filterLabel: string) { |
43 | | - const filterButton = page.getByRole('button', { name: filterLabel }).first(); |
| 10 | + const filterButton = page |
| 11 | + .getByRole('heading', { name: filterLabel, level: 3 }) |
| 12 | + .getByRole('button', { name: filterLabel }); |
| 13 | + |
44 | 14 | const isExpanded = await filterButton.getAttribute('aria-expanded'); |
45 | 15 |
|
46 | 16 | if (isExpanded === 'false') { |
47 | 17 | await filterButton.click(); |
48 | 18 | } |
49 | 19 | } |
50 | 20 |
|
51 | | -async function clickSpecificFilterOption( |
52 | | - page: Page, |
53 | | - filterName: string, |
54 | | - optionName: string, |
55 | | -): Promise<boolean> { |
56 | | - await expandFilterIfNeeded(page, filterName); |
57 | | - |
58 | | - const filterButton = page.getByRole('button', { name: filterName }).first(); |
59 | | - const accordionItem = filterButton.locator('xpath=ancestor::*[@data-state]').first(); |
60 | | - const contentRegion = accordionItem.locator('[role="region"]').first(); |
| 21 | +async function clickSpecificFilterOption(page: Page, filterName: string, optionName: string) { |
| 22 | + const filterButton = page |
| 23 | + .getByRole('region', { name: filterName }) |
| 24 | + .getByRole('button', { name: optionName, disabled: false }); |
61 | 25 |
|
62 | | - await expect(contentRegion).toBeVisible(); |
63 | | - |
64 | | - const buttons = contentRegion.locator('button[aria-label]'); |
65 | | - |
66 | | - const count = await buttons.count(); |
67 | | - |
68 | | - // Avoid await in loop by collecting all button data first |
69 | | - const buttonData = await Promise.all( |
70 | | - Array.from({ length: count }, async (_, index) => { |
71 | | - const button = buttons.nth(index); |
72 | | - const [ariaLabel, buttonText, isDisabled, hasDataDisabled] = await Promise.all([ |
73 | | - button.getAttribute('aria-label').catch(() => null), |
74 | | - button.textContent().catch(() => null), |
75 | | - button.isDisabled().catch(() => false), |
76 | | - button.getAttribute('data-disabled').catch(() => null), |
77 | | - ]); |
78 | | - |
79 | | - return { button, ariaLabel, buttonText, isDisabled, hasDataDisabled }; |
80 | | - }), |
81 | | - ); |
82 | | - |
83 | | - const matchingButton = buttonData.find( |
84 | | - ({ ariaLabel, buttonText, isDisabled, hasDataDisabled }) => { |
85 | | - const matchesLabel = ariaLabel?.includes(optionName); |
86 | | - const matchesText = buttonText?.includes(optionName); |
87 | | - |
88 | | - return !isDisabled && !hasDataDisabled && (matchesLabel || matchesText); |
89 | | - }, |
90 | | - ); |
91 | | - |
92 | | - if (matchingButton) { |
93 | | - await matchingButton.button.click(); |
94 | | - await page.waitForLoadState('networkidle'); |
95 | | - |
96 | | - return true; |
97 | | - } |
98 | | - |
99 | | - return false; |
| 26 | + await filterButton.click(); |
100 | 27 | } |
101 | 28 |
|
102 | 29 | test('Blue color filter shows expected product on shop-all page', async ({ page }) => { |
| 30 | + // Arrange |
103 | 31 | await page.goto(SHOP_ALL_URL); |
104 | | - await page.waitForLoadState('networkidle'); |
105 | | - |
106 | | - const filterApplied = await clickSpecificFilterOption(page, FILTER_COLOR, COLOR_BLUE); |
| 32 | + await expect(page.getByRole('heading', { name: 'Shop All 13' })).toBeVisible(); |
107 | 33 |
|
108 | | - expect(filterApplied).toBeTruthy(); |
109 | | - expect(new URL(page.url()).searchParams.get(URL_PARAM_ATTR_COLOR)).toBe(COLOR_BLUE); |
| 34 | + // Act |
| 35 | + await expandFilterIfNeeded(page, 'Color'); |
| 36 | + await clickSpecificFilterOption(page, 'Color', 'Blue'); |
110 | 37 |
|
| 38 | + // Assert |
| 39 | + await expect(page).toHaveURL((url) => url.searchParams.get('attr_Color') === 'Blue'); |
111 | 40 | await expect(page.getByRole('link', { name: PRODUCT_LE_PARFAIT_JAR })).toBeVisible(); |
112 | 41 | }); |
113 | 42 |
|
114 | 43 | test('Brand filter shows correct products', async ({ page }) => { |
115 | 44 | await page.goto(SHOP_ALL_URL); |
116 | | - await page.waitForLoadState('networkidle'); |
| 45 | + await expect(page.getByRole('heading', { name: 'Shop All 13' })).toBeVisible(); |
117 | 46 |
|
118 | | - const heading = page.getByRole('heading', { name: SHOP_ALL_HEADING_PATTERN }); |
119 | | - const initialCountText = await heading.textContent(); |
120 | | - const initialCount = extractNumber(initialCountText); |
121 | | - |
122 | | - const filterApplied = await clickSpecificFilterOption(page, FILTER_BRAND, BRAND_OFS); |
123 | | - |
124 | | - expect(filterApplied).toBeTruthy(); |
125 | | - await page.waitForLoadState('networkidle'); |
126 | | - expect(new URL(page.url()).searchParams.has(URL_PARAM_BRAND)).toBeTruthy(); |
127 | | - |
128 | | - await expect(heading).toContainText(String(EXPECTED_OFS_BRAND_COUNT)); |
129 | | - |
130 | | - const filteredCountText = await heading.textContent(); |
131 | | - const filteredCount = extractNumber(filteredCountText); |
132 | | - |
133 | | - expect(filteredCount).toBeLessThan(initialCount); |
134 | | - expect(filteredCount).toBe(EXPECTED_OFS_BRAND_COUNT); |
| 47 | + await expandFilterIfNeeded(page, 'Brand'); |
| 48 | + await clickSpecificFilterOption(page, 'Brand', 'OFS'); |
135 | 49 |
|
| 50 | + await expect(page).toHaveURL((url) => url.searchParams.has('brand')); |
| 51 | + await expect(page.getByRole('heading', { name: 'Shop All 5' })).toBeVisible(); |
136 | 52 | await expect(page.getByRole('link', { name: PRODUCT_DUSTPAN_BRUSH })).toBeVisible(); |
137 | 53 | await expect(page.getByRole('link', { name: PRODUCT_UTILITY_CADDY })).toBeVisible(); |
138 | 54 | await expect(page.getByRole('link', { name: PRODUCT_LE_PARFAIT_JAR })).toBeVisible(); |
139 | 55 | }); |
140 | 56 |
|
141 | 57 | test('Multiple filters work together (Color + Brand)', async ({ page }) => { |
142 | 58 | await page.goto(SHOP_ALL_URL); |
143 | | - await page.waitForLoadState('networkidle'); |
144 | | - |
145 | | - const heading = page.getByRole('heading', { name: SHOP_ALL_HEADING_PATTERN }); |
146 | | - const initialCountText = await heading.textContent(); |
147 | | - const initialCount = extractNumber(initialCountText); |
148 | | - |
149 | | - const colorFilterApplied = await clickSpecificFilterOption(page, FILTER_COLOR, COLOR_BLUE); |
150 | | - |
151 | | - expect(colorFilterApplied).toBeTruthy(); |
152 | | - await page.waitForLoadState('networkidle'); |
153 | | - expect(new URL(page.url()).searchParams.get(URL_PARAM_ATTR_COLOR)).toBe(COLOR_BLUE); |
154 | | - |
155 | | - await expect(heading).not.toContainText(String(initialCount)); |
156 | | - |
157 | | - const afterColorCountText = await heading.textContent(); |
158 | | - const afterColorCount = extractNumber(afterColorCountText); |
159 | | - |
160 | | - expect(afterColorCount).toBeLessThan(initialCount); |
161 | | - |
162 | | - const brandFilterApplied = await clickSpecificFilterOption(page, FILTER_BRAND, BRAND_OFS); |
| 59 | + await expect(page.getByRole('heading', { name: 'Shop All 13' })).toBeVisible(); |
163 | 60 |
|
164 | | - expect(brandFilterApplied).toBeTruthy(); |
| 61 | + await expandFilterIfNeeded(page, 'Color'); |
| 62 | + await clickSpecificFilterOption(page, 'Color', 'Blue'); |
165 | 63 |
|
166 | | - await page.waitForLoadState('networkidle'); |
| 64 | + await expect(page).toHaveURL((url) => url.searchParams.get('attr_Color') === 'Blue'); |
167 | 65 |
|
168 | | - const urlParams = new URL(page.url()).searchParams; |
169 | | - |
170 | | - expect(urlParams.get(URL_PARAM_ATTR_COLOR)).toBe(COLOR_BLUE); |
171 | | - expect(urlParams.has(URL_PARAM_BRAND)).toBeTruthy(); |
172 | | - |
173 | | - const afterBothCountText = await heading.textContent(); |
174 | | - const afterBothCount = extractNumber(afterBothCountText); |
175 | | - |
176 | | - expect(afterBothCount).toBeLessThanOrEqual(afterColorCount); |
| 66 | + await expandFilterIfNeeded(page, 'Brand'); |
| 67 | + await clickSpecificFilterOption(page, 'Brand', 'OFS'); |
177 | 68 |
|
| 69 | + await expect(page).toHaveURL((url) => { |
| 70 | + const params = url.searchParams; |
| 71 | + return params.get('attr_Color') === 'Blue' && params.has('brand'); |
| 72 | + }); |
178 | 73 | await expect(page.getByRole('link', { name: PRODUCT_LE_PARFAIT_JAR })).toBeVisible(); |
179 | 74 | }); |
180 | 75 |
|
181 | 76 | test('Removing filter restores product list', async ({ page }) => { |
182 | 77 | await page.goto(SHOP_ALL_URL); |
183 | | - await page.waitForLoadState('networkidle'); |
184 | | - |
185 | | - const heading = page.getByRole('heading', { name: SHOP_ALL_HEADING_PATTERN }); |
186 | | - const initialCountText = await heading.textContent(); |
187 | | - const initialCount = extractNumber(initialCountText); |
188 | | - |
189 | | - const filterApplied = await clickSpecificFilterOption(page, FILTER_BRAND, BRAND_OFS); |
190 | | - |
191 | | - expect(filterApplied).toBeTruthy(); |
192 | | - await page.waitForLoadState('networkidle'); |
193 | | - expect(new URL(page.url()).searchParams.has(URL_PARAM_BRAND)).toBeTruthy(); |
194 | | - |
195 | | - await expect(heading).not.toContainText(String(initialCount)); |
196 | | - |
197 | | - const filteredCountText = await heading.textContent(); |
198 | | - const filteredCount = extractNumber(filteredCountText); |
199 | | - |
200 | | - expect(filteredCount).toBeLessThan(initialCount); |
201 | | - |
202 | | - const resetButton = page.getByRole('button', { name: BUTTON_RESET_FILTERS }); |
203 | | - |
204 | | - await resetButton.click(); |
205 | | - await page.waitForLoadState('networkidle'); |
| 78 | + await expect(page.getByRole('heading', { name: 'Shop All 13' })).toBeVisible(); |
206 | 79 |
|
207 | | - expect(new URL(page.url()).searchParams.has(URL_PARAM_BRAND)).toBeFalsy(); |
| 80 | + await expandFilterIfNeeded(page, 'Brand'); |
| 81 | + await clickSpecificFilterOption(page, 'Brand', 'OFS'); |
208 | 82 |
|
209 | | - await expect(heading).toContainText(String(EXPECTED_INITIAL_PRODUCT_COUNT)); |
| 83 | + await expect(page).toHaveURL((url) => url.searchParams.has('brand')); |
| 84 | + await expect(page.getByRole('heading', { name: 'Shop All 5' })).toBeVisible(); |
210 | 85 |
|
211 | | - const restoredCountText = await heading.textContent(); |
212 | | - const restoredCount = extractNumber(restoredCountText); |
| 86 | + await page.getByRole('button', { name: 'Reset filters' }).click(); |
213 | 87 |
|
214 | | - expect(restoredCount).toBe(initialCount); |
| 88 | + await expect(page).toHaveURL((url) => !url.searchParams.has('brand')); |
| 89 | + await expect(page.getByRole('heading', { name: 'Shop All 13' })).toBeVisible(); |
215 | 90 | }); |
0 commit comments