|
| 1 | +/* eslint-disable no-new */ |
| 2 | + |
| 3 | +import {ShopifyCheckoutKit} from '../src'; |
| 4 | +import {ColorScheme, type Configuration} from '../src'; |
| 5 | +import {NativeModules} from 'react-native'; |
| 6 | + |
| 7 | +const checkoutUrl = 'https://shopify.com/checkout'; |
| 8 | +const config: Configuration = { |
| 9 | + colorScheme: ColorScheme.automatic, |
| 10 | +}; |
| 11 | + |
| 12 | +jest.mock('react-native', () => { |
| 13 | + const NativeEventEmitter = jest.fn(() => ({ |
| 14 | + addListener: jest.fn(), |
| 15 | + removeAllListeners: jest.fn(), |
| 16 | + })); |
| 17 | + |
| 18 | + const exampleConfig = { |
| 19 | + preloading: true, |
| 20 | + }; |
| 21 | + |
| 22 | + const ShopifyCheckoutKit = { |
| 23 | + version: '0.7.0', |
| 24 | + configure: jest.fn(), |
| 25 | + preload: jest.fn(), |
| 26 | + present: jest.fn(), |
| 27 | + getConfig: jest.fn(async () => exampleConfig), |
| 28 | + addEventListener: jest.fn(), |
| 29 | + removeEventListeners: jest.fn(), |
| 30 | + }; |
| 31 | + |
| 32 | + return { |
| 33 | + NativeEventEmitter, |
| 34 | + NativeModules: { |
| 35 | + ShopifyCheckoutKit, |
| 36 | + }, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +describe('ShopifyCheckoutKit', () => { |
| 41 | + afterEach(() => { |
| 42 | + NativeModules.ShopifyCheckoutKit.configure.mockReset(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('instantiation', () => { |
| 46 | + it('calls `configure` with the specified config on instantiation', () => { |
| 47 | + new ShopifyCheckoutKit(config); |
| 48 | + expect(NativeModules.ShopifyCheckoutKit.configure).toHaveBeenCalledWith( |
| 49 | + config, |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('does not call `configure` if no config was specified on instantiation', () => { |
| 54 | + new ShopifyCheckoutKit(); |
| 55 | + expect(NativeModules.ShopifyCheckoutKit.configure).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('configure', () => { |
| 60 | + it('calls the `configure` on the Native Module', () => { |
| 61 | + const instance = new ShopifyCheckoutKit(); |
| 62 | + instance.configure(config); |
| 63 | + expect(NativeModules.ShopifyCheckoutKit.configure).toHaveBeenCalledTimes( |
| 64 | + 1, |
| 65 | + ); |
| 66 | + expect(NativeModules.ShopifyCheckoutKit.configure).toHaveBeenCalledWith( |
| 67 | + config, |
| 68 | + ); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('preload', () => { |
| 73 | + it('calls `preload` with a checkout URL', () => { |
| 74 | + const instance = new ShopifyCheckoutKit(); |
| 75 | + instance.preload(checkoutUrl); |
| 76 | + expect(NativeModules.ShopifyCheckoutKit.preload).toHaveBeenCalledTimes(1); |
| 77 | + expect(NativeModules.ShopifyCheckoutKit.preload).toHaveBeenCalledWith( |
| 78 | + checkoutUrl, |
| 79 | + ); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('present', () => { |
| 84 | + it('calls `present` with a checkout URL', () => { |
| 85 | + const instance = new ShopifyCheckoutKit(); |
| 86 | + instance.present(checkoutUrl); |
| 87 | + expect(NativeModules.ShopifyCheckoutKit.present).toHaveBeenCalledTimes(1); |
| 88 | + expect(NativeModules.ShopifyCheckoutKit.present).toHaveBeenCalledWith( |
| 89 | + checkoutUrl, |
| 90 | + ); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('getConfig', () => { |
| 95 | + it('returns the config from the Native Module', async () => { |
| 96 | + const instance = new ShopifyCheckoutKit(); |
| 97 | + await expect(instance.getConfig()).resolves.toStrictEqual({ |
| 98 | + preloading: true, |
| 99 | + }); |
| 100 | + expect(NativeModules.ShopifyCheckoutKit.getConfig).toHaveBeenCalledTimes( |
| 101 | + 1, |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('addEventListener', () => { |
| 107 | + it('creates a new event listener for a specific event', () => { |
| 108 | + const instance = new ShopifyCheckoutKit(); |
| 109 | + const eventName = 'close'; |
| 110 | + const callback = jest.fn(); |
| 111 | + instance.addEventListener(eventName, callback); |
| 112 | + // @ts-expect-error |
| 113 | + expect(instance.eventEmitter.addListener).toHaveBeenCalledWith( |
| 114 | + eventName, |
| 115 | + callback, |
| 116 | + ); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('removeEventListeners', () => { |
| 121 | + it('Removes all listeners for a specific event', () => { |
| 122 | + const instance = new ShopifyCheckoutKit(); |
| 123 | + instance.addEventListener('close', () => {}); |
| 124 | + instance.addEventListener('close', () => {}); |
| 125 | + instance.removeEventListeners('close'); |
| 126 | + // @ts-expect-error |
| 127 | + expect(instance.eventEmitter.removeAllListeners).toHaveBeenCalledWith( |
| 128 | + 'close', |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments