forked from microsoft/contosotraders-cloudtesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.spec.ts
80 lines (72 loc) · 2.37 KB
/
cart.spec.ts
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
import { test, expect } from '@playwright/test';
// Shopping cart data
const USER = '[email protected]';
const PRODUCT = 'Xbox Wireless Controller Lunar Shift Special Edition';
const PRODUCTID = 1;
const PRODUCTIMAGE = 'PID1-1.jpg';
const PRODUCTPRICE = 99;
const PRODUCTQUANTITY = 1;
test.skip(() => !process.env.REACT_APP_APIURLSHOPPINGCART, 'requires REACT_APP_APIURLSHOPPINGCART');
test.use({
baseURL: process.env.REACT_APP_APIURLSHOPPINGCART + '/'
});
// SETUP: Create a new cart
test.beforeAll(async ({ request }) => {
const newCart = await request.post('./ShoppingCart', {
data: {
cartItemId: "string",
email: USER,
productId: PRODUCTID,
name: PRODUCT,
price: PRODUCTPRICE,
imageUrl: PRODUCTIMAGE,
quantity: PRODUCTQUANTITY
}
});
expect(newCart.status()).toBe(201);
});
test.describe('Shopping Cart API', () => {
test('should be able to GET shopping cart', async ({ request }) => {
const cart = await request.get('./ShoppingCart', {
headers: {
'Accept': 'accept: */*',
'x-tt-email': USER,
}
});
await expect(cart).toBeOK();
expect(await cart.json()).toContainEqual(expect.objectContaining({
email: USER,
productId: PRODUCTID,
name: PRODUCT,
price: PRODUCTPRICE,
imageUrl: PRODUCTIMAGE,
quantity: PRODUCTQUANTITY
}));
});
});
// TEARDOWN: Delete the cart
test.afterAll(async ({ request }) => {
const cart = await request.get('./ShoppingCart', {
headers: {
'Accept': 'accept: */*',
'x-tt-email': USER,
}
});
await expect(cart).toBeOK();
// Loop through each cart item and delete it
const cartBody = JSON.parse(await cart.text());
for (let i = 0; i < cartBody.length; i++) {
const deleteCart = await request.delete('./ShoppingCart/product', {
data: {
cartItemId: cartBody[i].cartItemId,
email: USER,
productId: cartBody[i].productId,
name: cartBody[i].name,
price: cartBody[i].price,
imageUrl: cartBody[i].imageUrl,
quantity: cartBody[i].quantity
}
});
await expect(deleteCart).toBeOK();
}
});