| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- /* eslint-env jest */
- import Vue from 'vue'
- import OrderPaystack from '@/ui-refactor/views/guest-shop/OrderPaystack'
- jest.mock('vue-paystack', () => ({
- props: ['paystackkey', 'email', 'amount', 'currency', 'reference', 'metadata', 'channels', 'callback', 'close'],
- created() { this.scriptLoaded = new Promise(resolve => this.loadScript(resolve)) },
- render: h => h('button')
- }))
- const instances = []
- function instance(currency = 'USD', email = 'guest@example.test') {
- 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() }})
- instances.push(vm)
- return vm
- }
- afterEach(() => {
- instances.splice(0).forEach(vm => vm.$destroy())
- document.querySelectorAll('script[src="https://js.paystack.co/v1/inline.js"]').forEach(script => script.remove())
- delete window.PaystackPop
- })
- it.each(['NGN', 'AED', 'USD'])('always sends NGN for source currency %s, preserving V1 metadata and subunits', async currency => {
- const openIframe = jest.fn()
- window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) }
- const vm = instance(currency)
- await vm.payWithPaystack()
- const options = window.PaystackPop.setup.mock.calls[0][0]
- expect(options).toMatchObject({ amount: 29, currency: 'NGN', email: 'guest@example.test', ref: 'guest-ORDER1-1', metadata: { cart_id: 'ORDER1' }})
- options.callback({ reference: 'guest-ORDER1-1' })
- options.onClose()
- expect(vm.callback).toHaveBeenCalledTimes(1)
- expect(vm.close).toHaveBeenCalledTimes(1)
- expect(openIframe).toHaveBeenCalledTimes(1)
- })
- it('reports script load failure and allows the page to recover', async() => {
- const vm = instance()
- const error = jest.fn()
- vm.$on('error', error)
- document.querySelector('script[src="https://js.paystack.co/v1/inline.js"]').onerror()
- await vm.payWithPaystack()
- expect(error).toHaveBeenCalledWith(expect.any(Error))
- })
- it('passes an empty email to Paystack without adding an email requirement', async() => {
- const openIframe = jest.fn()
- window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) }
- const vm = instance('NGN', '')
- await vm.payWithPaystack()
- expect(window.PaystackPop.setup).toHaveBeenCalledWith(expect.objectContaining({ email: '', currency: 'NGN', amount: 29 }))
- expect(openIframe).toHaveBeenCalledTimes(1)
- })
- it('reports unsupported currency or SDK setup errors without claiming success', async() => {
- window.PaystackPop = { setup: jest.fn(() => { throw new Error('Currency unavailable') }) }
- const vm = instance()
- const error = jest.fn()
- vm.$on('error', error)
- await vm.payWithPaystack()
- expect(error).toHaveBeenCalledWith(expect.objectContaining({ message: 'Currency unavailable' }))
- expect(vm.callback).not.toHaveBeenCalled()
- })
|