-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
mini-cart.spec.js
266 lines (247 loc) · 8.07 KB
/
mini-cart.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const { test, expect } = require( '@playwright/test' );
const { disableWelcomeModal } = require( '../../utils/editor' );
const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default;
const miniCartPageTitle = `Mini Cart ${ Date.now() }`;
const miniCartPageSlug = miniCartPageTitle.replace( / /gi, '-' ).toLowerCase();
const miniCartButton = '.wc-block-mini-cart__button';
const miniCartBadge = '.wc-block-mini-cart__badge';
const simpleProductName = 'Single Hundred Product';
const simpleProductDesc = 'Lorem ipsum dolor sit amet.';
const singleProductPrice = '100.00';
const singleProductSalePrice = '50.00';
const totalInclusiveTax = +singleProductSalePrice + 5 + 2.5;
let productId, countryTaxId, stateTaxId, shippingZoneId;
test.describe( 'Mini Cart block page', () => {
test.use( { storageState: process.env.ADMINSTATE } );
test.beforeAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
// add product
await api
.post( 'products', {
name: simpleProductName,
description: simpleProductDesc,
type: 'simple',
regular_price: singleProductPrice,
sale_price: singleProductSalePrice,
manage_stock: true,
stock_quantity: 2,
} )
.then( ( response ) => {
productId = response.data.id;
} );
// add tax
await api.put( 'settings/general/woocommerce_calc_taxes', {
value: 'yes',
} );
await api.put( 'settings/tax/woocommerce_tax_display_cart', {
value: 'excl',
} );
await api
.post( 'taxes', {
country: 'US',
state: '*',
cities: '*',
postcodes: '*',
rate: '10',
name: 'Country Tax',
shipping: false,
priority: 1,
} )
.then( ( response ) => {
countryTaxId = response.data.id;
} );
await api
.post( 'taxes', {
country: '*',
state: 'CA',
cities: '*',
postcodes: '*',
rate: '5',
name: 'State Tax',
shipping: false,
priority: 2,
} )
.then( ( response ) => {
stateTaxId = response.data.id;
} );
// add shipping zone, location and method
await api
.post( 'shipping/zones', {
name: 'US Free Shipping',
} )
.then( ( response ) => {
shippingZoneId = response.data.id;
} );
await api.put( `shipping/zones/${ shippingZoneId }/locations`, [
{
code: 'US',
},
] );
await api.post( `shipping/zones/${ shippingZoneId }/methods`, {
method_id: 'free_shipping',
} );
} );
test.afterAll( async ( { baseURL } ) => {
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
await api.post( 'products/batch', {
delete: [ productId ],
} );
await api.put( 'settings/general/woocommerce_calc_taxes', {
value: 'no',
} );
await api.post( 'taxes/batch', {
delete: [ countryTaxId, stateTaxId ],
} );
await api.delete( `shipping/zones/${ shippingZoneId }`, {
force: true,
} );
} );
test( 'can see empty mini cart, add and remove product, increase to max quantity, calculate tax and see redirection', async ( {
page,
baseURL,
context,
} ) => {
// create a new page with mini cart block
await page.goto( 'wp-admin/post-new.php?post_type=page' );
await disableWelcomeModal( { page } );
await page
.getByRole( 'textbox', { name: 'Add title' } )
.fill( miniCartPageTitle );
await page.getByRole( 'button', { name: 'Add default block' } ).click();
await page
.getByRole( 'document', {
name: 'Empty block; start writing or type forward slash to choose a block',
} )
.fill( '/mini' );
await page.keyboard.press( 'Enter' );
await page
.getByRole( 'button', { name: 'Publish', exact: true } )
.click();
await page
.getByRole( 'region', { name: 'Editor publish' } )
.getByRole( 'button', { name: 'Publish', exact: true } )
.click();
await expect(
page.getByText( `${ miniCartPageTitle } is now live.` )
).toBeVisible();
// go to the page to test mini cart
await page.goto( miniCartPageSlug );
await expect(
page.getByRole( 'heading', { name: miniCartPageTitle } )
).toBeVisible();
await page.locator( miniCartButton ).click();
await expect(
page.getByText( 'Your cart is currently empty!' )
).toBeVisible();
await page.getByRole( 'link', { name: 'Start shopping' } ).click();
await expect(
page.getByRole( 'heading', { name: 'Shop' } )
).toBeVisible();
// add product to cart
await page.goto( `/shop/?add-to-cart=${ productId }` );
// go to page with mini cart block and test with the product added
await page.goto( miniCartPageSlug );
await expect( page.locator( miniCartBadge ) ).toContainText( '1' );
await page.locator( miniCartButton ).click();
await expect(
page.getByRole( 'heading', { name: 'Your cart (1 item)' } )
).toBeVisible();
await expect(
page.getByRole( 'link', { name: simpleProductName } )
).toBeVisible();
await expect(
page.getByText( `Save $${ singleProductSalePrice }` )
).toBeVisible();
await expect( page.getByText( simpleProductDesc ) ).toBeVisible();
// increase product quantity to its maximum
await page
.getByRole( 'button' )
.filter( { hasText: '+', exact: true } )
.click();
await expect(
page.getByRole( 'heading', { name: 'Your cart (2 items)' } )
).toBeVisible();
await expect( page.locator( miniCartBadge ) ).toContainText( '2' );
await expect(
page.locator( '.wc-block-components-totals-item__value' )
).toContainText( `$${ singleProductSalePrice * 2 }` );
await expect(
page.getByRole( 'button' ).filter( { hasText: '+', exact: true } )
).toBeDisabled();
await page
.getByRole( 'button' )
.filter( { hasText: 'Remove item' } )
.click();
await expect(
page.getByText( 'Your cart is currently empty!' )
).toBeVisible();
// add product to cart and redirect from mini to regular cart
await page.goto( `/shop/?add-to-cart=${ productId }` );
await page.goto( miniCartPageSlug );
await page.locator( miniCartButton ).click();
await page.getByRole( 'link', { name: 'View my cart' } ).click();
await expect(
page.getByRole( 'heading', { name: 'Cart', exact: true } )
).toBeVisible();
await expect( page.locator( miniCartButton ) ).toBeHidden();
// go to mini cart and test redirection from mini cart to checkout
await page.goto( miniCartPageSlug );
await page.locator( miniCartButton ).click();
await page.getByRole( 'link', { name: 'Go to checkout' } ).click();
await expect(
page.getByRole( 'heading', { name: 'Checkout', exact: true } )
).toBeVisible();
await expect( page.locator( miniCartButton ) ).toBeHidden();
// shopping cart is very sensitive to cookies, so be explicit
await context.clearCookies();
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
// set inlcuding tax prices
await api.put( 'settings/tax/woocommerce_tax_display_cart', {
value: 'incl',
} );
// add product to cart
await page.goto( `/shop/?add-to-cart=${ productId }` );
// go to cart and add shipping details to calculate tax
await page.goto( '/cart/' ); // we will use the old cart for this purpose
await page.locator( '.shipping-calculator-button' ).click();
await page.getByLabel( 'Town / City' ).fill( 'Sacramento' );
await page.getByLabel( 'ZIP Code' ).fill( '96000' );
await page
.getByRole( 'button', { name: 'Update', exact: true } )
.click();
await expect( page.locator( '.woocommerce-info' ) ).toContainText(
'Shipping costs updated.'
);
await page.goto( miniCartPageSlug );
await expect(
page.getByRole( 'heading', { name: miniCartPageTitle } )
).toBeVisible();
await expect( page.locator( miniCartBadge ) ).toContainText( '1' );
await page.locator( miniCartButton ).click();
await expect(
page.locator( '.wc-block-components-totals-item__value' )
).toContainText( `$${ totalInclusiveTax }` );
await page
.getByRole( 'button' )
.filter( { hasText: 'Remove item' } )
.click();
await expect(
page.getByText( 'Your cart is currently empty!' )
).toBeVisible();
} );
} );