nextjs-starter-medusa
526 строк · 22.1 Кб
1import { seedDiscount, seedUser } from "../../data/seed"
2import { test, expect } from "../../index"
3
4test.describe("Discount tests", async () => {
5let discount = {
6id: "",
7code: "",
8rule_id: "",
9amount: 0,
10}
11test.beforeEach(async () => {
12discount = await seedDiscount()
13})
14
15test("Make sure discount works during transaction", async ({
16cartPage,
17checkoutPage,
18orderPage,
19productPage,
20storePage,
21}) => {
22let cartSubtotal = 0
23await test.step("Go through purchasing process, upto the cart page", async () => {
24await test.step("Navigate to a product page", async () => {
25await storePage.goto()
26const product = await storePage.getProduct("Sweatshirt")
27await product.locator.click()
28await productPage.container.waitFor({ state: "visible" })
29})
30
31await test.step("Add the product to the cart and goto checkout", async () => {
32await productPage.selectOption("M")
33await productPage.clickAddProduct()
34await productPage.cartDropdown.navCartLink.click()
35await productPage.cartDropdown.goToCartButton.click()
36await cartPage.container.waitFor({ state: "visible" })
37await cartPage.cartDropdown.close()
38cartSubtotal = Number(
39(await cartPage.cartTotal.getAttribute("data-value")) || ""
40)
41})
42await test.step("Navigate to the checkout page", async () => {
43await cartPage.checkoutButton.click()
44await checkoutPage.container.waitFor({ state: "visible" })
45})
46})
47
48await test.step("Enter in the discount and assert value works", async () => {
49await checkoutPage.discountButton.click()
50await expect(checkoutPage.discountInput).toBeVisible()
51await checkoutPage.discountInput.fill(discount.code)
52await checkoutPage.discountApplyButton.click()
53const paymentDiscount = await checkoutPage.getDiscount(discount.code)
54await expect(paymentDiscount.locator).toBeVisible()
55await expect(paymentDiscount.code).toHaveText(discount.code)
56expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
57expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
58(cartSubtotal - discount.amount).toString()
59)
60})
61
62let shippingTotal = 0
63await test.step("Go through checkout process", async () => {
64await test.step("Enter in the first step of the checkout process", async () => {
65await test.step("Enter in the shipping address info", async () => {
66await checkoutPage.shippingFirstNameInput.fill("First")
67await checkoutPage.shippingLastNameInput.fill("Last")
68await checkoutPage.shippingCompanyInput.fill("MyCorp")
69await checkoutPage.shippingAddressInput.fill("123 Fake street")
70await checkoutPage.shippingPostalCodeInput.fill("80010")
71await checkoutPage.shippingCityInput.fill("Denver")
72await checkoutPage.shippingProvinceInput.fill("Colorado")
73await checkoutPage.shippingCountrySelect.selectOption("United States")
74})
75
76await test.step("Enter in the contact info and open the billing info form", async () => {
77await checkoutPage.shippingEmailInput.fill("test@example.com")
78await checkoutPage.shippingPhoneInput.fill("3031112222")
79await checkoutPage.submitAddressButton.click()
80})
81})
82
83await test.step("Complete the rest of the payment process", async () => {
84await checkoutPage.selectDeliveryOption("FakeEx Standard")
85await checkoutPage.submitDeliveryOptionButton.click()
86shippingTotal = Number(
87(await checkoutPage.cartShipping.getAttribute("data-value")) || "0"
88)
89await checkoutPage.submitPaymentButton.click()
90})
91
92await test.step("Make sure the cart total is the expected value after selecting shipping", async () => {
93expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
94(cartSubtotal - discount.amount + shippingTotal).toString()
95)
96})
97
98await test.step("Finish completing the order", async () => {
99await checkoutPage.submitOrderButton.click()
100await orderPage.container.waitFor({ state: "visible" })
101})
102})
103const cartTotal = Number(cartSubtotal) + Number(shippingTotal)
104
105await test.step("Assert the order page shows the total was 0", async () => {
106expect(await orderPage.cartTotal.getAttribute("data-value")).toBe(
107(cartTotal - discount.amount).toString()
108)
109expect(await orderPage.cartSubtotal.getAttribute("data-value")).toBe(
110cartSubtotal.toString()
111)
112expect(await orderPage.cartDiscount.getAttribute("data-value")).toBe(
113discount.amount.toString()
114)
115})
116})
117
118test("Make sure discount can be used when entered in from cart", async ({
119cartPage,
120checkoutPage,
121orderPage,
122productPage,
123storePage,
124}) => {
125let cartSubtotal = 0
126await test.step("Go through purchasing process, upto the cart page", async () => {
127await test.step("Navigate to a product page", async () => {
128await storePage.goto()
129const product = await storePage.getProduct("Sweatshirt")
130await product.locator.click()
131await productPage.container.waitFor({ state: "visible" })
132})
133
134await test.step("Add the product to the cart and goto checkout", async () => {
135await productPage.selectOption("M")
136await productPage.clickAddProduct()
137await productPage.cartDropdown.navCartLink.click()
138await productPage.cartDropdown.goToCartButton.click()
139await cartPage.container.waitFor({ state: "visible" })
140await cartPage.cartDropdown.close()
141cartSubtotal = Number(
142(await cartPage.cartTotal.getAttribute("data-value")) || ""
143)
144})
145})
146
147await test.step("Enter in the discount and assert value works", async () => {
148await cartPage.discountButton.click()
149await expect(cartPage.discountInput).toBeVisible()
150await cartPage.discountInput.fill(discount.code)
151await cartPage.discountApplyButton.click()
152const paymentDiscount = await cartPage.getDiscount(discount.code)
153await expect(paymentDiscount.locator).toBeVisible()
154await expect(paymentDiscount.code).toHaveText(discount.code)
155expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
156expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
157(cartSubtotal - discount.amount).toString()
158)
159})
160
161await test.step("Go to checkout and assert the value is still discounted", async () => {
162await cartPage.checkoutButton.click()
163await checkoutPage.container.waitFor({ state: "visible" })
164expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
165(cartSubtotal - discount.amount).toString()
166)
167})
168
169let shippingTotal = 0
170await test.step("Go through checkout process", async () => {
171await test.step("Enter in the first step of the checkout process", async () => {
172await test.step("Enter in the shipping address info", async () => {
173await checkoutPage.shippingFirstNameInput.fill("First")
174await checkoutPage.shippingLastNameInput.fill("Last")
175await checkoutPage.shippingCompanyInput.fill("MyCorp")
176await checkoutPage.shippingAddressInput.fill("123 Fake street")
177await checkoutPage.shippingPostalCodeInput.fill("80010")
178await checkoutPage.shippingCityInput.fill("Denver")
179await checkoutPage.shippingProvinceInput.fill("Colorado")
180await checkoutPage.shippingCountrySelect.selectOption("United States")
181})
182
183await test.step("Enter in the contact info and open the billing info form", async () => {
184await checkoutPage.shippingEmailInput.fill("test@example.com")
185await checkoutPage.shippingPhoneInput.fill("3031112222")
186await checkoutPage.submitAddressButton.click()
187})
188})
189
190await test.step("Complete the rest of the payment process", async () => {
191await checkoutPage.selectDeliveryOption("FakeEx Standard")
192await checkoutPage.submitDeliveryOptionButton.click()
193shippingTotal = Number(
194(await checkoutPage.cartShipping.getAttribute("data-value")) || "0"
195)
196await checkoutPage.submitPaymentButton.click()
197await checkoutPage.submitOrderButton.click()
198await orderPage.container.waitFor({ state: "visible" })
199})
200})
201const cartTotal = Number(cartSubtotal) + Number(shippingTotal)
202
203await test.step("Assert the order page shows the total was 0", async () => {
204expect(await orderPage.cartTotal.getAttribute("data-value")).toBe(
205(cartTotal - discount.amount).toString()
206)
207expect(await orderPage.cartSubtotal.getAttribute("data-value")).toBe(
208cartSubtotal.toString()
209)
210expect(await orderPage.cartDiscount.getAttribute("data-value")).toBe(
211discount.amount.toString()
212)
213})
214})
215
216test("Ensure adding and removing a discout does not impact checkout amount", async ({
217cartPage,
218checkoutPage,
219orderPage,
220productPage,
221storePage,
222}) => {
223let cartSubtotal = 0
224await test.step("Go through purchasing process, upto the cart page", async () => {
225await test.step("Navigate to a product page", async () => {
226await storePage.goto()
227const product = await storePage.getProduct("Sweatshirt")
228await product.locator.click()
229await productPage.container.waitFor({ state: "visible" })
230})
231
232await test.step("Add the product to the cart and goto checkout", async () => {
233await productPage.selectOption("M")
234await productPage.clickAddProduct()
235await productPage.cartDropdown.navCartLink.click()
236await productPage.cartDropdown.goToCartButton.click()
237await cartPage.container.waitFor({ state: "visible" })
238await cartPage.cartDropdown.close()
239cartSubtotal = Number(
240(await cartPage.cartTotal.getAttribute("data-value")) || ""
241)
242})
243})
244
245await test.step("Enter in the discount and assert value works", async () => {
246await cartPage.discountButton.click()
247await expect(cartPage.discountInput).toBeVisible()
248await cartPage.discountInput.fill(discount.code)
249await cartPage.discountApplyButton.click()
250const paymentDiscount = await cartPage.getDiscount(discount.code)
251await expect(paymentDiscount.locator).toBeVisible()
252await expect(paymentDiscount.code).toHaveText(discount.code)
253expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
254expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
255(cartSubtotal - discount.amount).toString()
256)
257})
258
259await test.step("Go to checkout and assert the value is still discounted", async () => {
260await cartPage.checkoutButton.click()
261await checkoutPage.container.waitFor({ state: "visible" })
262expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
263(cartSubtotal - discount.amount).toString()
264)
265const paymentDiscount = await checkoutPage.getDiscount(discount.code)
266await paymentDiscount.removeButton.click()
267await expect(paymentDiscount.locator).not.toBeVisible()
268expect(await checkoutPage.cartTotal.getAttribute("data-value")).not.toBe(
269(cartSubtotal - discount.amount).toString()
270)
271})
272
273let shippingTotal = ""
274await test.step("Go through checkout process", async () => {
275await test.step("Enter in the first step of the checkout process", async () => {
276await test.step("Enter in the shipping address info", async () => {
277await checkoutPage.shippingFirstNameInput.fill("First")
278await checkoutPage.shippingLastNameInput.fill("Last")
279await checkoutPage.shippingCompanyInput.fill("MyCorp")
280await checkoutPage.shippingAddressInput.fill("123 Fake street")
281await checkoutPage.shippingPostalCodeInput.fill("80010")
282await checkoutPage.shippingCityInput.fill("Denver")
283await checkoutPage.shippingProvinceInput.fill("Colorado")
284await checkoutPage.shippingCountrySelect.selectOption("United States")
285})
286
287await test.step("Enter in the contact info and open the billing info form", async () => {
288await checkoutPage.shippingEmailInput.fill("test@example.com")
289await checkoutPage.shippingPhoneInput.fill("3031112222")
290await checkoutPage.submitAddressButton.click()
291})
292})
293
294await test.step("Complete the rest of the payment process", async () => {
295await checkoutPage.selectDeliveryOption("FakeEx Standard")
296await checkoutPage.submitDeliveryOptionButton.click()
297shippingTotal =
298(await checkoutPage.cartShipping.getAttribute("data-value")) || ""
299await checkoutPage.submitPaymentButton.click()
300await checkoutPage.submitOrderButton.click()
301await orderPage.container.waitFor({ state: "visible" })
302})
303})
304const cartTotal = (Number(cartSubtotal) + Number(shippingTotal)).toString()
305
306await test.step("Assert the order page shows the total was not discounted", async () => {
307expect(await orderPage.cartTotal.getAttribute("data-value")).toBe(
308cartTotal
309)
310expect(await orderPage.cartSubtotal.getAttribute("data-value")).toBe(
311cartSubtotal.toString()
312)
313})
314})
315
316test("Make sure a fake discount displays an error message on the cart page", async ({
317cartPage,
318productPage,
319storePage,
320}) => {
321await test.step("Go through purchasing process, upto the cart page", async () => {
322await test.step("Navigate to a product page", async () => {
323await storePage.goto()
324const product = await storePage.getProduct("Sweatshirt")
325await product.locator.click()
326await productPage.container.waitFor({ state: "visible" })
327})
328
329await test.step("Add the product to the cart and goto checkout", async () => {
330await productPage.selectOption("M")
331await productPage.clickAddProduct()
332await productPage.cartDropdown.navCartLink.click()
333await productPage.cartDropdown.goToCartButton.click()
334await cartPage.container.waitFor({ state: "visible" })
335await cartPage.cartDropdown.close()
336})
337})
338await test.step("Enter in the fake discount", async () => {
339await cartPage.discountButton.click()
340await expect(cartPage.discountInput).toBeVisible()
341await cartPage.discountInput.fill("__FAKE_DISCOUNT_DNE_1111111")
342await cartPage.discountApplyButton.click()
343await expect(cartPage.discountErrorMessage).toBeVisible()
344})
345})
346
347test("Make sure a fake discount displays an error message on the checkout page", async ({
348cartPage,
349checkoutPage,
350productPage,
351storePage,
352}) => {
353await test.step("Go through purchasing process, upto the cart page", async () => {
354await test.step("Navigate to a product page", async () => {
355await storePage.goto()
356const product = await storePage.getProduct("Sweatshirt")
357await product.locator.highlight()
358await product.locator.click()
359await productPage.container.waitFor({ state: "visible" })
360})
361
362await test.step("Add the product to the cart and goto checkout", async () => {
363await productPage.selectOption("M")
364await productPage.clickAddProduct()
365await productPage.cartDropdown.navCartLink.click()
366await productPage.cartDropdown.goToCartButton.click()
367await cartPage.container.waitFor({ state: "visible" })
368await cartPage.cartDropdown.close()
369await cartPage.checkoutButton.click()
370await checkoutPage.container.waitFor({ state: "visible" })
371})
372})
373await test.step("Enter in the fake discount", async () => {
374await checkoutPage.discountButton.click()
375await expect(checkoutPage.discountInput).toBeVisible()
376await checkoutPage.discountInput.fill("__FAKE_DISCOUNT_DNE_1111111")
377await checkoutPage.discountApplyButton.click()
378await expect(checkoutPage.discountErrorMessage).toBeVisible()
379})
380})
381
382test("Adding a discount and then accessing the cart at a later point keeps the discount amount", async ({
383cartPage,
384checkoutPage,
385productPage,
386storePage,
387}) => {
388let cartSubtotal = 0
389await test.step("Go through purchasing process, upto the cart page", async () => {
390await test.step("Navigate to a product page", async () => {
391await storePage.goto()
392const product = await storePage.getProduct("Sweatshirt")
393await product.locator.click()
394await productPage.container.waitFor({ state: "visible" })
395})
396
397await test.step("Add the product to the cart and goto checkout", async () => {
398await productPage.selectOption("M")
399await productPage.clickAddProduct()
400await productPage.cartDropdown.navCartLink.click()
401await productPage.cartDropdown.goToCartButton.click()
402await cartPage.container.waitFor({ state: "visible" })
403await cartPage.cartDropdown.close()
404cartSubtotal = Number(
405(await cartPage.cartTotal.getAttribute("data-value")) || ""
406)
407})
408})
409
410await test.step("Enter in the giftcard and assert value works", async () => {
411await cartPage.discountButton.click()
412await cartPage.discountInput.fill(discount.code)
413await cartPage.discountApplyButton.click()
414const paymentDiscount = await cartPage.getDiscount(discount.code)
415expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
416expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
417(cartSubtotal - discount.amount).toString()
418)
419})
420
421await test.step("Navigate away from the cart page and return to it", async () => {
422await storePage.goto()
423const product = await storePage.getProduct("Sweatpants")
424await product.locator.click()
425await productPage.container.waitFor({ state: "visible" })
426await cartPage.goto()
427await cartPage.cartDropdown.close()
428})
429
430await test.step("Verify the giftcard is still on the cart page", async () => {
431const paymentDiscount = await cartPage.getDiscount(discount.code)
432await expect(paymentDiscount.locator).toBeVisible()
433await expect(paymentDiscount.code).toContainText(discount.code)
434expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
435(cartSubtotal - discount.amount).toString()
436)
437})
438
439await test.step("Verify the giftcard is still on the checkout page", async () => {
440await cartPage.checkoutButton.click()
441await checkoutPage.container.waitFor({ state: "visible" })
442const paymentDiscount = await checkoutPage.getDiscount(discount.code)
443await expect(paymentDiscount.locator).toBeVisible()
444await expect(paymentDiscount.code).toContainText(discount.code)
445expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
446(cartSubtotal - discount.amount).toString()
447)
448expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
449})
450})
451
452test("Adding a discount and then adding another item to the cart keeps the discount", async ({
453cartPage,
454checkoutPage,
455productPage,
456storePage,
457}) => {
458let cartSubtotal = 0
459await test.step("Go through purchasing process, upto the cart page", async () => {
460await test.step("Navigate to a product page", async () => {
461await storePage.goto()
462const product = await storePage.getProduct("Sweatshirt")
463await product.locator.click()
464await productPage.container.waitFor({ state: "visible" })
465})
466
467await test.step("Add the product to the cart and goto checkout", async () => {
468await productPage.selectOption("M")
469await productPage.clickAddProduct()
470await productPage.cartDropdown.navCartLink.click()
471await productPage.cartDropdown.goToCartButton.click()
472await cartPage.container.waitFor({ state: "visible" })
473await cartPage.cartDropdown.close()
474cartSubtotal = Number(
475(await cartPage.cartTotal.getAttribute("data-value")) || ""
476)
477})
478})
479
480await test.step("Enter in the giftcard and assert value works", async () => {
481await cartPage.discountButton.click()
482await cartPage.discountInput.fill(discount.code)
483await cartPage.discountApplyButton.click()
484const paymentDiscount = await cartPage.getDiscount(discount.code)
485expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
486expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
487(cartSubtotal - discount.amount).toString()
488)
489})
490
491await test.step("Navigate away from the cart page and return to it", async () => {
492await storePage.goto()
493const product = await storePage.getProduct("Sweatpants")
494await product.locator.click()
495await productPage.container.waitFor({ state: "visible" })
496await productPage.selectOption("XL")
497await productPage.clickAddProduct()
498await productPage.cartDropdown.close()
499await cartPage.goto()
500cartSubtotal = Number(
501(await cartPage.cartSubtotal.getAttribute("data-value")) || ""
502)
503})
504
505await test.step("Verify the giftcard is still on the cart page", async () => {
506const paymentDiscount = await cartPage.getDiscount(discount.code)
507await expect(paymentDiscount.locator).toBeVisible()
508await expect(paymentDiscount.code).toContainText(discount.code)
509expect(await cartPage.cartTotal.getAttribute("data-value")).toBe(
510(cartSubtotal - discount.amount).toString()
511)
512})
513
514await test.step("Verify the giftcard is still on the checkout page", async () => {
515await cartPage.checkoutButton.click()
516await checkoutPage.container.waitFor({ state: "visible" })
517const paymentDiscount = await checkoutPage.getDiscount(discount.code)
518await expect(paymentDiscount.locator).toBeVisible()
519await expect(paymentDiscount.code).toContainText(discount.code)
520expect(await checkoutPage.cartTotal.getAttribute("data-value")).toBe(
521(cartSubtotal - discount.amount).toString()
522)
523expect(paymentDiscount.amountValue).toBe(discount.amount.toString())
524})
525})
526})
527