Skip to content

Commit 30f5646

Browse files
authored
Merge pull request #317 from QAComet/qacomet/discount-tests
Test suite around discount functionality
2 parents bf88eac + f31476f commit 30f5646

File tree

10 files changed

+1465
-25
lines changed

10 files changed

+1465
-25
lines changed

e2e/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ PRODUCTION_POSTGRES_DATABASE=medusa_db
1515

1616
# Backend server API
1717
CLIENT_SERVER=http://localhost:9000
18+
MEDUSA_ADMIN_EMAIL=[email protected]
19+
MEDUSA_ADMIN_PASSWORD=supersecret

e2e/data/seed.ts

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
import axios, { AxiosError } from "axios"
1+
import axios, { AxiosError, AxiosInstance } from "axios"
2+
3+
axios.defaults.baseURL = process.env.CLIENT_SERVER || "http://localhost:9000"
4+
let region = undefined as any
25

36
export async function seedData() {
7+
const axios = getOrInitAxios()
48
return {
59
user: await seedUser(),
610
}
711
}
812

9-
function getUrl(path: string) {
10-
const baseUrl = process.env.CLIENT_SERVER || "http://localhost:9000"
11-
const url = new URL(path, baseUrl)
12-
return url.toString()
13-
}
14-
15-
async function seedUser() {
13+
export async function seedUser(email?: string, password?: string) {
1614
const user = {
1715
first_name: "Test",
1816
last_name: "User",
19-
20-
password: "password",
17+
email: email || "[email protected]",
18+
password: password || "password",
2119
}
2220
try {
23-
await axios.post(getUrl("/store/customers"), user)
21+
await axios.post("/store/customers", user)
2422
return user
2523
} catch (e: unknown) {
26-
e = e as AxiosError
2724
if (e instanceof AxiosError) {
2825
if (e.response && e.response.status) {
2926
const status = e.response.status
@@ -36,3 +33,70 @@ async function seedUser() {
3633
}
3734
}
3835
}
36+
37+
async function loadRegion(axios: AxiosInstance) {
38+
const resp = await axios.get("/admin/regions")
39+
region = resp.data.regions.filter((r: any) => r.currency_code === "usd")[0]
40+
}
41+
42+
async function getOrInitAxios(axios?: AxiosInstance) {
43+
if (!axios) {
44+
axios = await loginAdmin()
45+
}
46+
if (!region) {
47+
await loadRegion(axios)
48+
}
49+
return axios
50+
}
51+
52+
export async function seedGiftcard(axios?: AxiosInstance) {
53+
axios = await getOrInitAxios(axios)
54+
const resp = await axios.post("/admin/gift-cards", {
55+
region_id: region.id,
56+
value: 10000,
57+
})
58+
resp.data.gift_card.amount = resp.data.gift_card.value.toString()
59+
return resp.data.gift_card as {
60+
id: string
61+
code: string
62+
value: number
63+
amount: string
64+
balance: string
65+
}
66+
}
67+
68+
export async function seedDiscount(axios?: AxiosInstance) {
69+
axios = await getOrInitAxios(axios)
70+
const amount = 2000
71+
const resp = await axios.post("/admin/discounts", {
72+
code: "TEST_DISCOUNT_FIXED",
73+
regions: [region.id],
74+
rule: {
75+
type: "fixed",
76+
value: amount,
77+
allocation: "total",
78+
},
79+
})
80+
const discount = resp.data.discount
81+
return {
82+
id: discount.id,
83+
code: discount.code,
84+
rule_id: discount.rule_id,
85+
amount,
86+
}
87+
}
88+
89+
async function loginAdmin() {
90+
const resp = await axios.post("/admin/auth/token", {
91+
email: process.env.MEDUSA_ADMIN_EMAIL || "[email protected]",
92+
password: process.env.MEDUSA_ADMIN_PASSWORD || "supersecret",
93+
})
94+
if (resp.status !== 200) {
95+
throw { error: "must be able to log in user" }
96+
}
97+
return axios.create({
98+
headers: {
99+
Authorization: `Bearer ${resp.data.access_token}`,
100+
},
101+
})
102+
}

e2e/fixtures/base/base-page.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class BasePage {
99
cartDropdown: CartDropdown
1010
searchModal: SearchModal
1111
accountLink: Locator
12+
cartLink: Locator
1213
searchLink: Locator
1314
storeLink: Locator
1415
categoriesList: Locator
@@ -19,6 +20,7 @@ export class BasePage {
1920
this.cartDropdown = new CartDropdown(page)
2021
this.searchModal = new SearchModal(page)
2122
this.accountLink = page.getByTestId("nav-account-link")
23+
this.cartLink = page.getByTestId("nav-cart-link")
2224
this.storeLink = page.getByTestId("nav-store-link")
2325
this.searchLink = page.getByTestId("nav-search-link")
2426
this.categoriesList = page.getByTestId("footer-categories")

e2e/fixtures/cart-page.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class CartPage extends BasePage {
1414
discountInput: Locator
1515
discountApplyButton: Locator
1616
discountErrorMessage: Locator
17+
discountRow: Locator
1718
giftCardRow: Locator
1819
giftCardCode: Locator
1920
giftCardAmount: Locator
@@ -49,7 +50,8 @@ export class CartPage extends BasePage {
4950
this.discountErrorMessage = this.container.getByTestId(
5051
"discount-error-message"
5152
)
52-
this.giftCardRow = this.container.getByTestId("gift-card-row")
53+
this.discountRow = this.container.getByTestId("discount-row")
54+
this.giftCardRow = this.container.getByTestId("gift-card")
5355
this.giftCardCode = this.container.getByTestId("gift-card-code")
5456
this.giftCardAmount = this.container.getByTestId("gift-card-amount")
5557
this.giftCardRemoveButton = this.container.getByTestId(
@@ -86,13 +88,32 @@ export class CartPage extends BasePage {
8688

8789
async getGiftCard(code: string) {
8890
const giftCardRow = this.giftCardRow.filter({
89-
has: this.giftCardCode.filter({ hasText: code }),
91+
hasText: code,
9092
})
93+
const amount = giftCardRow.getByTestId("gift-card-amount")
9194
return {
92-
giftCardRow,
95+
locator: giftCardRow,
9396
code: giftCardRow.getByTestId("gift-card-code"),
94-
amount: giftCardRow.getByTestId("gift-card-amount"),
97+
amount,
98+
amountValue: await amount.getAttribute("data-value"),
9599
removeButton: giftCardRow.getByTestId("remove-gift-card-button"),
96100
}
97101
}
102+
103+
async getDiscount(code: string) {
104+
const discount = this.discountRow
105+
const amount = discount.getByTestId("discount-amount")
106+
return {
107+
locator: discount,
108+
code: discount.getByTestId("discount-code"),
109+
amount,
110+
amountValue: await amount.getAttribute("data-value"),
111+
removeButton: discount.getByTestId("remove-discount-button"),
112+
}
113+
}
114+
115+
async goto() {
116+
await this.cartLink.click({ clickCount: 2 })
117+
await this.container.waitFor({ state: "visible" })
118+
}
98119
}

e2e/fixtures/checkout-page.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class CheckoutPage extends BasePage {
6161
discountInput: Locator
6262
discountApplyButton: Locator
6363
discountErrorMessage: Locator
64+
discountRow: Locator
6465
giftCardRow: Locator
6566
giftCardCode: Locator
6667
giftCardAmount: Locator
@@ -214,7 +215,8 @@ export class CheckoutPage extends BasePage {
214215
this.discountErrorMessage = this.container.getByTestId(
215216
"discount-error-message"
216217
)
217-
this.giftCardRow = this.container.getByTestId("gift-card-row")
218+
this.discountRow = this.container.getByTestId("discount-row")
219+
this.giftCardRow = this.container.getByTestId("gift-card")
218220
this.giftCardCode = this.container.getByTestId("gift-card-code")
219221
this.giftCardAmount = this.container.getByTestId("gift-card-amount")
220222
this.giftCardRemoveButton = this.container.getByTestId(
@@ -264,4 +266,30 @@ export class CheckoutPage extends BasePage {
264266
async selectDeliveryOption(option: string) {
265267
await this.deliveryOptionRadio.filter({ hasText: option }).click()
266268
}
269+
270+
async getGiftCard(code: string) {
271+
const giftCardRow = this.giftCardRow.filter({
272+
hasText: code,
273+
})
274+
const amount = giftCardRow.getByTestId("gift-card-amount")
275+
return {
276+
locator: giftCardRow,
277+
code: giftCardRow.getByTestId("gift-card-code"),
278+
amount,
279+
amountValue: await amount.getAttribute("data-value"),
280+
removeButton: giftCardRow.getByTestId("remove-gift-card-button"),
281+
}
282+
}
283+
284+
async getDiscount(code: string) {
285+
const discount = this.discountRow
286+
const amount = discount.getByTestId("discount-amount")
287+
return {
288+
locator: discount,
289+
code: discount.getByTestId("discount-code"),
290+
amount,
291+
amountValue: await amount.getAttribute("data-value"),
292+
removeButton: discount.getByTestId("remove-discount-button"),
293+
}
294+
}
267295
}

e2e/fixtures/order-page.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { BasePage } from "./base/base-page"
33

44
export class OrderPage extends BasePage {
55
container: Locator
6+
cartSubtotal: Locator
7+
cartDiscount: Locator
8+
cartGiftCardAmount: Locator
9+
cartShipping: Locator
10+
cartTaxes: Locator
11+
cartTotal: Locator
612
orderEmail: Locator
713
orderDate: Locator
814
orderId: Locator
@@ -30,6 +36,14 @@ export class OrderPage extends BasePage {
3036
this.orderDate = this.container.getByTestId("order-date")
3137
this.orderId = this.container.getByTestId("order-id")
3238
this.orderStatus = this.container.getByTestId("order-status")
39+
this.cartSubtotal = this.container.getByTestId("cart-subtotal")
40+
this.cartDiscount = this.container.getByTestId("cart-discount")
41+
this.cartGiftCardAmount = this.container.getByTestId(
42+
"cart-gift-card-amount"
43+
)
44+
this.cartShipping = this.container.getByTestId("cart-shipping")
45+
this.cartTaxes = this.container.getByTestId("cart-taxes")
46+
this.cartTotal = this.container.getByTestId("cart-total")
3347
this.orderPaymentStatus = this.container.getByTestId("order-payment-status")
3448
this.shippingAddressSummary = this.container.getByTestId(
3549
"shipping-address-summary"

0 commit comments

Comments
 (0)