Skip to content

Commit 1dde382

Browse files
committed
GH-1765 Removing duplication in test
1 parent b3701c6 commit 1dde382

File tree

1 file changed

+59
-97
lines changed

1 file changed

+59
-97
lines changed

projects/storefrontapp-e2e-cypress/cypress/helpers/product-search-product-type-flow.ts

+59-97
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export const resultsTitle = 'cx-breadcrumb h1';
44

55
export function productTypeFlow(mobile?: string) {
66
cy.server();
7-
cy.route('GET', '/rest/v2/electronics-spa/products/search*').as('query');
7+
8+
createDefaultQueryRoute('query');
9+
810
cy.get('cx-searchbox input').type('sony{enter}');
911

1012
cy.get(resultsTitle).should('contain', '131 results for "sony"');
@@ -14,154 +16,90 @@ export function productTypeFlow(mobile?: string) {
1416
PRODUCT_LISTING.PRODUCTS_PER_PAGE
1517
);
1618

17-
cy.get('cx-product-list-item')
18-
.first()
19-
.should('contain', '10.2 Megapixel D-SLR with Standard Zoom Lens');
19+
checkFirstItem('10.2 Megapixel D-SLR with Standard Zoom Lens');
2020

2121
// Filter by brand
22-
cy.route(
23-
'GET',
24-
'/rest/v2/electronics-spa/products/search?fields=*&query=sony:relevance:brand*'
25-
).as('brand_query');
26-
cy.get('.cx-facet-header')
27-
.contains('Brand')
28-
.parents('.cx-facet-group')
29-
.within(() => {
30-
cy.get('.cx-facet-checkbox')
31-
.first()
32-
.click({ force: true });
33-
});
22+
createQueryRoute('brand', 'brand_query');
23+
24+
clickFacet('Brand');
3425

3526
cy.wait('@brand_query');
3627

3728
cy.get(resultsTitle).should('contain', '86 results for "sony"');
38-
cy.get('cx-product-list-item')
39-
.first()
40-
.should('contain', '10.2 Megapixel D-SLR with Standard Zoom Lens');
4129

42-
cy.route('GET', '/rest/v2/electronics-spa/products/search*').as('query1');
30+
createDefaultQueryRoute('query1');
4331

44-
if (mobile) {
45-
cy.get(
46-
`cx-product-facet-navigation ${mobile} .cx-facet-filter-pill .close:first`
47-
).click({ force: true });
48-
} else {
49-
cy.get(
50-
'cx-product-facet-navigation .cx-facet-filter-container .cx-facet-filter-pill .close:first'
51-
).click({ force: true });
52-
}
32+
clearSelectedFacet(mobile);
5333

5434
cy.wait('@query1');
5535

5636
cy.get(resultsTitle).should('contain', '131 results for "sony"');
5737

5838
// Filter by price
59-
cy.route(
60-
'GET',
61-
'/rest/v2/electronics-spa/products/search?fields=*&query=sony:relevance:price*'
62-
).as('price_query');
39+
createQueryRoute('price', 'price_query');
6340

64-
cy.get('.cx-facet-header')
65-
.contains('Price')
66-
.parents('.cx-facet-group')
67-
.within(() => {
68-
cy.get('.cx-facet-checkbox')
69-
.first()
70-
.click({ force: true });
71-
});
41+
clickFacet('Price');
7242

7343
cy.wait('@price_query')
7444
.its('url')
7545
.should('include', 'sony:relevance:price');
7646

7747
cy.get(resultsTitle).should('contain', '16 results for "sony"');
78-
cy.get('cx-product-list-item')
79-
.first()
80-
.should('contain', 'MSHX8A');
8148

82-
cy.route('GET', '/rest/v2/electronics-spa/products/search*').as('query2');
49+
checkFirstItem('MSHX8A');
8350

84-
if (mobile) {
85-
cy.get(
86-
`cx-product-facet-navigation ${mobile} .cx-facet-filter-pill .close:first`
87-
).click({ force: true });
88-
} else {
89-
cy.get(
90-
'cx-product-facet-navigation .cx-facet-filter-container .cx-facet-filter-pill .close:first'
91-
).click({ force: true });
92-
}
51+
createDefaultQueryRoute('query2');
52+
53+
clearSelectedFacet(mobile);
9354

9455
cy.wait('@query2');
9556

9657
cy.get(resultsTitle).should('contain', '131 results for "sony"');
9758

9859
// Filter by category
99-
cy.route(
100-
'GET',
101-
'/rest/v2/electronics-spa/products/search?fields=*&query=sony:relevance:category*'
102-
).as('category_query');
60+
createQueryRoute('category', 'category_query');
10361

104-
cy.get('.cx-facet-header')
105-
.contains('Category')
106-
.parents('.cx-facet-group')
107-
.within(() => {
108-
cy.get('.cx-facet-checkbox')
109-
.first()
110-
.click({ force: true });
111-
});
62+
clickFacet('Category');
11263

11364
cy.wait('@category_query')
11465
.its('url')
11566
.should('include', 'sony:relevance:category');
11667

11768
cy.get(resultsTitle).should('contain', '95 results for "sony"');
11869

119-
cy.get('cx-product-list-item')
120-
.first()
121-
.should('contain', '10.2 Megapixel D-SLR with Standard Zoom Lens');
70+
checkFirstItem('10.2 Megapixel D-SLR with Standard Zoom Lens');
12271

123-
cy.route('GET', '/rest/v2/electronics-spa/products/search*').as('query3');
72+
createDefaultQueryRoute('query3');
12473

125-
if (mobile) {
126-
cy.get(
127-
`cx-product-facet-navigation ${mobile} .cx-facet-filter-pill .close:first`
128-
).click({ force: true });
129-
} else {
130-
cy.get(
131-
'cx-product-facet-navigation .cx-facet-filter-pill .close:first'
132-
).click({ force: true });
133-
}
74+
clearSelectedFacet(mobile);
13475

13576
cy.wait('@query3');
13677

13778
cy.get(resultsTitle).should('contain', '131 results for "sony"');
13879

13980
// Filter by color
140-
cy.route(
141-
'GET',
142-
'/rest/v2/electronics-spa/products/search?fields=*&query=sony:relevance:Colour*'
143-
).as('color_query');
81+
createQueryRoute('Colour', 'color_query');
14482

145-
cy.get('.cx-facet-header')
146-
.contains('Color')
147-
.parents('.cx-facet-group')
148-
.within(() => {
149-
cy.get('.cx-facet-checkbox')
150-
.first()
151-
.click({ force: true });
152-
});
83+
clickFacet('Color');
15384

15485
cy.wait('@color_query')
15586
.its('url')
15687
.should('include', 'sony:relevance:Colour');
15788

15889
cy.get(resultsTitle).should('contain', '7 results for "sony"');
159-
cy.get('cx-product-list-item')
160-
.first()
161-
.should('contain', 'NP-FV 70');
16290

163-
cy.route('GET', '/rest/v2/electronics-spa/products/search*').as('query4');
91+
checkFirstItem('NP-FV 70');
92+
93+
createDefaultQueryRoute('query4');
16494

95+
clearSelectedFacet(mobile);
96+
97+
cy.wait('@query4');
98+
99+
cy.get(resultsTitle).should('contain', '131 results for "sony"');
100+
}
101+
102+
function clearSelectedFacet(mobile: string) {
165103
if (mobile) {
166104
cy.get(
167105
`cx-product-facet-navigation ${mobile} .cx-facet-filter-pill .close:first`
@@ -171,8 +109,32 @@ export function productTypeFlow(mobile?: string) {
171109
'cx-product-facet-navigation .cx-facet-filter-container .cx-facet-filter-pill .close:first'
172110
).click({ force: true });
173111
}
112+
}
174113

175-
cy.wait('@query4');
114+
function clickFacet(header: string) {
115+
cy.get('.cx-facet-header')
116+
.contains(header)
117+
.parents('.cx-facet-group')
118+
.within(() => {
119+
cy.get('.cx-facet-checkbox')
120+
.first()
121+
.click({ force: true });
122+
});
123+
}
176124

177-
cy.get(resultsTitle).should('contain', '131 results for "sony"');
125+
function checkFirstItem(title: string): void {
126+
cy.get('cx-product-list-item')
127+
.first()
128+
.should('contain', title);
129+
}
130+
131+
function createDefaultQueryRoute(alias: string): void {
132+
cy.route('GET', '/rest/v2/electronics-spa/products/search*').as(alias);
133+
}
134+
135+
function createQueryRoute(param: string, alias: string): void {
136+
cy.route(
137+
'GET',
138+
`/rest/v2/electronics-spa/products/search?fields=*&query=sony:relevance:${param}*`
139+
).as(alias);
178140
}

0 commit comments

Comments
 (0)