-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.spec.ts
161 lines (139 loc) · 4.23 KB
/
checkout.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
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
import { expect, test, Page } from '@playwright/test';
import { format } from 'date-fns';
type BookStrings = {
authors: string;
isbn13: string;
price: string;
title: string;
};
type OrderTotal = {
subTotal: string;
tax: string;
total: string;
};
async function addBookToOrder({
book,
page,
}: {
book: BookStrings;
page: Page;
}) {
// enter in an ISBN and press enter
const skuInput = page.getByPlaceholder('Scan or Enter SKU');
await skuInput.fill(book.isbn13);
await skuInput.press('Enter');
await expect(page.getByText('Pay Now')).toBeVisible();
// verify the row added to the table
const row = page.getByRole('row').nth(1);
await expect(row).toHaveText(
`${book.isbn13}${book.title}$${book.price}1$${book.price}`,
);
}
async function verifyOrderTotal({
subTotal,
tax,
total,
page,
}: OrderTotal & {
page: Page;
}) {
const orderTotal = page.getByTestId('order-total');
await expect(orderTotal).toHaveText(
`Subtotal:$${subTotal}Tax:$${tax}Total:$${total}`,
);
}
async function payNowAndVerifyOrder({
subTotal,
tax,
total,
page,
}: OrderTotal & {
page: Page;
}) {
await page.getByText('Pay Now').click();
// verify the waiting state
await expect(page.getByText('Awaiting transaction')).toBeVisible();
await expect(page.getByText(`Total: $${total}`)).toBeVisible();
await expect(
page.getByRole('button', { name: 'Cancel transaction' }),
).toBeVisible();
// wait for the order to complete, and verify the complete state
await expect(page.getByText('Checkout complete!')).toBeVisible();
await expect(page.getByText(`Total: $${total}`)).toBeVisible();
await expect(
page.getByRole('button', { name: 'Start New Checkout' }),
).toBeVisible();
const orderId = await page.getByTestId('order-uid').textContent();
// navigate to the orders page
await page.getByRole('navigation').getByText('Orders').click();
await expect(page.getByRole('heading')).toHaveText('Orders');
// verify the row added to the table
const row = page.getByRole('row').nth(1);
await expect(row).toHaveText(
`${orderId}Paid${format(new Date(), 'M/d/yyyy')}1$${subTotal}$${tax}$${total}`,
);
}
async function verifyBookQuantity({
book,
quantity,
page,
}: {
book: BookStrings;
quantity: number;
page: Page;
}) {
await page.getByRole('navigation').getByText('Search').click();
await page.getByTestId('search-command-input').fill(book.title);
await page.getByTestId('search-command-input').press('Enter');
await page.getByText(`${book.title} by ${book.authors}`).click();
const bookComponent = page.getByTestId(book.isbn13);
await expect(bookComponent.getByText(book.title)).toBeVisible();
await expect(bookComponent.getByText(`ISBN:${book.isbn13}`)).toBeVisible();
await expect(bookComponent.getByText(`By:${book.authors}`)).toBeVisible();
await expect(bookComponent.getByText(`Quantity:${quantity}`)).toBeVisible();
}
/**
* This test assumes inventory exists for the book described below, which
* should be seeded via the CI seed script.
*
* This test assumes Square checkout is being run in sandbox mode, and that
* the sandbox mode is setup for a successful transaction for orders < $25.
*/
test.describe('checkout', () => {
test('checkout book with inventory, and successful square transaction', async ({
page,
}) => {
const book1: BookStrings = {
authors: 'Sarah J. Maas',
isbn13: '9781635575583',
price: '19.99',
title: 'A Court of Mist and Fury',
};
const orderTotal: OrderTotal = {
subTotal: '19.99',
tax: '1.65',
total: '21.64',
};
// we have 5 to start, and we're buying 1, so we'll have 4 remaining
const QUANTITY_REMAINING = 4;
await page.goto('/');
await expect(page.getByRole('heading')).toHaveText('Bookstore');
// navigate to the checkout page
await page.getByRole('navigation').getByText('Checkout').click();
await expect(page.getByRole('heading')).toHaveText('Checkout');
await addBookToOrder({ book: book1, page });
await verifyOrderTotal({
page,
...orderTotal,
});
await payNowAndVerifyOrder({
page,
...orderTotal,
});
await verifyBookQuantity({
book: book1,
page,
quantity: QUANTITY_REMAINING,
});
});
});