guestShopUi.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* eslint-env jest */
  2. import Vue from 'vue'
  3. import ElementUI from 'element-ui'
  4. import path from 'path'
  5. import { load } from './loadGuestComponent'
  6. import * as shop from '@/api/shop'
  7. import { getGuestRegionList } from '@/api/config'
  8. import { toThousandFilter } from '@/filters'
  9. jest.mock('@/api/shop', () => ({ fetchGuestProducts: jest.fn(), fetchGuestCart: jest.fn(), createGuestOrder: jest.fn(), fetchGuestOrderDetail: jest.fn(), downloadGuestOrder: jest.fn(), downloadOrder: jest.fn() }))
  10. jest.mock('@/api/config', () => ({ getGuestRegionList: jest.fn() }))
  11. jest.mock('@/router', () => ({ push: jest.fn(), replace: jest.fn() }))
  12. jest.mock('vue-paystack', () => ({
  13. name: 'Paystack', props: ['amount', 'currency', 'reference', 'email', 'metadata', 'callback', 'close', 'paystackkey', 'channels'],
  14. created() { this.scriptLoaded = Promise.resolve() },
  15. render: h => h('button')
  16. }))
  17. Vue.use(ElementUI)
  18. Vue.component('RouterLink', { render(h) { return h('a', this.$slots.default) } })
  19. Vue.filter('toThousandFilter', toThousandFilter)
  20. const push = jest.fn()
  21. Vue.prototype.$router = { push, replace: jest.fn() }
  22. Vue.prototype.$route = { query: {}}
  23. Vue.prototype.$message = { error: jest.fn(), info: jest.fn() }
  24. const root = path.resolve('src/ui-refactor/views/guest-shop')
  25. const Flow = load(path.join(root, 'GuestPurchaseFlow.vue'))
  26. const MarketDialog = load(path.join(root, 'MarketDialog.vue'))
  27. const Order = load(path.join(root, 'GuestOrderPage.vue'))
  28. const errors = []
  29. const instances = []
  30. Vue.config.errorHandler = error => errors.push(error)
  31. async function settle() { for (let i = 0; i < 16; i++) { await Promise.resolve(); await Vue.nextTick() } }
  32. const descendants = vm => vm.$children.flatMap(child => [child, ...descendants(child)])
  33. function mount(component, propsData = {}) {
  34. const vm = new (Vue.extend(component))({ propsData }).$mount()
  35. document.body.appendChild(vm.$el)
  36. instances.push(vm)
  37. return vm
  38. }
  39. beforeEach(() => {
  40. jest.clearAllMocks()
  41. errors.length = 0
  42. 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 }})
  43. shop.fetchGuestCart.mockResolvedValue({ data: { sellType: [{ id: 1, sell_type: [{ label: 'pay_stack', name: 'Paystack' }, { label: 'cash', name: 'Cash' }] }], freight: 10, freeShipping: 500, coin: 'AED' }})
  44. getGuestRegionList.mockResolvedValue({ data: [{ value: 'R1', label: 'Dubai' }] })
  45. })
  46. afterEach(() => {
  47. instances.splice(0).forEach(vm => { vm.$destroy(); vm.$el.remove() })
  48. delete window.PaystackPop
  49. expect(errors).toEqual([])
  50. })
  51. it('offers the two markets and navigates without asking for credentials', async() => {
  52. const vm = mount(MarketDialog, { visible: true })
  53. await settle()
  54. const options = vm.$el.querySelectorAll('.ng-market-option')
  55. expect(options).toHaveLength(2)
  56. expect(options[1].textContent).toContain('UAE')
  57. options[1].click()
  58. expect(push).toHaveBeenCalledWith({ path: '/guest-shop/ae' })
  59. })
  60. it('hides Sales Price and BV in guest product details while keeping market price and its tax', async() => {
  61. const vm = mount(Flow, { market: 'ae', stage: 'catalog' })
  62. await settle()
  63. vm.$el.querySelector('.ng-signup-product-view').click()
  64. await settle()
  65. const dialog = document.body.querySelector('.ng-product-details-dialog')
  66. expect(dialog.textContent).not.toContain('Sales Price')
  67. expect(dialog.textContent).not.toContain('BV')
  68. expect(dialog.textContent).toContain('AED 150')
  69. expect(dialog.textContent).toContain('AED 10.47')
  70. })
  71. it('connects product, cart and checkout controls without guest BV or member fields', async() => {
  72. const vm = mount(Flow, { market: 'ae', stage: 'catalog' })
  73. await settle()
  74. expect(vm.$el.querySelector('.ng-signup-product-price').textContent).toContain('AED 150')
  75. expect(vm.$el.querySelector('.ng-signup-products').textContent).not.toContain('BV')
  76. vm.$el.querySelector('.ng-signup-product-add').click()
  77. await settle()
  78. vm.stage = 'cart'
  79. await settle()
  80. expect(vm.$el.querySelector('.ng-purchase-cart-item').textContent).toContain('AED 150 each')
  81. const cart = descendants(vm).find(child => child.$options.name === 'GuestCart')
  82. expect(cart.$children[0].$options.name).toBe('PurchaseCartItems')
  83. expect(vm.$el.querySelector('.ng-purchase-summary').textContent).not.toContain('BV')
  84. vm.stage = 'checkout'
  85. await settle()
  86. expect(getGuestRegionList).toHaveBeenCalledWith({ countryId: '620699181539139643' })
  87. const shippingForm = vm.$refs.shipping.$children[0]
  88. expect(shippingForm.$children.some(child => child.$options.name === 'PurchaseCheckoutFields')).toBe(true)
  89. expect(vm.$el.textContent).not.toContain('Member information')
  90. expect(vm.$el.textContent).not.toContain('Reporting Center')
  91. const recipient = vm.$el.querySelector('#ng-guest-consignee')
  92. recipient.value = 'Recipient'
  93. recipient.dispatchEvent(new Event('input', { bubbles: true }))
  94. await settle()
  95. expect(vm.form.consignee).toBe('Recipient')
  96. Object.assign(vm.form, { acceptMobile: '123', email: 'guest@example.test', province: ['R1'], lgaName: 'District', cityName: 'Dubai', detailaddress: 'Street' })
  97. vm.$el.querySelector('.ng-purchase-place-order').click()
  98. await settle()
  99. const dialog = vm.$children.find(child => child.$options.name === 'PaystackPaymentDialog')
  100. expect(dialog.visible).toBe(true)
  101. expect(dialog.$el.querySelector('input')).toBeNull()
  102. expect(dialog.$el.textContent).not.toContain('Confirm your email')
  103. expect(dialog.$el.querySelector('[type=password]')).toBeNull()
  104. })
  105. it('keeps Nigeria merchandise in the product currency and uses the cart coin for payment', async() => {
  106. 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 }})
  107. const vm = mount(Flow, { market: 'ng', stage: 'catalog' })
  108. await settle()
  109. expect(vm.$el.querySelector('.ng-signup-product-price').textContent).toContain('NGN 120')
  110. vm.$el.querySelector('.ng-signup-product-add').click()
  111. vm.stage = 'cart'
  112. await settle()
  113. expect(vm.$el.querySelector('.ng-purchase-cart-item').textContent).toContain('NGN 120 each')
  114. expect(vm.$el.querySelector('.ng-purchase-summary').textContent).toContain('NGN 130')
  115. vm.stage = 'checkout'
  116. await settle()
  117. expect(vm.$el.querySelector('.ng-purchase-summary').textContent).toContain('NGN 130')
  118. Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', email: 'guest@example.test', province: ['R1'], lgaName: 'District', cityName: 'Lagos', detailaddress: 'Street' })
  119. vm.openEmail()
  120. await settle()
  121. const dialog = descendants(vm).find(child => child.$options.name === 'PaystackPaymentDialog')
  122. expect(dialog.visible).toBe(true)
  123. expect(dialog.$el.querySelector('.ng-paystack-payment-amount').textContent).toContain('AED 130')
  124. })
  125. it.each(['ng', 'ae'])('uses the show-cart coin in the %s payment dialog before and after an order is created', async market => {
  126. shop.fetchGuestCart.mockResolvedValue({ data: { sellType: [{ id: 1, sell_type: [{ label: 'pay_stack', name: 'Paystack' }] }], freight: '20', freeShipping: '10000', coin: 'NGN' }})
  127. const vm = mount(Flow, { market, stage: 'catalog' })
  128. await settle()
  129. expect(vm.$el.querySelector('.ng-signup-product-price').textContent).toContain('AED 150')
  130. vm.toggleProduct(vm.products[0])
  131. vm.stage = 'checkout'
  132. await settle()
  133. expect(vm.$el.querySelector('.ng-purchase-summary').textContent).toContain('AED 170')
  134. Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', email: 'guest@example.test', province: ['R1'], lgaName: 'District', cityName: 'City', detailaddress: 'Street' })
  135. vm.openEmail()
  136. await settle()
  137. const dialog = descendants(vm).find(child => child.$options.name === 'PaystackPaymentDialog')
  138. const amount = dialog.$el.querySelector('.ng-paystack-payment-amount strong')
  139. expect(amount.textContent).toBe('NGN 170')
  140. vm.pending = { SN: 'PENDING-ORDER', amount: 1280, currency: 'AED', subunits: 128000 }
  141. await settle()
  142. expect(amount.textContent).toBe('NGN 1,280')
  143. vm.closePayment()
  144. vm.openEmail()
  145. await settle()
  146. expect(amount.textContent).toBe('NGN 1,280')
  147. expect(shop.createGuestOrder).not.toHaveBeenCalled()
  148. })
  149. it.each(['ng', 'ae'])('renders and validates the %s checkout email between phone and region', async market => {
  150. const vm = mount(Flow, { market, stage: 'catalog' })
  151. await settle()
  152. vm.toggleProduct(vm.products[0])
  153. vm.stage = 'checkout'
  154. await settle()
  155. Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', province: ['R1'], lgaName: 'District', cityName: 'City', detailaddress: 'Street' })
  156. const input = vm.$el.querySelector('#ng-guest-email')
  157. expect(input).not.toBeNull()
  158. expect(input.type).toBe('email')
  159. expect(input.autocomplete).toBe('email')
  160. 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'])
  161. vm.$el.querySelector('.ng-purchase-place-order').click()
  162. await settle()
  163. expect(vm.emailVisible).toBe(false)
  164. expect(input.closest('.el-form-item').textContent).toContain('Please enter email.')
  165. input.value = 'invalid'
  166. input.dispatchEvent(new Event('input', { bubbles: true }))
  167. await settle()
  168. expect(vm.form.email).toBe('invalid')
  169. input.dispatchEvent(new Event('blur'))
  170. vm.$el.querySelector('.ng-purchase-place-order').click()
  171. await settle()
  172. expect(vm.emailVisible).toBe(false)
  173. expect(input.closest('.el-form-item').textContent).toContain('Please enter a valid email address.')
  174. expect(shop.createGuestOrder).not.toHaveBeenCalled()
  175. input.value = 'guest@example.test'
  176. input.dispatchEvent(new Event('input', { bubbles: true }))
  177. vm.$el.querySelector('.ng-purchase-place-order').click()
  178. await settle()
  179. expect(vm.form.email).toBe('guest@example.test')
  180. expect(vm.emailVisible).toBe(true)
  181. })
  182. it.each(['visitor@example.test', ''])('opens Paystack with the entered email when server EMAIL is %s', async email => {
  183. const openIframe = jest.fn()
  184. window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) }
  185. 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 }})
  186. shop.fetchGuestCart.mockResolvedValue({ data: { sellType: [{ id: 1, sell_type: [{ label: 'pay_stack', name: 'Paystack' }] }], freight: 0, freeShipping: 0, coin: 'NGN' }})
  187. 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 }})
  188. const vm = mount(Flow, { market: 'ng', stage: 'catalog' })
  189. vm.publicKey = 'sample-key'
  190. await settle()
  191. vm.toggleProduct(vm.products[0])
  192. vm.stage = 'checkout'
  193. await settle()
  194. Object.assign(vm.form, { consignee: 'Recipient', acceptMobile: '123', province: ['R1'], lgaName: 'District', cityName: 'Lagos', detailaddress: 'Street' })
  195. const emailInput = vm.$el.querySelector('#ng-guest-email')
  196. emailInput.value = ' buyer@example.test '
  197. emailInput.dispatchEvent(new Event('input', { bubbles: true }))
  198. vm.$el.querySelector('.ng-purchase-place-order').click()
  199. await settle()
  200. const dialog = descendants(vm).find(child => child.$options.name === 'PaystackPaymentDialog')
  201. dialog.$el.querySelector('.ng-paystack-payment-continue').click()
  202. await settle()
  203. expect(openIframe).toHaveBeenCalledTimes(1)
  204. expect(shop.createGuestOrder).toHaveBeenCalledWith(expect.objectContaining({ email: 'buyer@example.test' }))
  205. expect(window.PaystackPop.setup.mock.calls[0][0]).toMatchObject({ amount: 39900000, currency: 'NGN', email: 'buyer@example.test', metadata: { cart_id: 'SAMPLE-ORDER' }})
  206. expect(dialog.$el.querySelector('input')).toBeNull()
  207. expect(vm.$el.textContent).not.toContain('Your unpaid order is saved for this visit.')
  208. expect(push).not.toHaveBeenCalled()
  209. window.PaystackPop.setup.mock.calls[0][0].onClose()
  210. await settle()
  211. vm.openEmail()
  212. await settle()
  213. dialog.$el.querySelector('.ng-paystack-payment-continue').click()
  214. await settle()
  215. expect(shop.createGuestOrder).toHaveBeenCalledTimes(1)
  216. expect(openIframe).toHaveBeenCalledTimes(2)
  217. expect(window.PaystackPop.setup.mock.calls[1][0].email).toBe('buyer@example.test')
  218. window.PaystackPop.setup.mock.calls[1][0].callback({ reference: 'sample-reference' })
  219. await settle()
  220. expect(push).toHaveBeenCalledWith({ path: '/guest-order', query: { order: 'SAMPLE-ORDER' }})
  221. })
  222. it('does not turn a backend guest Cash option into a member balance', async() => {
  223. const vm = mount(Flow, { market: 'ae', stage: 'catalog' })
  224. await settle()
  225. vm.toggleProduct(vm.products[0])
  226. vm.stage = 'checkout'
  227. vm.payType = 'cash'
  228. await settle()
  229. expect(vm.$el.querySelector('.ng-payment-methods').textContent).not.toContain('Account Balance')
  230. expect(vm.$el.textContent).not.toContain('Available balance')
  231. })
  232. it('loads the supplied order reference without fabricating paid data', async() => {
  233. Vue.prototype.$route = { query: { order: 'REAL-ORDER' }}
  234. Vue.prototype.$router.resolve = () => ({ href: '/#/guest-order?order=REAL-ORDER' })
  235. shop.fetchGuestOrderDetail.mockResolvedValue({ data: { order: { SN: 'REAL-ORDER', STATUS: 'Unpaid', CURRENCY: 'NGN' }, goods: [] }})
  236. const vm = mount(Order)
  237. await settle()
  238. expect(vm.$el.textContent).toContain('Order REAL-ORDER')
  239. expect(vm.$el.textContent).toContain('Item details are unavailable')
  240. expect(vm.$el.textContent).toContain('Unpaid')
  241. expect(shop.fetchGuestOrderDetail).toHaveBeenCalledWith({ orderSn: 'REAL-ORDER' })
  242. expect(shop.fetchGuestProducts).not.toHaveBeenCalled()
  243. })