Skip to content

Commit 9358956

Browse files
itsjustrileyclaude
andcommitted
refactor(e2e): extract variant selector utils from generic CartUtil
Move waitForOptionSelectors and selectDifferentOption into a dedicated CustomCartMethodUtil class. These methods are only used by the custom-cart-method recipe tests and don't belong in the generic cart utility, which caused confusion about whether they were shared helpers. Addresses fredericoo's review feedback on PR #3546. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7981757 commit 9358956

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

e2e/fixtures/cart-utils.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,10 @@
11
import {expect, Locator, Page} from '@playwright/test';
2-
import assert from './assertions';
32

43
const CART_ID_PREFIX = 'gid://shopify/Cart/';
54

65
export class CartUtil {
76
constructor(private page: Page) {}
87

9-
async waitForOptionSelectors(lineItem: Locator) {
10-
const optionSelectors = lineItem.getByRole('combobox');
11-
await expect(optionSelectors.first()).toBeVisible();
12-
return optionSelectors;
13-
}
14-
15-
async selectDifferentOption(optionSelect: Locator) {
16-
const optionName = await optionSelect.getAttribute('name');
17-
if (!optionName) {
18-
throw new Error('Expected option select to have a name attribute');
19-
}
20-
21-
const initialValue = await optionSelect.inputValue();
22-
const enabledOptionValues = await optionSelect.evaluate(
23-
(el: HTMLSelectElement) =>
24-
Array.from(el.options)
25-
.filter((option) => !option.disabled && option.value.trim() !== '')
26-
.map((option) => option.value),
27-
);
28-
29-
expect(enabledOptionValues.length).toBeGreaterThan(1);
30-
31-
const nextValue = enabledOptionValues.find(
32-
(value) => value !== initialValue,
33-
);
34-
assert(
35-
nextValue,
36-
'Expected option select to have at least two different values',
37-
);
38-
39-
await optionSelect.selectOption(nextValue);
40-
await expect(optionSelect).toHaveValue(nextValue);
41-
42-
return {optionName, nextValue};
43-
}
44-
458
async addItem(productName: string) {
469
await expect(
4710
this.page.getByRole('heading', {level: 1, name: productName}),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {expect, Locator} from '@playwright/test';
2+
3+
export class CustomCartMethodUtil {
4+
async waitForOptionSelectors(lineItem: Locator) {
5+
const optionSelectors = lineItem.getByRole('combobox');
6+
await expect(optionSelectors.first()).toBeVisible();
7+
return optionSelectors;
8+
}
9+
10+
async selectDifferentOption(optionSelect: Locator) {
11+
const optionName = await optionSelect.getAttribute('name');
12+
if (!optionName) {
13+
throw new Error('Expected option select to have a name attribute');
14+
}
15+
16+
const initialValue = await optionSelect.inputValue();
17+
const enabledOptionValues = await optionSelect.evaluate((element) => {
18+
const selectElement = element as HTMLSelectElement;
19+
return Array.from(selectElement.options)
20+
.filter((option) => !option.disabled && option.value.trim() !== '')
21+
.map((option) => option.value);
22+
});
23+
24+
expect(enabledOptionValues.length).toBeGreaterThan(1);
25+
26+
const nextValue = enabledOptionValues.find(
27+
(value) => value !== initialValue,
28+
);
29+
if (!nextValue) {
30+
throw new Error(
31+
'Expected option select to have at least two different values',
32+
);
33+
}
34+
35+
await optionSelect.selectOption(nextValue);
36+
await expect(optionSelect).toHaveValue(nextValue);
37+
38+
return {optionName, nextValue};
39+
}
40+
}

e2e/specs/recipes/custom-cart-method.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {test, expect, setRecipeFixture} from '../../fixtures';
22
import assert from '../../fixtures/assertions';
33
import {CartUtil} from '../../fixtures/cart-utils';
4+
import {CustomCartMethodUtil} from '../../fixtures/custom-cart-method-utils';
45

56
setRecipeFixture({
67
recipeName: 'custom-cart-method',
@@ -23,6 +24,8 @@ const KNOWN_PRODUCT_WITH_VARIANTS = {
2324
name: 'The Ascend',
2425
} as const;
2526

27+
const variantSelector = new CustomCartMethodUtil();
28+
2629
test.describe('Custom Cart Method Recipe', () => {
2730
test.describe('Cart Line Item Variant Selector', () => {
2831
test.beforeEach(async ({page}) => {
@@ -54,7 +57,8 @@ test.describe('Custom Cart Method Recipe', () => {
5457
await expect(cart.getLineItems()).toHaveCount(1);
5558

5659
const firstLineItem = cart.getFirstLineItem();
57-
const optionSelects = await cart.waitForOptionSelectors(firstLineItem);
60+
const optionSelects =
61+
await variantSelector.waitForOptionSelectors(firstLineItem);
5862
const minimumExpectedOptions = 2;
5963
expect(await optionSelects.count()).toBeGreaterThanOrEqual(
6064
minimumExpectedOptions,
@@ -74,10 +78,10 @@ test.describe('Custom Cart Method Recipe', () => {
7478
assert(initialUrl);
7579

7680
const optionSelect = (
77-
await cart.waitForOptionSelectors(firstLineItem)
81+
await variantSelector.waitForOptionSelectors(firstLineItem)
7882
).first();
7983
const {optionName, nextValue} =
80-
await cart.selectDifferentOption(optionSelect);
84+
await variantSelector.selectDifferentOption(optionSelect);
8185

8286
const href = await productLink.getAttribute('href');
8387
assert(href);
@@ -105,9 +109,9 @@ test.describe('Custom Cart Method Recipe', () => {
105109
const initialUrl = await productLink.getAttribute('href');
106110

107111
const optionSelect = (
108-
await cart.waitForOptionSelectors(firstLineItem)
112+
await variantSelector.waitForOptionSelectors(firstLineItem)
109113
).first();
110-
await cart.selectDifferentOption(optionSelect);
114+
await variantSelector.selectDifferentOption(optionSelect);
111115

112116
// Wait for the cart update to complete before checking preservation
113117
const href = await productLink.getAttribute('href');
@@ -132,9 +136,9 @@ test.describe('Custom Cart Method Recipe', () => {
132136
const initialProductUrl = await productLink.getAttribute('href');
133137

134138
const optionSelect = (
135-
await cart.waitForOptionSelectors(firstLineItem)
139+
await variantSelector.waitForOptionSelectors(firstLineItem)
136140
).first();
137-
await cart.selectDifferentOption(optionSelect);
141+
await variantSelector.selectDifferentOption(optionSelect);
138142

139143
// Verify the cart update completed without navigating away
140144
const href = await productLink.getAttribute('href');

0 commit comments

Comments
 (0)