/* eslint-env jest */ import Vue from 'vue' import ElementUI from 'element-ui' import path from 'path' import { load } from './loadGuestComponent' import * as shop from '@/api/shop' import { getGuestRegionList } from '@/api/config' import { toThousandFilter } from '@/filters' jest.mock('@/api/shop', () => ({ fetchGuestProducts: jest.fn(), fetchGuestCart: jest.fn(), createGuestOrder: jest.fn(), fetchGuestOrderDetail: jest.fn(), downloadGuestOrder: jest.fn(), downloadOrder: jest.fn() })) jest.mock('@/api/config', () => ({ getGuestRegionList: jest.fn() })) jest.mock('@/router', () => ({ push: jest.fn(), replace: jest.fn() })) jest.mock('vue-paystack', () => ({ name: 'Paystack', props: ['amount', 'currency', 'reference', 'email', 'metadata', 'callback', 'close', 'paystackkey', 'channels'], created() { this.scriptLoaded = Promise.resolve() }, render: h => h('button') })) Vue.use(ElementUI) Vue.component('RouterLink', { render(h) { return h('a', this.$slots.default) } }) Vue.filter('toThousandFilter', toThousandFilter) const push = jest.fn() Vue.prototype.$router = { push, replace: jest.fn() } Vue.prototype.$route = { query: {}} Vue.prototype.$message = { error: jest.fn(), info: jest.fn() } const root = path.resolve('src/ui-refactor/views/guest-shop') const Flow = load(path.join(root, 'GuestPurchaseFlow.vue')) const MarketDialog = load(path.join(root, 'MarketDialog.vue')) const Order = load(path.join(root, 'GuestOrderPage.vue')) const errors = [] const instances = [] Vue.config.errorHandler = error => errors.push(error) async function settle() { for (let i = 0; i < 16; i++) { await Promise.resolve(); await Vue.nextTick() } } const descendants = vm => vm.$children.flatMap(child => [child, ...descendants(child)]) function mount(component, propsData = {}) { const vm = new (Vue.extend(component))({ propsData }).$mount() document.body.appendChild(vm.$el) instances.push(vm) return vm } beforeEach(() => { jest.clearAllMocks() errors.length = 0 shop.fetchGuestProducts.mockResolvedValue({ data: { list: [{ ID: 'P1', GOODS_NAME: 'Guest product', GOODS_NO: 'SKU', SELL_PRICE: 100, MARKET_PRICE: 150, PRICE_PV: 20, DISCOUNT: 80, TAX_RATE: 7.5, STORE_NUMS: 5, COIN: 'AED' }], totalCount: 1 }}) shop.fetchGuestCart.mockResolvedValue({ data: { sellType: [{ id: 1, sell_type: [{ label: 'pay_stack', name: 'Paystack' }, { label: 'cash', name: 'Cash' }] }], freight: 10, freeShipping: 500, coin: 'AED' }}) getGuestRegionList.mockResolvedValue({ data: [{ value: 'R1', label: 'Dubai' }] }) }) afterEach(() => { instances.splice(0).forEach(vm => { vm.$destroy(); vm.$el.remove() }) delete window.PaystackPop expect(errors).toEqual([]) }) it('offers the two markets and navigates without asking for credentials', async() => { const vm = mount(MarketDialog, { visible: true }) await settle() const options = vm.$el.querySelectorAll('.ng-market-option') expect(options).toHaveLength(2) expect(options[1].textContent).toContain('UAE') options[1].click() expect(push).toHaveBeenCalledWith({ path: '/guest-shop/ae' }) }) it('hides Sales Price and BV in guest product details while keeping market price and its tax', async() => { const vm = mount(Flow, { market: 'ae', stage: 'catalog' }) await settle() vm.$el.querySelector('.ng-signup-product-view').click() await settle() const dialog = document.body.querySelector('.ng-product-details-dialog') expect(dialog.textContent).not.toContain('Sales Price') expect(dialog.textContent).not.toContain('BV') expect(dialog.textContent).toContain('AED 150') expect(dialog.textContent).toContain('AED 10.47') }) it('connects product, cart and checkout controls without guest BV or member fields', async() => { const vm = mount(Flow, { market: 'ae', stage: 'catalog' }) await settle() expect(vm.$el.querySelector('.ng-signup-product-price').textContent).toContain('AED 150') expect(vm.$el.querySelector('.ng-signup-products').textContent).not.toContain('BV') vm.$el.querySelector('.ng-signup-product-add').click() await settle() vm.stage = 'cart' await settle() expect(vm.$el.querySelector('.ng-purchase-cart-item').textContent).toContain('AED 150 each') const cart = descendants(vm).find(child => child.$options.name === 'GuestCart') expect(cart.$children[0].$options.name).toBe('PurchaseCartItems') expect(vm.$el.querySelector('.ng-purchase-summary').textContent).not.toContain('BV') vm.stage = 'checkout' await settle() expect(getGuestRegionList).toHaveBeenCalledWith({ countryId: '620699181539139643' }) const shippingForm = vm.$refs.shipping.$children[0] expect(shippingForm.$children.some(child => child.$options.name === 'PurchaseCheckoutFields')).toBe(true) expect(vm.$el.textContent).not.toContain('Member information') expect(vm.$el.textContent).not.toContain('Reporting Center') const recipient = vm.$el.querySelector('#ng-guest-consignee') recipient.value = 'Recipient' recipient.dispatchEvent(new Event('input', { bubbles: true })) await settle() expect(vm.form.consignee).toBe('Recipient') Object.assign(vm.form, { acceptMobile: '123', email: 'guest@example.test', province: ['R1'], lgaName: 'District', cityName: 'Dubai', detailaddress: 'Street' }) vm.$el.querySelector('.ng-purchase-place-order').click() await settle() const dialog = vm.$children.find(child => child.$options.name === 'PaystackPaymentDialog') expect(dialog.visible).toBe(true) expect(dialog.$el.querySelector('input')).toBeNull() expect(dialog.$el.textContent).not.toContain('Confirm your email') expect(dialog.$el.querySelector('[type=password]')).toBeNull() }) it('keeps Nigeria merchandise in the product currency and uses the cart coin for payment', async() => { shop.fetchGuestProducts.mockResolvedValue({ data: { list: [{ ID: 'P1', GOODS_NAME: 'Nigeria product', SELL_PRICE: 100, MARKET_PRICE: 120, DISCOUNT: 80, TAX_RATE: 7.5, STORE_NUMS: 5, COIN: 'NGN' }], totalCount: 1 }}) const vm = mount(Flow, { market: 'ng', stage: 'catalog' }) await settle() expect(vm.$el.querySelector('.ng-signup-product-price').textContent).toContain('NGN 120') vm.$el.querySelector('.ng-signup-product-add').click() vm.stage = 'cart' await settle() expect(vm.$el.querySelector('.ng-purchase-cart-item').textContent).toContain('NGN 120 each') expect(vm.$el.querySelector('.ng-purchase-summary').textContent).toContain('NGN 130') vm.stage = 'checkout' await settle() expect(vm.$el.querySelector('.ng-purchase-summary').textContent).toContain('NGN 130') Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', email: 'guest@example.test', province: ['R1'], lgaName: 'District', cityName: 'Lagos', detailaddress: 'Street' }) vm.openEmail() await settle() const dialog = descendants(vm).find(child => child.$options.name === 'PaystackPaymentDialog') expect(dialog.visible).toBe(true) expect(dialog.$el.querySelector('.ng-paystack-payment-amount').textContent).toContain('AED 130') }) it.each(['ng', 'ae'])('uses the show-cart coin in the %s payment dialog before and after an order is created', async market => { shop.fetchGuestCart.mockResolvedValue({ data: { sellType: [{ id: 1, sell_type: [{ label: 'pay_stack', name: 'Paystack' }] }], freight: '20', freeShipping: '10000', coin: 'NGN' }}) const vm = mount(Flow, { market, stage: 'catalog' }) await settle() expect(vm.$el.querySelector('.ng-signup-product-price').textContent).toContain('AED 150') vm.toggleProduct(vm.products[0]) vm.stage = 'checkout' await settle() expect(vm.$el.querySelector('.ng-purchase-summary').textContent).toContain('AED 170') Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', email: 'guest@example.test', province: ['R1'], lgaName: 'District', cityName: 'City', detailaddress: 'Street' }) vm.openEmail() await settle() const dialog = descendants(vm).find(child => child.$options.name === 'PaystackPaymentDialog') const amount = dialog.$el.querySelector('.ng-paystack-payment-amount strong') expect(amount.textContent).toBe('NGN 170') vm.pending = { SN: 'PENDING-ORDER', amount: 1280, currency: 'AED', subunits: 128000 } await settle() expect(amount.textContent).toBe('NGN 1,280') vm.closePayment() vm.openEmail() await settle() expect(amount.textContent).toBe('NGN 1,280') expect(shop.createGuestOrder).not.toHaveBeenCalled() }) it.each(['ng', 'ae'])('renders and validates the %s checkout email between phone and region', async market => { const vm = mount(Flow, { market, stage: 'catalog' }) await settle() vm.toggleProduct(vm.products[0]) vm.stage = 'checkout' await settle() Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', province: ['R1'], lgaName: 'District', cityName: 'City', detailaddress: 'Street' }) const input = vm.$el.querySelector('#ng-guest-email') expect(input).not.toBeNull() expect(input.type).toBe('email') expect(input.autocomplete).toBe('email') expect(Array.from(vm.$el.querySelectorAll('.ng-purchase-shipping-address label')).map(label => label.htmlFor)).toEqual(['ng-guest-consignee', 'ng-guest-acceptMobile', 'ng-guest-email', 'ng-guest-province', 'ng-guest-lgaName', 'ng-guest-cityName', 'ng-guest-detailaddress']) vm.$el.querySelector('.ng-purchase-place-order').click() await settle() expect(vm.emailVisible).toBe(false) expect(input.closest('.el-form-item').textContent).toContain('Please enter email.') input.value = 'invalid' input.dispatchEvent(new Event('input', { bubbles: true })) await settle() expect(vm.form.email).toBe('invalid') input.dispatchEvent(new Event('blur')) vm.$el.querySelector('.ng-purchase-place-order').click() await settle() expect(vm.emailVisible).toBe(false) expect(input.closest('.el-form-item').textContent).toContain('Please enter a valid email address.') expect(shop.createGuestOrder).not.toHaveBeenCalled() input.value = 'guest@example.test' input.dispatchEvent(new Event('input', { bubbles: true })) vm.$el.querySelector('.ng-purchase-place-order').click() await settle() expect(vm.form.email).toBe('guest@example.test') expect(vm.emailVisible).toBe(true) }) it.each(['visitor@example.test', ''])('opens Paystack with the entered email when server EMAIL is %s', async email => { const openIframe = jest.fn() window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) } shop.fetchGuestProducts.mockResolvedValue({ data: { list: [{ ID: 'P1', GOODS_NAME: 'Nigeria product', SELL_PRICE: 62500, MARKET_PRICE: 50000, DISCOUNT: 80, TAX_RATE: 7.5, STORE_NUMS: 5, COIN: 'NGN', LOCAL_CURRENCY_ID: '104' }], totalCount: 1 }}) shop.fetchGuestCart.mockResolvedValue({ data: { sellType: [{ id: 1, sell_type: [{ label: 'pay_stack', name: 'Paystack' }] }], freight: 0, freeShipping: 0, coin: 'NGN' }}) shop.createGuestOrder.mockResolvedValue({ data: { SN: 'SAMPLE-ORDER', DEC_SN: 'SAMPLE-DECLARATION', ORDER_AMOUNT: 399000, PAY_AMOUNT: 399000, PAY_FREIGHT: 0, COUNTRY_ID: '620699181539139625', CURRENCY_ID: '104', EMAIL: email, STATUS: 0 }}) const vm = mount(Flow, { market: 'ng', stage: 'catalog' }) vm.publicKey = 'sample-key' await settle() vm.toggleProduct(vm.products[0]) vm.stage = 'checkout' await settle() Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', province: ['R1'], lgaName: 'District', cityName: 'Lagos', detailaddress: 'Street' }) const emailInput = vm.$el.querySelector('#ng-guest-email') emailInput.value = ' buyer@example.test ' emailInput.dispatchEvent(new Event('input', { bubbles: true })) vm.$el.querySelector('.ng-purchase-place-order').click() await settle() const dialog = descendants(vm).find(child => child.$options.name === 'PaystackPaymentDialog') dialog.$el.querySelector('.ng-paystack-payment-continue').click() await settle() expect(openIframe).toHaveBeenCalledTimes(1) expect(shop.createGuestOrder).toHaveBeenCalledWith(expect.objectContaining({ email: 'buyer@example.test' })) expect(window.PaystackPop.setup.mock.calls[0][0]).toMatchObject({ amount: 39900000, currency: 'NGN', email: 'buyer@example.test', metadata: { cart_id: 'SAMPLE-ORDER' }}) expect(dialog.$el.querySelector('input')).toBeNull() expect(vm.$el.textContent).not.toContain('Your unpaid order is saved for this visit.') expect(push).not.toHaveBeenCalled() window.PaystackPop.setup.mock.calls[0][0].onClose() await settle() vm.openEmail() await settle() dialog.$el.querySelector('.ng-paystack-payment-continue').click() await settle() expect(shop.createGuestOrder).toHaveBeenCalledTimes(1) expect(openIframe).toHaveBeenCalledTimes(2) expect(window.PaystackPop.setup.mock.calls[1][0].email).toBe('buyer@example.test') window.PaystackPop.setup.mock.calls[1][0].callback({ reference: 'sample-reference' }) await settle() expect(push).toHaveBeenCalledWith({ path: '/guest-order', query: { order: 'SAMPLE-ORDER' }}) }) it('does not turn a backend guest Cash option into a member balance', async() => { const vm = mount(Flow, { market: 'ae', stage: 'catalog' }) await settle() vm.toggleProduct(vm.products[0]) vm.stage = 'checkout' vm.payType = 'cash' await settle() expect(vm.$el.querySelector('.ng-payment-methods').textContent).not.toContain('Account Balance') expect(vm.$el.textContent).not.toContain('Available balance') }) it('loads the supplied order reference without fabricating paid data', async() => { Vue.prototype.$route = { query: { order: 'REAL-ORDER' }} Vue.prototype.$router.resolve = () => ({ href: '/#/guest-order?order=REAL-ORDER' }) shop.fetchGuestOrderDetail.mockResolvedValue({ data: { order: { SN: 'REAL-ORDER', STATUS: 'Unpaid', CURRENCY: 'NGN' }, goods: [] }}) const vm = mount(Order) await settle() expect(vm.$el.textContent).toContain('Order REAL-ORDER') expect(vm.$el.textContent).toContain('Item details are unavailable') expect(vm.$el.textContent).toContain('Unpaid') expect(shop.fetchGuestOrderDetail).toHaveBeenCalledWith({ orderSn: 'REAL-ORDER' }) expect(shop.fetchGuestProducts).not.toHaveBeenCalled() })