guestShopOrder.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 { fetchGuestOrderDetail, downloadGuestOrder, downloadOrder } from '@/api/shop'
  7. import handleClipboard from '@/utils/clipboard'
  8. import usersInfo from '@/utils/usersInfo'
  9. jest.mock('@/api/shop', () => ({ fetchGuestOrderDetail: jest.fn(), downloadGuestOrder: jest.fn(), downloadOrder: jest.fn() }))
  10. jest.mock('@/utils/clipboard', () => jest.fn())
  11. jest.mock('@/utils/usersInfo', () => ({ accessToken: jest.fn() }))
  12. jest.mock('@/router', () => ({ push: jest.fn(), replace: jest.fn() }))
  13. Vue.use(ElementUI)
  14. const Order = load(path.resolve('src/ui-refactor/views/guest-shop/GuestOrderPage.vue'))
  15. const sample = {
  16. SN: 'SAMPLE-ORDER',
  17. STATUS: 'Unpaid',
  18. CREATED_AT: 1792000000,
  19. CURRENCY: 'NGN',
  20. ORDER_AMOUNT: 38000,
  21. PAY_AMOUNT: 38100,
  22. PAY_FREIGHT: 100,
  23. CONSIGNEE: 'Sample recipient',
  24. MOBILE: '+234123456789',
  25. FULL_ADDRESS: 'Sample street, Lagos',
  26. COUNTRY: 'Nigeria',
  27. PAY_TYPE: 'pay_stack',
  28. hasChildren: [
  29. {
  30. GOODS_TITLE: 'Sample product',
  31. SKU_CODE: 'SKU-1',
  32. COVER: 'sample.png',
  33. REAL_PRICE: 19000,
  34. BUY_NUMS: 2,
  35. TAX_RATE: 7.5
  36. }
  37. ]
  38. }
  39. const response = (order = sample) => ({ data: { order, goods: order.hasChildren || [] }})
  40. const instances = []
  41. const errors = []
  42. Vue.config.errorHandler = error => errors.push(error)
  43. async function settle() {
  44. for (let i = 0; i < 12; i++) {
  45. await Promise.resolve()
  46. await Vue.nextTick()
  47. }
  48. }
  49. function mount(order = 'SAMPLE-ORDER') {
  50. const route = Vue.observable({ query: { order }})
  51. const router = {
  52. push: jest.fn(),
  53. resolve: jest.fn(() => ({ href: '/app/#/guest-order?order=' + encodeURIComponent(order) }))
  54. }
  55. const vm = new (Vue.extend(Order))({
  56. beforeCreate() {
  57. this.$route = route
  58. this.$router = router
  59. }
  60. }).$mount()
  61. document.body.appendChild(vm.$el)
  62. instances.push(vm)
  63. return vm
  64. }
  65. beforeEach(() => {
  66. jest.clearAllMocks()
  67. errors.length = 0
  68. usersInfo.accessToken.mockReturnValue('')
  69. process.env.VUE_APP_BASE_DO_API = 'https://api.example.test'
  70. fetchGuestOrderDetail.mockResolvedValue(response())
  71. downloadGuestOrder.mockResolvedValue({
  72. data: { fileUrl: 'receipts/sample.pdf', targetName: 'sample.pdf' }
  73. })
  74. downloadOrder.mockResolvedValue({ data: { fileUrl: 'receipts/member.pdf', targetName: 'member.pdf' }})
  75. })
  76. afterEach(() => {
  77. instances.splice(0).forEach(vm => {
  78. vm.$destroy()
  79. vm.$el.remove()
  80. })
  81. jest.restoreAllMocks()
  82. expect(errors).toEqual([])
  83. })
  84. it('requests the anonymous detail by the URL reference and renders server data without adding tax', async() => {
  85. const vm = mount()
  86. await settle()
  87. expect(fetchGuestOrderDetail).toHaveBeenCalledWith({ orderSn: 'SAMPLE-ORDER' })
  88. const text = vm.$el.textContent
  89. for (const value of [
  90. 'Order SAMPLE-ORDER',
  91. 'Sample product',
  92. 'SKU-1',
  93. 'Sample recipient',
  94. '+234123456789',
  95. 'Sample street, Lagos',
  96. 'NGN 38,100',
  97. 'Unpaid',
  98. 'Paystack'
  99. ]) { expect(text).toContain(value) }
  100. expect(text).not.toContain('40,950')
  101. expect(text).not.toContain('BV')
  102. })
  103. it('does not request a missing reference', async() => {
  104. const vm = mount('')
  105. await settle()
  106. expect(fetchGuestOrderDetail).not.toHaveBeenCalled()
  107. expect(vm.$el.textContent).toContain('Order reference unavailable')
  108. })
  109. it('supports a direct link and a fresh mount without saved receipts', async() => {
  110. const storage = jest.spyOn(Storage.prototype, 'setItem')
  111. mount()
  112. await settle()
  113. mount()
  114. await settle()
  115. expect(fetchGuestOrderDetail).toHaveBeenCalledTimes(2)
  116. expect(storage).not.toHaveBeenCalled()
  117. })
  118. it('clears old details while querying another order and ignores the older response', async() => {
  119. let oldResponse
  120. fetchGuestOrderDetail.mockImplementationOnce(
  121. () =>
  122. new Promise(resolve => {
  123. oldResponse = resolve
  124. })
  125. )
  126. const vm = mount()
  127. vm.$route.query = { order: 'SECOND-ORDER' }
  128. fetchGuestOrderDetail.mockResolvedValue(
  129. response({ ...sample, SN: 'SECOND-ORDER', CONSIGNEE: 'New recipient' })
  130. )
  131. await settle()
  132. oldResponse(response())
  133. await settle()
  134. expect(vm.$el.textContent).toContain('Order SECOND-ORDER')
  135. expect(vm.$el.textContent).toContain('New recipient')
  136. expect(vm.$el.textContent).not.toContain('Sample recipient')
  137. })
  138. it('ignores a response after the page is destroyed', async() => {
  139. let finish
  140. fetchGuestOrderDetail.mockImplementationOnce(
  141. () =>
  142. new Promise(resolve => {
  143. finish = resolve
  144. })
  145. )
  146. const vm = mount()
  147. vm.$destroy()
  148. finish(response())
  149. await settle()
  150. expect(vm.detail).toBeNull()
  151. })
  152. it('shows an error with retry and recovers from a failed query', async() => {
  153. fetchGuestOrderDetail.mockRejectedValueOnce(new Error('not found'))
  154. const vm = mount()
  155. await settle()
  156. expect(vm.$el.textContent).toContain('Unable to load this order')
  157. const retry = [...vm.$el.querySelectorAll('button')].find(button =>
  158. button.textContent.includes('Retry')
  159. )
  160. retry.click()
  161. await settle()
  162. expect(vm.$el.textContent).toContain('Sample product')
  163. })
  164. it('rejects an unexpected order returned for this reference', async() => {
  165. fetchGuestOrderDetail.mockResolvedValue(response({ ...sample, SN: 'ANOTHER-ORDER' }))
  166. const vm = mount()
  167. await settle()
  168. expect(vm.$el.textContent).toContain('Unable to load this order')
  169. expect(vm.$el.textContent).not.toContain('Sample recipient')
  170. })
  171. it('uses the router and current origin for copy links, excluding personal data', async() => {
  172. const vm = mount()
  173. await settle()
  174. const button = [...vm.$el.querySelectorAll('button')].find(
  175. button => button.textContent.trim() === 'Copy'
  176. )
  177. button.click()
  178. expect(handleClipboard.mock.calls[0][0]).toBe(
  179. window.location.origin + '/app/#/guest-order?order=SAMPLE-ORDER'
  180. )
  181. expect(vm.$router.resolve).toHaveBeenCalledWith({
  182. path: '/guest-order',
  183. query: { order: 'SAMPLE-ORDER' }
  184. })
  185. })
  186. it('downloads through the anonymous receipt endpoint and prevents double clicks', async() => {
  187. let complete
  188. downloadGuestOrder.mockImplementationOnce(
  189. () =>
  190. new Promise(resolve => {
  191. complete = resolve
  192. })
  193. )
  194. const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
  195. const vm = mount()
  196. await settle()
  197. vm.downloadReceipt()
  198. vm.downloadReceipt()
  199. expect(downloadGuestOrder).toHaveBeenCalledTimes(1)
  200. expect(downloadGuestOrder).toHaveBeenCalledWith('SAMPLE-ORDER')
  201. expect(downloadOrder).not.toHaveBeenCalled()
  202. complete({ data: { fileUrl: 'receipts/sample.pdf', targetName: 'sample.pdf' }})
  203. await settle()
  204. expect(click.mock.instances[0].href).toBe('https://api.example.test/receipts/sample.pdf')
  205. expect(click.mock.instances[0].rel).toContain('noopener')
  206. expect(vm.downloading).toBe(false)
  207. })
  208. it('uses the original receipt method for a signed-in visitor', async() => {
  209. usersInfo.accessToken.mockReturnValue('sample-session')
  210. jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
  211. const vm = mount()
  212. await settle()
  213. await vm.downloadReceipt()
  214. expect(downloadOrder).toHaveBeenCalledWith('SAMPLE-ORDER')
  215. expect(downloadGuestOrder).not.toHaveBeenCalled()
  216. })
  217. it.each([
  218. () => Promise.reject(new Error('Unavailable')),
  219. () => Promise.resolve({ data: { status: 401, message: 'Unauthorized' }}),
  220. () => Promise.resolve({ data: { fileUrl: 'javascript:alert(1)' }})
  221. ])('recovers from receipt errors without navigating away', async response => {
  222. downloadGuestOrder.mockImplementation(response)
  223. const vm = mount()
  224. await settle()
  225. await vm.downloadReceipt()
  226. expect(vm.$el.textContent).toContain('Receipt download is unavailable')
  227. expect(vm.downloading).toBe(false)
  228. expect(vm.$router.push).not.toHaveBeenCalled()
  229. })
  230. it('does not download or show a stale receipt after switching references', async() => {
  231. let complete
  232. const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
  233. downloadGuestOrder.mockImplementationOnce(
  234. () =>
  235. new Promise(resolve => {
  236. complete = resolve
  237. })
  238. )
  239. const vm = mount()
  240. await settle()
  241. vm.downloadReceipt()
  242. vm.$route.query = { order: 'SECOND-ORDER' }
  243. await settle()
  244. complete({ data: { fileUrl: 'receipts/sample.pdf' }})
  245. await settle()
  246. expect(click).not.toHaveBeenCalled()
  247. })
  248. it('renders the confirmed order/goods response with an empty goods list and missing address', async() => {
  249. fetchGuestOrderDetail.mockResolvedValue({
  250. data: {
  251. order: {
  252. ...sample,
  253. ORDER_AMOUNT: '2999000.00',
  254. PAY_AMOUNT: '2999000.00',
  255. STATUS: 'Paid',
  256. FULL_ADDRESS: '',
  257. PROVINCE: '1',
  258. SHOW_BV: true,
  259. USER_NAME: 'PRIVATE-MEMBER'
  260. },
  261. goods: []
  262. }
  263. })
  264. const vm = mount()
  265. await settle()
  266. expect(vm.$el.textContent).toContain('NGN 2,999,000')
  267. expect(vm.$el.textContent).toContain('Item details are unavailable')
  268. expect(vm.$el.textContent).toContain('Paid')
  269. expect(vm.$el.textContent).not.toContain('PRIVATE-MEMBER')
  270. expect(vm.$el.textContent).not.toContain('BV')
  271. expect(vm.$el.querySelector('.ng-guest-order-address').textContent).toContain('—')
  272. })
  273. it.each(['Paid', 'Unpaid', 'Cancelled', 1, null])(
  274. 'uses the server status %s rather than a payment callback or URL flag',
  275. async status => {
  276. fetchGuestOrderDetail.mockResolvedValue(response({ ...sample, STATUS: status }))
  277. const vm = mount()
  278. vm.$route.query = { order: 'SAMPLE-ORDER', paid: 'true' }
  279. await settle()
  280. const tag = vm.$el.querySelector('.el-tag')
  281. expect(tag.textContent).toBe(
  282. status == null ? 'Status unavailable' : status === 1 ? 'Status: 1' : status
  283. )
  284. expect(tag.classList.contains('el-tag--success')).toBe(status === 'Paid')
  285. }
  286. )
  287. it('keeps zero amounts and missing amounts distinct without inventing a currency', async() => {
  288. fetchGuestOrderDetail.mockResolvedValue({
  289. data: { order: { SN: sample.SN, PAY_AMOUNT: 0, PAY_FREIGHT: 0 }, goods: [] }
  290. })
  291. const vm = mount()
  292. await settle()
  293. expect(vm.$el.querySelector('.ng-guest-order-grand-total strong').textContent).toBe('0.00')
  294. expect(vm.$el.querySelector('.ng-guest-order-totals dd').textContent).toBe('—')
  295. expect(vm.$el.textContent).toContain('Currency information is unavailable')
  296. expect(vm.$el.textContent).not.toContain('NGN')
  297. })
  298. it('uses the response currency and final amount without exchange or another tax charge', async() => {
  299. fetchGuestOrderDetail.mockResolvedValue(
  300. response({
  301. ...sample,
  302. CURRENCY: 'AED',
  303. PAY_AMOUNT: '41000.50',
  304. PAY_AMOUNT_STANDARD: 10,
  305. EXCHANGE_RATE: 1200
  306. })
  307. )
  308. const vm = mount()
  309. await settle()
  310. expect(vm.$el.querySelector('.ng-guest-order-grand-total strong').textContent).toBe(
  311. 'AED 41,000.50'
  312. )
  313. })