|
|
@@ -1,6 +1,6 @@
|
|
|
/* eslint-env jest */
|
|
|
import Vue from 'vue'
|
|
|
-import behavior, { cartTotals, paymentOrder } from '@/ui-refactor/views/guest-shop/guestPurchase'
|
|
|
+import behavior, { cartTotals, paymentOrder, validItem } from '@/ui-refactor/views/guest-shop/guestPurchase'
|
|
|
import * as shop from '@/api/shop'
|
|
|
import { getGuestRegionList } from '@/api/config'
|
|
|
|
|
|
@@ -8,7 +8,7 @@ jest.mock('@/api/shop', () => ({ fetchGuestProducts: jest.fn(), fetchGuestCart:
|
|
|
jest.mock('@/api/config', () => ({ getGuestRegionList: jest.fn() }))
|
|
|
jest.mock('@/router', () => ({ push: jest.fn(), replace: jest.fn() }))
|
|
|
|
|
|
-const product = { ID: 'P1', GOODS_NAME: 'Product', SELL_PRICE: 100, DISCOUNT: 80, STORE_NUMS: 5, TAX_RATE: 7.5, COIN: 'NGN' }
|
|
|
+const product = { ID: 'P1', GOODS_NAME: 'Product', SELL_PRICE: 100, MARKET_PRICE: 120, DISCOUNT: 80, STORE_NUMS: 5, TAX_RATE: 7.5, COIN: 'NGN' }
|
|
|
const instances = []
|
|
|
const shipping = { consignee: 'Recipient', acceptMobile: '123456789', province: ['R1'], lgaName: 'District', cityName: 'City', detailaddress: 'Street', email: 'guest@example.test' }
|
|
|
async function settle() { for (let i = 0; i < 12; i++) { await Promise.resolve(); await Vue.nextTick() } }
|
|
|
@@ -36,7 +36,7 @@ beforeEach(() => {
|
|
|
shop.fetchGuestProducts.mockResolvedValue({ data: { list: [product], totalCount: 1 }})
|
|
|
shop.fetchGuestCart.mockResolvedValue({ data: { sellType: [{ id: 1, sell_type: [{ label: 'pay_stack', name: 'Paystack' }] }], freight: 10, freeShipping: 500, coin: 'NGN' }})
|
|
|
getGuestRegionList.mockResolvedValue({ data: [{ value: 'R1', label: 'Region' }] })
|
|
|
- shop.createGuestOrder.mockResolvedValue({ data: { SN: 'ORDER1', amount: 90, currency: 'NGN' }})
|
|
|
+ shop.createGuestOrder.mockResolvedValue({ data: { SN: 'ORDER1', amount: 130, currency: 'NGN' }})
|
|
|
})
|
|
|
afterEach(() => { instances.splice(0).forEach(vm => vm.$destroy()) })
|
|
|
|
|
|
@@ -68,19 +68,41 @@ it.each([
|
|
|
expect(vm.currency).toBe(coin)
|
|
|
expect(vm.displayProducts[0].CODE).toBe(coin)
|
|
|
})
|
|
|
-it('calculates the legacy discount and shipping without adding included tax again', () => {
|
|
|
- expect(cartTotals([{ ...product, quantity: 2 }], 500, 10)).toMatchObject({ subtotal: 160, shipping: 10, total: 170, tax: 13.95 })
|
|
|
- expect(cartTotals([{ ...product, quantity: 5 }], 400, 10).total).toBe(400)
|
|
|
+it('uses market price without member discount and does not add included tax again', () => {
|
|
|
+ expect(cartTotals([{ ...product, quantity: 2 }], 500, 10)).toEqual({ subtotal: 240, shipping: 10, total: 250, tax: 16.74 })
|
|
|
+ expect(cartTotals([{ ...product, quantity: 5 }], 600, 10).total).toBe(600)
|
|
|
+ expect(cartTotals([], 500, 10)).toEqual({ subtotal: 0, shipping: 0, total: 0, tax: 0 })
|
|
|
})
|
|
|
it('holds quantity and total invariants over a range of stock and prices', () => {
|
|
|
for (let cents = 1; cents < 130; cents += 7) {
|
|
|
- const row = { ...product, SELL_PRICE: cents / 100, DISCOUNT: 100, quantity: 3 }
|
|
|
- const result = cartTotals([row], 0, 0)
|
|
|
- expect(result.total).toBe(Math.round(cents * 3) / 100)
|
|
|
- expect(result.tax).toBeGreaterThanOrEqual(0)
|
|
|
- expect(result.tax).toBeLessThanOrEqual(result.total)
|
|
|
+ for (let quantity = 1; quantity <= product.STORE_NUMS; quantity++) {
|
|
|
+ for (const price of [cents / 100, (cents / 100).toFixed(2)]) {
|
|
|
+ const row = { ...product, MARKET_PRICE: price, quantity }
|
|
|
+ const result = cartTotals([row], 0, 0)
|
|
|
+ expect(result.total).toBe(cents * quantity / 100)
|
|
|
+ expect(result.tax).toBeGreaterThanOrEqual(0)
|
|
|
+ expect(result.tax).toBeLessThanOrEqual(result.total)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
+it.each([null, undefined, '', ' ', -1, 'bad', Infinity, true, []])('rejects invalid market price %s instead of falling back to sales price', price => {
|
|
|
+ expect(validItem({ ...product, MARKET_PRICE: price, quantity: 1 })).toBe(false)
|
|
|
+})
|
|
|
+it.each([0, '0', '120.50'])('validates market price %s without requiring sales price or discount', price => {
|
|
|
+ expect(validItem({ ...product, MARKET_PRICE: price, SELL_PRICE: undefined, DISCOUNT: undefined, quantity: 1 })).toBe(true)
|
|
|
+})
|
|
|
+it('uses original market prices for product details, cart unit and line prices', async() => {
|
|
|
+ const vm = await instance()
|
|
|
+ const row = vm.displayProducts[0]
|
|
|
+ expect(row.SELL_PRICE).toBe(100)
|
|
|
+ vm.toggleProduct(row)
|
|
|
+ vm.changeQuantity({ row, value: 2 })
|
|
|
+ expect(vm.cartRows[0]).toMatchObject({ unitPrice: 120, linePrice: 240 })
|
|
|
+ vm.showProduct(row)
|
|
|
+ expect(vm.product).toMatchObject({ SELL_PRICE: 100, MARKET_PRICE: 120, taxAmount: 8.37 })
|
|
|
+ expect(product).not.toHaveProperty('taxAmount')
|
|
|
+})
|
|
|
it.each([null, '', ' ', 0, -1, 'abc', Infinity, true, ['100'], 1e20])('rejects invalid payment amount %s', amount => {
|
|
|
expect(() => paymentOrder({ SN: 'O1', amount, currency: 'NGN' })).toThrow()
|
|
|
})
|
|
|
@@ -125,7 +147,7 @@ it('blocks duplicate submit and retains the same unpaid order after cancellation
|
|
|
const first = vm.continuePayment()
|
|
|
await vm.continuePayment()
|
|
|
expect(shop.createGuestOrder).toHaveBeenCalledTimes(1)
|
|
|
- resolve({ data: { SN: 'ORDER1', amount: 90, currency: 'NGN' }})
|
|
|
+ resolve({ data: { SN: 'ORDER1', amount: 130, currency: 'NGN' }})
|
|
|
await first
|
|
|
const callback = vm.paymentCallback
|
|
|
vm.closePayment()
|
|
|
@@ -163,9 +185,9 @@ it.each([
|
|
|
vm.stage = 'checkout'
|
|
|
await settle()
|
|
|
vm.openEmail()
|
|
|
- shop.createGuestOrder.mockResolvedValue({ data: { SN: 'RECORD1', DEC_SN: 'DECLARATION1', COUNTRY_ID: countryId, CURRENCY_ID: currencyId, PAY_AMOUNT: 90, ORDER_AMOUNT: 100, PAY_AMOUNT_STANDARD: 0.075, EXCHANGE_RATE: '1200', EMAIL: '', STATUS: 1 }})
|
|
|
+ shop.createGuestOrder.mockResolvedValue({ data: { SN: 'RECORD1', DEC_SN: 'DECLARATION1', COUNTRY_ID: countryId, CURRENCY_ID: currencyId, PAY_AMOUNT: 130, ORDER_AMOUNT: 150, PAY_AMOUNT_STANDARD: 0.075, EXCHANGE_RATE: '1200', EMAIL: '', STATUS: 1 }})
|
|
|
await vm.continuePayment()
|
|
|
- expect(vm.pending).toEqual({ SN: 'RECORD1', amount: 90, currency, subunits: 9000 })
|
|
|
+ expect(vm.pending).toEqual({ SN: 'RECORD1', amount: 130, currency, subunits: 13000 })
|
|
|
expect(vm.$refs.paystack.payWithPaystack).toHaveBeenCalledTimes(1)
|
|
|
expect(vm.metadata.cart_id).toBe('RECORD1')
|
|
|
expect(vm.form.email).toBe(shipping.email)
|
|
|
@@ -179,9 +201,9 @@ it.each([
|
|
|
it('accepts ORDER_AMOUNT when the returned order record has no PAY_AMOUNT', async() => {
|
|
|
shop.fetchGuestProducts.mockResolvedValue({ data: { list: [{ ...product, LOCAL_CURRENCY_ID: '104' }], totalCount: 1 }})
|
|
|
const vm = await checkout()
|
|
|
- shop.createGuestOrder.mockResolvedValue({ data: { SN: 'RECORD1', COUNTRY_ID: '620699181539139625', CURRENCY_ID: '104', ORDER_AMOUNT: 90 }})
|
|
|
+ shop.createGuestOrder.mockResolvedValue({ data: { SN: 'RECORD1', COUNTRY_ID: '620699181539139625', CURRENCY_ID: '104', ORDER_AMOUNT: 130 }})
|
|
|
await vm.continuePayment()
|
|
|
- expect(vm.pending).toEqual({ SN: 'RECORD1', amount: 90, currency: 'NGN', subunits: 9000 })
|
|
|
+ expect(vm.pending).toEqual({ SN: 'RECORD1', amount: 130, currency: 'NGN', subunits: 13000 })
|
|
|
expect(vm.$refs.paystack.payWithPaystack).toHaveBeenCalledTimes(1)
|
|
|
})
|
|
|
it('updates the email amount and requests confirmation when the order record changes the total', async() => {
|