Skip to content

Commit

Permalink
Merge pull request #317 from QAComet/qacomet/discount-tests
Browse files Browse the repository at this point in the history
Test suite around discount functionality
  • Loading branch information
VariableVic committed Apr 22, 2024
2 parents bf88eac + f31476f commit 30f5646
Show file tree
Hide file tree
Showing 10 changed files with 1,465 additions and 25 deletions.
2 changes: 2 additions & 0 deletions e2e/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ PRODUCTION_POSTGRES_DATABASE=medusa_db

# Backend server API
CLIENT_SERVER=http://localhost:9000
MEDUSA_ADMIN_EMAIL=[email protected]
MEDUSA_ADMIN_PASSWORD=supersecret
88 changes: 76 additions & 12 deletions e2e/data/seed.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import axios, { AxiosError } from "axios"
import axios, { AxiosError, AxiosInstance } from "axios"

axios.defaults.baseURL = process.env.CLIENT_SERVER || "http://localhost:9000"
let region = undefined as any

export async function seedData() {
const axios = getOrInitAxios()
return {
user: await seedUser(),
}
}

function getUrl(path: string) {
const baseUrl = process.env.CLIENT_SERVER || "http://localhost:9000"
const url = new URL(path, baseUrl)
return url.toString()
}

async function seedUser() {
export async function seedUser(email?: string, password?: string) {
const user = {
first_name: "Test",
last_name: "User",
email: "[email protected]",
password: "password",
email: email || "[email protected]",
password: password || "password",
}
try {
await axios.post(getUrl("/store/customers"), user)
await axios.post("/store/customers", user)
return user
} catch (e: unknown) {
e = e as AxiosError
if (e instanceof AxiosError) {
if (e.response && e.response.status) {
const status = e.response.status
Expand All @@ -36,3 +33,70 @@ async function seedUser() {
}
}
}

async function loadRegion(axios: AxiosInstance) {
const resp = await axios.get("/admin/regions")
region = resp.data.regions.filter((r: any) => r.currency_code === "usd")[0]
}

async function getOrInitAxios(axios?: AxiosInstance) {
if (!axios) {
axios = await loginAdmin()
}
if (!region) {
await loadRegion(axios)
}
return axios
}

export async function seedGiftcard(axios?: AxiosInstance) {
axios = await getOrInitAxios(axios)
const resp = await axios.post("/admin/gift-cards", {
region_id: region.id,
value: 10000,
})
resp.data.gift_card.amount = resp.data.gift_card.value.toString()
return resp.data.gift_card as {
id: string
code: string
value: number
amount: string
balance: string
}
}

export async function seedDiscount(axios?: AxiosInstance) {
axios = await getOrInitAxios(axios)
const amount = 2000
const resp = await axios.post("/admin/discounts", {
code: "TEST_DISCOUNT_FIXED",
regions: [region.id],
rule: {
type: "fixed",
value: amount,
allocation: "total",
},
})
const discount = resp.data.discount
return {
id: discount.id,
code: discount.code,
rule_id: discount.rule_id,
amount,
}
}

async function loginAdmin() {
const resp = await axios.post("/admin/auth/token", {
email: process.env.MEDUSA_ADMIN_EMAIL || "[email protected]",
password: process.env.MEDUSA_ADMIN_PASSWORD || "supersecret",
})
if (resp.status !== 200) {
throw { error: "must be able to log in user" }
}
return axios.create({
headers: {
Authorization: `Bearer ${resp.data.access_token}`,
},
})
}
2 changes: 2 additions & 0 deletions e2e/fixtures/base/base-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class BasePage {
cartDropdown: CartDropdown
searchModal: SearchModal
accountLink: Locator
cartLink: Locator
searchLink: Locator
storeLink: Locator
categoriesList: Locator
Expand All @@ -19,6 +20,7 @@ export class BasePage {
this.cartDropdown = new CartDropdown(page)
this.searchModal = new SearchModal(page)
this.accountLink = page.getByTestId("nav-account-link")
this.cartLink = page.getByTestId("nav-cart-link")
this.storeLink = page.getByTestId("nav-store-link")
this.searchLink = page.getByTestId("nav-search-link")
this.categoriesList = page.getByTestId("footer-categories")
Expand Down
29 changes: 25 additions & 4 deletions e2e/fixtures/cart-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class CartPage extends BasePage {
discountInput: Locator
discountApplyButton: Locator
discountErrorMessage: Locator
discountRow: Locator
giftCardRow: Locator
giftCardCode: Locator
giftCardAmount: Locator
Expand Down Expand Up @@ -49,7 +50,8 @@ export class CartPage extends BasePage {
this.discountErrorMessage = this.container.getByTestId(
"discount-error-message"
)
this.giftCardRow = this.container.getByTestId("gift-card-row")
this.discountRow = this.container.getByTestId("discount-row")
this.giftCardRow = this.container.getByTestId("gift-card")
this.giftCardCode = this.container.getByTestId("gift-card-code")
this.giftCardAmount = this.container.getByTestId("gift-card-amount")
this.giftCardRemoveButton = this.container.getByTestId(
Expand Down Expand Up @@ -86,13 +88,32 @@ export class CartPage extends BasePage {

async getGiftCard(code: string) {
const giftCardRow = this.giftCardRow.filter({
has: this.giftCardCode.filter({ hasText: code }),
hasText: code,
})
const amount = giftCardRow.getByTestId("gift-card-amount")
return {
giftCardRow,
locator: giftCardRow,
code: giftCardRow.getByTestId("gift-card-code"),
amount: giftCardRow.getByTestId("gift-card-amount"),
amount,
amountValue: await amount.getAttribute("data-value"),
removeButton: giftCardRow.getByTestId("remove-gift-card-button"),
}
}

async getDiscount(code: string) {
const discount = this.discountRow
const amount = discount.getByTestId("discount-amount")
return {
locator: discount,
code: discount.getByTestId("discount-code"),
amount,
amountValue: await amount.getAttribute("data-value"),
removeButton: discount.getByTestId("remove-discount-button"),
}
}

async goto() {
await this.cartLink.click({ clickCount: 2 })
await this.container.waitFor({ state: "visible" })
}
}
30 changes: 29 additions & 1 deletion e2e/fixtures/checkout-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class CheckoutPage extends BasePage {
discountInput: Locator
discountApplyButton: Locator
discountErrorMessage: Locator
discountRow: Locator
giftCardRow: Locator
giftCardCode: Locator
giftCardAmount: Locator
Expand Down Expand Up @@ -214,7 +215,8 @@ export class CheckoutPage extends BasePage {
this.discountErrorMessage = this.container.getByTestId(
"discount-error-message"
)
this.giftCardRow = this.container.getByTestId("gift-card-row")
this.discountRow = this.container.getByTestId("discount-row")
this.giftCardRow = this.container.getByTestId("gift-card")
this.giftCardCode = this.container.getByTestId("gift-card-code")
this.giftCardAmount = this.container.getByTestId("gift-card-amount")
this.giftCardRemoveButton = this.container.getByTestId(
Expand Down Expand Up @@ -264,4 +266,30 @@ export class CheckoutPage extends BasePage {
async selectDeliveryOption(option: string) {
await this.deliveryOptionRadio.filter({ hasText: option }).click()
}

async getGiftCard(code: string) {
const giftCardRow = this.giftCardRow.filter({
hasText: code,
})
const amount = giftCardRow.getByTestId("gift-card-amount")
return {
locator: giftCardRow,
code: giftCardRow.getByTestId("gift-card-code"),
amount,
amountValue: await amount.getAttribute("data-value"),
removeButton: giftCardRow.getByTestId("remove-gift-card-button"),
}
}

async getDiscount(code: string) {
const discount = this.discountRow
const amount = discount.getByTestId("discount-amount")
return {
locator: discount,
code: discount.getByTestId("discount-code"),
amount,
amountValue: await amount.getAttribute("data-value"),
removeButton: discount.getByTestId("remove-discount-button"),
}
}
}
14 changes: 14 additions & 0 deletions e2e/fixtures/order-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { BasePage } from "./base/base-page"

export class OrderPage extends BasePage {
container: Locator
cartSubtotal: Locator
cartDiscount: Locator
cartGiftCardAmount: Locator
cartShipping: Locator
cartTaxes: Locator
cartTotal: Locator
orderEmail: Locator
orderDate: Locator
orderId: Locator
Expand Down Expand Up @@ -30,6 +36,14 @@ export class OrderPage extends BasePage {
this.orderDate = this.container.getByTestId("order-date")
this.orderId = this.container.getByTestId("order-id")
this.orderStatus = this.container.getByTestId("order-status")
this.cartSubtotal = this.container.getByTestId("cart-subtotal")
this.cartDiscount = this.container.getByTestId("cart-discount")
this.cartGiftCardAmount = this.container.getByTestId(
"cart-gift-card-amount"
)
this.cartShipping = this.container.getByTestId("cart-shipping")
this.cartTaxes = this.container.getByTestId("cart-taxes")
this.cartTotal = this.container.getByTestId("cart-total")
this.orderPaymentStatus = this.container.getByTestId("order-payment-status")
this.shippingAddressSummary = this.container.getByTestId(
"shipping-address-summary"
Expand Down

0 comments on commit 30f5646

Please sign in to comment.