| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- /* 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() {
- 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() }})
- 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('uses the existing V1 SN metadata contract and never scales subunits again', async() => {
- const openIframe = jest.fn()
- window.PaystackPop = { setup: jest.fn(() => ({ openIframe })) }
- const vm = instance()
- await vm.payWithPaystack()
- const options = window.PaystackPop.setup.mock.calls[0][0]
- expect(options).toMatchObject({ amount: 29, currency: 'USD', 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('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()
- })
|