paystackPaymentDialog.spec.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* eslint-env jest */
  2. import Vue from 'vue'
  3. import ElementUI from 'element-ui'
  4. import path from 'path'
  5. import { load } from '@/ui-refactor/views/purchase/__tests__/loadComponent'
  6. jest.mock('@/utils/usersInfo', () => ({}))
  7. Vue.use(ElementUI)
  8. const PaymentDialog = load(path.resolve('src/ui-refactor/components/PaystackPaymentDialog.vue'))
  9. const instances = []
  10. async function settle() { await Vue.nextTick(); jest.advanceTimersByTime(350); await Vue.nextTick() }
  11. beforeEach(() => jest.useFakeTimers())
  12. afterEach(() => {
  13. instances.splice(0).forEach(vm => { vm.$destroy(); vm.$el.remove() })
  14. jest.clearAllTimers()
  15. jest.useRealTimers()
  16. })
  17. it.each([false, true])('preserves editable/readonly email and delegates payment and cancellation (readonly=%s)', async readonlyEmail => {
  18. const changed = jest.fn()
  19. const continued = jest.fn()
  20. const cancelled = jest.fn()
  21. const vm = new Vue({
  22. data: () => ({ visible: true, email: 'member@example.test', loading: false }),
  23. render(h) {
  24. return h(PaymentDialog, {
  25. props: { visible: this.visible, email: this.email, amount: 104000, currency: 'NGN', readonlyEmail, loading: this.loading },
  26. attrs: { 'before-close': cancelled, 'close-on-press-escape': false },
  27. on: { 'update:email': value => { this.email = value; changed(value) }, continue: continued }
  28. })
  29. }
  30. }).$mount()
  31. document.body.appendChild(vm.$el)
  32. instances.push(vm)
  33. await settle()
  34. const input = vm.$el.querySelector('input')
  35. expect(input.value).toBe('member@example.test')
  36. expect(input.readOnly).toBe(readonlyEmail)
  37. expect(vm.$el.querySelector('.ng-paystack-payment-amount').textContent).toContain('NGN 104,000')
  38. if (!readonlyEmail) {
  39. input.value = 'confirmed@example.test'
  40. input.dispatchEvent(new Event('input', { bubbles: true }))
  41. expect(changed).toHaveBeenCalledWith('confirmed@example.test')
  42. }
  43. const button = vm.$el.querySelector('.ng-paystack-payment-continue')
  44. button.click()
  45. expect(continued).toHaveBeenCalledTimes(1)
  46. vm.loading = true
  47. await settle()
  48. expect(input.disabled).toBe(true)
  49. expect(button.disabled).toBe(true)
  50. expect(vm.$el.querySelector('.el-dialog__headerbtn')).toBeNull()
  51. button.click()
  52. expect(continued).toHaveBeenCalledTimes(1)
  53. vm.loading = false
  54. await settle()
  55. vm.$el.querySelector('.el-dialog__headerbtn').click()
  56. expect(cancelled).toHaveBeenCalledTimes(1)
  57. expect(vm.visible).toBe(true)
  58. vm.visible = false
  59. await Vue.nextTick()
  60. button.click()
  61. expect(continued).toHaveBeenCalledTimes(1)
  62. })