guestShopPaystack.spec.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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() {
  11. const vm = new (Vue.extend(OrderPaystack))({ propsData: { paystackkey: 'test-key', email: 'guest@example.test', amount: 29, currency: 'USD', 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('uses the existing V1 SN metadata contract and never scales subunits again', async() => {
  21. const openIframe = jest.fn()
  22. window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) }
  23. const vm = instance()
  24. await vm.payWithPaystack()
  25. const options = window.PaystackPop.setup.mock.calls[0][0]
  26. expect(options).toMatchObject({ amount: 29, currency: 'USD', 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('reports unsupported currency or SDK setup errors without claiming success', async() => {
  42. window.PaystackPop = { setup: jest.fn(() => { throw new Error('Currency unavailable') }) }
  43. const vm = instance()
  44. const error = jest.fn()
  45. vm.$on('error', error)
  46. await vm.payWithPaystack()
  47. expect(error).toHaveBeenCalledWith(expect.objectContaining({ message: 'Currency unavailable' }))
  48. expect(vm.callback).not.toHaveBeenCalled()
  49. })