126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
/**
|
|
* Coupon CRUD as staff, then the user end: the coupon must show up as eligible
|
|
* and actually reduce an order's amount when passed to placeOrder.
|
|
*
|
|
* Flow (ordered within the file):
|
|
* create -> getById -> update -> eligible for the user -> apply to an order
|
|
* -> delete invalidates it (no longer eligible).
|
|
*/
|
|
|
|
import { apiClient, captureError, uniqueSuffix } from '../helpers/api'
|
|
import { createFutureSlot, createStoreWithSku, ensureUserAddress } from '../helpers/fixtures'
|
|
import { staffSession, userSession } from '../helpers/sessions'
|
|
|
|
const FLAT_DISCOUNT = 2
|
|
|
|
describe('coupon CRUD + apply', () => {
|
|
let admin: ReturnType<typeof apiClient>
|
|
let user: ReturnType<typeof apiClient>
|
|
let code = ''
|
|
let couponId = 0
|
|
let skuId = 0
|
|
let slotId = 0
|
|
let addressId = 0
|
|
|
|
beforeAll(async () => {
|
|
const staff = await staffSession()
|
|
admin = apiClient(staff.token)
|
|
|
|
const store = await createStoreWithSku()
|
|
const slot = await createFutureSlot([store.skuId])
|
|
const address = await ensureUserAddress()
|
|
const session = await userSession()
|
|
user = apiClient(session.token)
|
|
|
|
skuId = store.skuId
|
|
slotId = slot.slotId
|
|
addressId = address.addressId
|
|
|
|
code = `APITEST${uniqueSuffix()}`.toUpperCase().replace(/[^A-Z0-9]/g, '').slice(0, 20)
|
|
|
|
const created = await admin.admin.coupon.create.mutate({
|
|
couponCode: code,
|
|
flatDiscount: FLAT_DISCOUNT,
|
|
minOrder: 1,
|
|
maxLimitForUser: 3,
|
|
isApplyForAll: true,
|
|
validTill: new Date(Date.now() + 24 * 3600_000).toISOString(),
|
|
})
|
|
|
|
couponId = created[0].id
|
|
})
|
|
|
|
it('creates the coupon and getById returns it', async () => {
|
|
expect(couponId).toBeGreaterThan(0)
|
|
|
|
const coupon = await admin.admin.coupon.getById.query({ id: couponId })
|
|
expect(coupon.couponCode).toBe(code)
|
|
expect(Number(coupon.flatDiscount)).toBe(FLAT_DISCOUNT)
|
|
})
|
|
|
|
it('updates the coupon', async () => {
|
|
await admin.admin.coupon.update.mutate({
|
|
id: couponId,
|
|
updates: { maxLimitForUser: 7 },
|
|
})
|
|
|
|
const coupon = await admin.admin.coupon.getById.query({ id: couponId })
|
|
expect(Number(coupon.maxLimitForUser)).toBe(7)
|
|
})
|
|
|
|
it('is eligible for the user', async () => {
|
|
const eligible = await user.user.coupon.getEligible.query()
|
|
|
|
const match = eligible.data.find((c) => c.id === couponId)
|
|
expect(match).toBeDefined()
|
|
expect(match!.couponCode).toBe(code)
|
|
})
|
|
|
|
it('reduces the order amount when the user applies it', async () => {
|
|
const placed = await user.user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
regionId: 1,
|
|
couponIds: [couponId],
|
|
})
|
|
|
|
const orderId = placed.data[0].id
|
|
const detail = await user.user.order.getOrderById.query({ orderId: String(orderId) })
|
|
|
|
expect(detail.couponCode).toBe(code)
|
|
expect(Number(detail.discountAmount)).toBe(FLAT_DISCOUNT)
|
|
})
|
|
|
|
it('deleting the coupon invalidates it for the user', async () => {
|
|
await admin.admin.coupon.delete.mutate({ id: couponId })
|
|
|
|
const eligible = await user.user.coupon.getEligible.query()
|
|
expect(eligible.data.some((c) => c.id === couponId)).toBe(false)
|
|
})
|
|
|
|
it('rejects applying the invalidated coupon to an order', async () => {
|
|
const error = await captureError(() =>
|
|
user.user.order.placeOrder.mutate({
|
|
selectedItems: [{ skuId, quantity: 1, slotId }],
|
|
addressId,
|
|
regionId: 1,
|
|
couponIds: [couponId],
|
|
})
|
|
)
|
|
|
|
expect(error.message).toMatch(/invalid|expired|no longer valid/i)
|
|
})
|
|
|
|
it('rejects a coupon with both discount types', async () => {
|
|
const error = await captureError(() =>
|
|
admin.admin.coupon.create.mutate({
|
|
couponCode: `BOTH${uniqueSuffix()}`.toUpperCase(),
|
|
discountPercent: 10,
|
|
flatDiscount: 5,
|
|
isApplyForAll: true,
|
|
})
|
|
)
|
|
|
|
expect(error.message).toMatch(/discountPercent or flatDiscount/i)
|
|
})
|
|
})
|