|
| 1 | +import { createOnboardingEventHandlers } from './create-onboarding-event-handlers'; |
| 2 | + |
| 3 | +jest.mock('@/bridge', () => { |
| 4 | + const actual = jest.requireActual('@/bridge'); |
| 5 | + return { |
| 6 | + ...actual, |
| 7 | + $bridge: { |
| 8 | + addEventListener: jest.fn(() => ({ remove: jest.fn() })), |
| 9 | + removeAllEventListeners: jest.fn(), |
| 10 | + }, |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +jest.mock('./onboarding-view-emitter', () => { |
| 15 | + return { |
| 16 | + OnboardingViewEmitter: jest.fn().mockImplementation(() => ({ |
| 17 | + addListener: jest.fn(), |
| 18 | + removeAllListeners: jest.fn(), |
| 19 | + })), |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +describe('createOnboardingEventHandlers', () => { |
| 24 | + beforeEach(() => { |
| 25 | + jest.clearAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('creates OnboardingViewEmitter with provided viewId', () => { |
| 29 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 30 | + './onboarding-view-emitter', |
| 31 | + ); |
| 32 | + const viewId = 'test-onboarding-view-id-123'; |
| 33 | + |
| 34 | + createOnboardingEventHandlers({}, viewId); |
| 35 | + |
| 36 | + expect(OnboardingViewEmitter).toHaveBeenCalledWith(viewId); |
| 37 | + expect(OnboardingViewEmitter).toHaveBeenCalledTimes(1); |
| 38 | + }); |
| 39 | + |
| 40 | + it('merges default handlers with custom handlers', () => { |
| 41 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 42 | + './onboarding-view-emitter', |
| 43 | + ); |
| 44 | + const addListener = jest.fn(); |
| 45 | + (OnboardingViewEmitter as unknown as jest.Mock).mockImplementation(() => ({ |
| 46 | + addListener, |
| 47 | + removeAllListeners: jest.fn(), |
| 48 | + })); |
| 49 | + |
| 50 | + const customHandler = jest.fn(() => false); |
| 51 | + createOnboardingEventHandlers({ onClose: customHandler }, 'test-id'); |
| 52 | + |
| 53 | + // Should register default handlers + custom override |
| 54 | + expect(addListener).toHaveBeenCalled(); |
| 55 | + |
| 56 | + // Check that custom handler was registered |
| 57 | + const calls = (addListener as jest.Mock).mock.calls; |
| 58 | + const closeCall = calls.find(call => call[0] === 'onClose'); |
| 59 | + expect(closeCall).toBeDefined(); |
| 60 | + expect(closeCall[1]).toBe(customHandler); |
| 61 | + }); |
| 62 | + |
| 63 | + it('registers all default handlers when no custom handlers provided', () => { |
| 64 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 65 | + './onboarding-view-emitter', |
| 66 | + ); |
| 67 | + const addListener = jest.fn(); |
| 68 | + (OnboardingViewEmitter as unknown as jest.Mock).mockImplementation(() => ({ |
| 69 | + addListener, |
| 70 | + removeAllListeners: jest.fn(), |
| 71 | + })); |
| 72 | + |
| 73 | + createOnboardingEventHandlers({}, 'test-id'); |
| 74 | + |
| 75 | + // Should register 1 default handler: onClose |
| 76 | + expect(addListener).toHaveBeenCalledTimes(1); |
| 77 | + |
| 78 | + const calls = (addListener as jest.Mock).mock.calls; |
| 79 | + const registeredEvents = calls.map(call => call[0]); |
| 80 | + |
| 81 | + expect(registeredEvents).toContain('onClose'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('registers custom handlers alongside defaults', () => { |
| 85 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 86 | + './onboarding-view-emitter', |
| 87 | + ); |
| 88 | + const addListener = jest.fn(); |
| 89 | + (OnboardingViewEmitter as unknown as jest.Mock).mockImplementation(() => ({ |
| 90 | + addListener, |
| 91 | + removeAllListeners: jest.fn(), |
| 92 | + })); |
| 93 | + |
| 94 | + const customCustomHandler = jest.fn(); |
| 95 | + const customPaywallHandler = jest.fn(); |
| 96 | + |
| 97 | + createOnboardingEventHandlers( |
| 98 | + { |
| 99 | + onCustom: customCustomHandler, |
| 100 | + onPaywall: customPaywallHandler, |
| 101 | + }, |
| 102 | + 'test-id', |
| 103 | + ); |
| 104 | + |
| 105 | + // Should register 1 default + 2 custom = 3 handlers |
| 106 | + expect(addListener).toHaveBeenCalledTimes(3); |
| 107 | + |
| 108 | + const calls = (addListener as jest.Mock).mock.calls; |
| 109 | + const customCall = calls.find(call => call[0] === 'onCustom'); |
| 110 | + const paywallCall = calls.find(call => call[0] === 'onPaywall'); |
| 111 | + |
| 112 | + expect(customCall[1]).toBe(customCustomHandler); |
| 113 | + expect(paywallCall[1]).toBe(customPaywallHandler); |
| 114 | + }); |
| 115 | + |
| 116 | + it('passes onRequestClose to addListener', () => { |
| 117 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 118 | + './onboarding-view-emitter', |
| 119 | + ); |
| 120 | + const addListener = jest.fn(); |
| 121 | + (OnboardingViewEmitter as unknown as jest.Mock).mockImplementation(() => ({ |
| 122 | + addListener, |
| 123 | + removeAllListeners: jest.fn(), |
| 124 | + })); |
| 125 | + |
| 126 | + const onRequestClose = jest.fn(); |
| 127 | + createOnboardingEventHandlers({}, 'test-id', onRequestClose); |
| 128 | + |
| 129 | + // All addListener calls should receive the onRequestClose function |
| 130 | + const calls = (addListener as jest.Mock).mock.calls; |
| 131 | + calls.forEach(call => { |
| 132 | + expect(call[2]).toBe(onRequestClose); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('returns unsubscribe function', () => { |
| 137 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 138 | + './onboarding-view-emitter', |
| 139 | + ); |
| 140 | + const removeAllListeners = jest.fn(); |
| 141 | + (OnboardingViewEmitter as unknown as jest.Mock).mockImplementation(() => ({ |
| 142 | + addListener: jest.fn(), |
| 143 | + removeAllListeners, |
| 144 | + })); |
| 145 | + |
| 146 | + const unsubscribe = createOnboardingEventHandlers({}, 'test-id'); |
| 147 | + |
| 148 | + expect(typeof unsubscribe).toBe('function'); |
| 149 | + |
| 150 | + unsubscribe(); |
| 151 | + |
| 152 | + expect(removeAllListeners).toHaveBeenCalledTimes(1); |
| 153 | + }); |
| 154 | + |
| 155 | + it('custom handlers override default handlers', () => { |
| 156 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 157 | + './onboarding-view-emitter', |
| 158 | + ); |
| 159 | + const addListener = jest.fn(); |
| 160 | + (OnboardingViewEmitter as unknown as jest.Mock).mockImplementation(() => ({ |
| 161 | + addListener, |
| 162 | + removeAllListeners: jest.fn(), |
| 163 | + })); |
| 164 | + |
| 165 | + const customCloseHandler = jest.fn(() => false); |
| 166 | + |
| 167 | + createOnboardingEventHandlers( |
| 168 | + { |
| 169 | + onClose: customCloseHandler, |
| 170 | + }, |
| 171 | + 'test-id', |
| 172 | + ); |
| 173 | + |
| 174 | + const calls = (addListener as jest.Mock).mock.calls; |
| 175 | + const closeCall = calls.find(call => call[0] === 'onClose'); |
| 176 | + |
| 177 | + // Custom handler should be used instead of default |
| 178 | + expect(closeCall[1]).toBe(customCloseHandler); |
| 179 | + |
| 180 | + // Should still have only 1 handler, because custom one overrides default |
| 181 | + expect(addListener).toHaveBeenCalledTimes(1); |
| 182 | + }); |
| 183 | + |
| 184 | + it('creates default onRequestClose when not provided', () => { |
| 185 | + const { OnboardingViewEmitter } = jest.requireMock( |
| 186 | + './onboarding-view-emitter', |
| 187 | + ); |
| 188 | + const addListener = jest.fn(); |
| 189 | + (OnboardingViewEmitter as unknown as jest.Mock).mockImplementation(() => ({ |
| 190 | + addListener, |
| 191 | + removeAllListeners: jest.fn(), |
| 192 | + })); |
| 193 | + |
| 194 | + createOnboardingEventHandlers({}, 'test-id'); |
| 195 | + |
| 196 | + // Should not throw, default async noop function should be created |
| 197 | + const calls = (addListener as jest.Mock).mock.calls; |
| 198 | + expect(calls[0][2]).toBeDefined(); |
| 199 | + expect(typeof calls[0][2]).toBe('function'); |
| 200 | + }); |
| 201 | +}); |
0 commit comments