| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- /* eslint-env jest */
- import Vue from 'vue'
- import ElementUI from 'element-ui'
- import path from 'path'
- import { load } from './loadGuestComponent'
- import { fetchGuestOrderDetail, downloadGuestOrder, downloadOrder } from '@/api/shop'
- import handleClipboard from '@/utils/clipboard'
- import usersInfo from '@/utils/usersInfo'
- jest.mock('@/api/shop', () => ({ fetchGuestOrderDetail: jest.fn(), downloadGuestOrder: jest.fn(), downloadOrder: jest.fn() }))
- jest.mock('@/utils/clipboard', () => jest.fn())
- jest.mock('@/utils/usersInfo', () => ({ accessToken: jest.fn() }))
- jest.mock('@/router', () => ({ push: jest.fn(), replace: jest.fn() }))
- Vue.use(ElementUI)
- const Order = load(path.resolve('src/ui-refactor/views/guest-shop/GuestOrderPage.vue'))
- const sample = {
- SN: 'SAMPLE-ORDER',
- STATUS: 'Unpaid',
- CREATED_AT: 1792000000,
- CURRENCY: 'NGN',
- ORDER_AMOUNT: 38000,
- PAY_AMOUNT: 38100,
- PAY_FREIGHT: 100,
- CONSIGNEE: 'Sample recipient',
- MOBILE: '+234123456789',
- FULL_ADDRESS: 'Sample street, Lagos',
- COUNTRY: 'Nigeria',
- PAY_TYPE: 'pay_stack',
- hasChildren: [
- {
- GOODS_TITLE: 'Sample product',
- SKU_CODE: 'SKU-1',
- COVER: 'sample.png',
- REAL_PRICE: 19000,
- BUY_NUMS: 2,
- TAX_RATE: 7.5
- }
- ]
- }
- const response = (order = sample) => ({ data: { order, goods: order.hasChildren || [] }})
- const instances = []
- const errors = []
- Vue.config.errorHandler = error => errors.push(error)
- async function settle() {
- for (let i = 0; i < 12; i++) {
- await Promise.resolve()
- await Vue.nextTick()
- }
- }
- function mount(order = 'SAMPLE-ORDER') {
- const route = Vue.observable({ query: { order }})
- const router = {
- push: jest.fn(),
- resolve: jest.fn(() => ({ href: '/app/#/guest-order?order=' + encodeURIComponent(order) }))
- }
- const vm = new (Vue.extend(Order))({
- beforeCreate() {
- this.$route = route
- this.$router = router
- }
- }).$mount()
- document.body.appendChild(vm.$el)
- instances.push(vm)
- return vm
- }
- beforeEach(() => {
- jest.clearAllMocks()
- errors.length = 0
- usersInfo.accessToken.mockReturnValue('')
- process.env.VUE_APP_BASE_DO_API = 'https://api.example.test'
- fetchGuestOrderDetail.mockResolvedValue(response())
- downloadGuestOrder.mockResolvedValue({
- data: { fileUrl: 'receipts/sample.pdf', targetName: 'sample.pdf' }
- })
- downloadOrder.mockResolvedValue({ data: { fileUrl: 'receipts/member.pdf', targetName: 'member.pdf' }})
- })
- afterEach(() => {
- instances.splice(0).forEach(vm => {
- vm.$destroy()
- vm.$el.remove()
- })
- jest.restoreAllMocks()
- expect(errors).toEqual([])
- })
- it('requests the anonymous detail by the URL reference and renders server data without adding tax', async() => {
- const vm = mount()
- await settle()
- expect(fetchGuestOrderDetail).toHaveBeenCalledWith({ orderSn: 'SAMPLE-ORDER' })
- const text = vm.$el.textContent
- for (const value of [
- 'Order SAMPLE-ORDER',
- 'Sample product',
- 'SKU-1',
- 'Sample recipient',
- '+234123456789',
- 'Sample street, Lagos',
- 'NGN 38,100',
- 'Unpaid',
- 'Paystack'
- ]) { expect(text).toContain(value) }
- expect(text).not.toContain('40,950')
- expect(text).not.toContain('BV')
- })
- it('does not request a missing reference', async() => {
- const vm = mount('')
- await settle()
- expect(fetchGuestOrderDetail).not.toHaveBeenCalled()
- expect(vm.$el.textContent).toContain('Order reference unavailable')
- })
- it('supports a direct link and a fresh mount without saved receipts', async() => {
- const storage = jest.spyOn(Storage.prototype, 'setItem')
- mount()
- await settle()
- mount()
- await settle()
- expect(fetchGuestOrderDetail).toHaveBeenCalledTimes(2)
- expect(storage).not.toHaveBeenCalled()
- })
- it('clears old details while querying another order and ignores the older response', async() => {
- let oldResponse
- fetchGuestOrderDetail.mockImplementationOnce(
- () =>
- new Promise(resolve => {
- oldResponse = resolve
- })
- )
- const vm = mount()
- vm.$route.query = { order: 'SECOND-ORDER' }
- fetchGuestOrderDetail.mockResolvedValue(
- response({ ...sample, SN: 'SECOND-ORDER', CONSIGNEE: 'New recipient' })
- )
- await settle()
- oldResponse(response())
- await settle()
- expect(vm.$el.textContent).toContain('Order SECOND-ORDER')
- expect(vm.$el.textContent).toContain('New recipient')
- expect(vm.$el.textContent).not.toContain('Sample recipient')
- })
- it('ignores a response after the page is destroyed', async() => {
- let finish
- fetchGuestOrderDetail.mockImplementationOnce(
- () =>
- new Promise(resolve => {
- finish = resolve
- })
- )
- const vm = mount()
- vm.$destroy()
- finish(response())
- await settle()
- expect(vm.detail).toBeNull()
- })
- it('shows an error with retry and recovers from a failed query', async() => {
- fetchGuestOrderDetail.mockRejectedValueOnce(new Error('not found'))
- const vm = mount()
- await settle()
- expect(vm.$el.textContent).toContain('Unable to load this order')
- const retry = [...vm.$el.querySelectorAll('button')].find(button =>
- button.textContent.includes('Retry')
- )
- retry.click()
- await settle()
- expect(vm.$el.textContent).toContain('Sample product')
- })
- it('rejects an unexpected order returned for this reference', async() => {
- fetchGuestOrderDetail.mockResolvedValue(response({ ...sample, SN: 'ANOTHER-ORDER' }))
- const vm = mount()
- await settle()
- expect(vm.$el.textContent).toContain('Unable to load this order')
- expect(vm.$el.textContent).not.toContain('Sample recipient')
- })
- it('uses the router and current origin for copy links, excluding personal data', async() => {
- const vm = mount()
- await settle()
- const button = [...vm.$el.querySelectorAll('button')].find(
- button => button.textContent.trim() === 'Copy'
- )
- button.click()
- expect(handleClipboard.mock.calls[0][0]).toBe(
- window.location.origin + '/app/#/guest-order?order=SAMPLE-ORDER'
- )
- expect(vm.$router.resolve).toHaveBeenCalledWith({
- path: '/guest-order',
- query: { order: 'SAMPLE-ORDER' }
- })
- })
- it('downloads through the anonymous receipt endpoint and prevents double clicks', async() => {
- let complete
- downloadGuestOrder.mockImplementationOnce(
- () =>
- new Promise(resolve => {
- complete = resolve
- })
- )
- const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
- const vm = mount()
- await settle()
- vm.downloadReceipt()
- vm.downloadReceipt()
- expect(downloadGuestOrder).toHaveBeenCalledTimes(1)
- expect(downloadGuestOrder).toHaveBeenCalledWith('SAMPLE-ORDER')
- expect(downloadOrder).not.toHaveBeenCalled()
- complete({ data: { fileUrl: 'receipts/sample.pdf', targetName: 'sample.pdf' }})
- await settle()
- expect(click.mock.instances[0].href).toBe('https://api.example.test/receipts/sample.pdf')
- expect(click.mock.instances[0].rel).toContain('noopener')
- expect(vm.downloading).toBe(false)
- })
- it('uses the original receipt method for a signed-in visitor', async() => {
- usersInfo.accessToken.mockReturnValue('sample-session')
- jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
- const vm = mount()
- await settle()
- await vm.downloadReceipt()
- expect(downloadOrder).toHaveBeenCalledWith('SAMPLE-ORDER')
- expect(downloadGuestOrder).not.toHaveBeenCalled()
- })
- it.each([
- () => Promise.reject(new Error('Unavailable')),
- () => Promise.resolve({ data: { status: 401, message: 'Unauthorized' }}),
- () => Promise.resolve({ data: { fileUrl: 'javascript:alert(1)' }})
- ])('recovers from receipt errors without navigating away', async response => {
- downloadGuestOrder.mockImplementation(response)
- const vm = mount()
- await settle()
- await vm.downloadReceipt()
- expect(vm.$el.textContent).toContain('Receipt download is unavailable')
- expect(vm.downloading).toBe(false)
- expect(vm.$router.push).not.toHaveBeenCalled()
- })
- it('does not download or show a stale receipt after switching references', async() => {
- let complete
- const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
- downloadGuestOrder.mockImplementationOnce(
- () =>
- new Promise(resolve => {
- complete = resolve
- })
- )
- const vm = mount()
- await settle()
- vm.downloadReceipt()
- vm.$route.query = { order: 'SECOND-ORDER' }
- await settle()
- complete({ data: { fileUrl: 'receipts/sample.pdf' }})
- await settle()
- expect(click).not.toHaveBeenCalled()
- })
- it('renders the confirmed order/goods response with an empty goods list and missing address', async() => {
- fetchGuestOrderDetail.mockResolvedValue({
- data: {
- order: {
- ...sample,
- ORDER_AMOUNT: '2999000.00',
- PAY_AMOUNT: '2999000.00',
- STATUS: 'Paid',
- FULL_ADDRESS: '',
- PROVINCE: '1',
- SHOW_BV: true,
- USER_NAME: 'PRIVATE-MEMBER'
- },
- goods: []
- }
- })
- const vm = mount()
- await settle()
- expect(vm.$el.textContent).toContain('NGN 2,999,000')
- expect(vm.$el.textContent).toContain('Item details are unavailable')
- expect(vm.$el.textContent).toContain('Paid')
- expect(vm.$el.textContent).not.toContain('PRIVATE-MEMBER')
- expect(vm.$el.textContent).not.toContain('BV')
- expect(vm.$el.querySelector('.ng-guest-order-address').textContent).toContain('—')
- })
- it.each(['Paid', 'Unpaid', 'Cancelled', 1, null])(
- 'uses the server status %s rather than a payment callback or URL flag',
- async status => {
- fetchGuestOrderDetail.mockResolvedValue(response({ ...sample, STATUS: status }))
- const vm = mount()
- vm.$route.query = { order: 'SAMPLE-ORDER', paid: 'true' }
- await settle()
- const tag = vm.$el.querySelector('.el-tag')
- expect(tag.textContent).toBe(
- status == null ? 'Status unavailable' : status === 1 ? 'Status: 1' : status
- )
- expect(tag.classList.contains('el-tag--success')).toBe(status === 'Paid')
- }
- )
- it('keeps zero amounts and missing amounts distinct without inventing a currency', async() => {
- fetchGuestOrderDetail.mockResolvedValue({
- data: { order: { SN: sample.SN, PAY_AMOUNT: 0, PAY_FREIGHT: 0 }, goods: [] }
- })
- const vm = mount()
- await settle()
- expect(vm.$el.querySelector('.ng-guest-order-grand-total strong').textContent).toBe('0.00')
- expect(vm.$el.querySelector('.ng-guest-order-totals dd').textContent).toBe('—')
- expect(vm.$el.textContent).toContain('Currency information is unavailable')
- expect(vm.$el.textContent).not.toContain('NGN')
- })
- it('uses the response currency and final amount without exchange or another tax charge', async() => {
- fetchGuestOrderDetail.mockResolvedValue(
- response({
- ...sample,
- CURRENCY: 'AED',
- PAY_AMOUNT: '41000.50',
- PAY_AMOUNT_STANDARD: 10,
- EXCHANGE_RATE: 1200
- })
- )
- const vm = mount()
- await settle()
- expect(vm.$el.querySelector('.ng-guest-order-grand-total strong').textContent).toBe(
- 'AED 41,000.50'
- )
- })
|