guestShopPaystack.spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* eslint-env jest */
  2. import Vue from 'vue'
  3. import OrderPaystack from '@/ui-refactor/views/guest-shop/OrderPaystack'
  4. jest.mock('vue-paystack', () => ({
  5. props: ['paystackkey', 'email', 'amount', 'currency', 'reference', 'metadata', 'channels', 'callback', 'close'],
  6. created() { this.scriptLoaded = new Promise(resolve => this.loadScript(resolve)) },
  7. render: h => h('button')
  8. }))
  9. const instances = []
  10. function instance(currency = 'USD', email = 'guest@example.test') {
  11. const vm = new (Vue.extend(OrderPaystack))({ propsData: { paystackkey: 'test-key', email, amount: 29, currency, reference: 'guest-ORDER1-1', metadata: { cart_id: 'ORDER1' }, channels: ['card'], callback: jest.fn(), close: jest.fn() }})
  12. instances.push(vm)
  13. return vm
  14. }
  15. afterEach(() => {
  16. instances.splice(0).forEach(vm => vm.$destroy())
  17. document.querySelectorAll('script[src="https://js.paystack.co/v1/inline.js"]').forEach(script => script.remove())
  18. delete window.PaystackPop
  19. })
  20. it.each(['NGN', 'AED', 'USD'])('always sends NGN for source currency %s, preserving V1 metadata and subunits', async currency => {
  21. const openIframe = jest.fn()
  22. window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) }
  23. const vm = instance(currency)
  24. await vm.payWithPaystack()
  25. const options = window.PaystackPop.setup.mock.calls[0][0]
  26. expect(options).toMatchObject({ amount: 29, currency: 'NGN', email: 'guest@example.test', ref: 'guest-ORDER1-1', metadata: { cart_id: 'ORDER1' }})
  27. options.callback({ reference: 'guest-ORDER1-1' })
  28. options.onClose()
  29. expect(vm.callback).toHaveBeenCalledTimes(1)
  30. expect(vm.close).toHaveBeenCalledTimes(1)
  31. expect(openIframe).toHaveBeenCalledTimes(1)
  32. })
  33. it('reports script load failure and allows the page to recover', async() => {
  34. const vm = instance()
  35. const error = jest.fn()
  36. vm.$on('error', error)
  37. document.querySelector('script[src="https://js.paystack.co/v1/inline.js"]').onerror()
  38. await vm.payWithPaystack()
  39. expect(error).toHaveBeenCalledWith(expect.any(Error))
  40. })
  41. it('passes an empty email to Paystack without adding an email requirement', async() => {
  42. const openIframe = jest.fn()
  43. window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) }
  44. const vm = instance('NGN', '')
  45. await vm.payWithPaystack()
  46. expect(window.PaystackPop.setup).toHaveBeenCalledWith(expect.objectContaining({ email: '', currency: 'NGN', amount: 29 }))
  47. expect(openIframe).toHaveBeenCalledTimes(1)
  48. })
  49. it('reports unsupported currency or SDK setup errors without claiming success', async() => {
  50. window.PaystackPop = { setup: jest.fn(() => { throw new Error('Currency unavailable') }) }
  51. const vm = instance()
  52. const error = jest.fn()
  53. vm.$on('error', error)
  54. await vm.payWithPaystack()
  55. expect(error).toHaveBeenCalledWith(expect.objectContaining({ message: 'Currency unavailable' }))
  56. expect(vm.callback).not.toHaveBeenCalled()
  57. })