|
| 1 | +import { GoogleMobileAdsNativeEventEmitter } from '../src/internal/GoogleMobileAdsNativeEventEmitter'; |
| 2 | +import NativeAppModule from '../src/specs/modules/NativeAppModule'; |
| 3 | + |
| 4 | +describe('GoogleMobileAdsNativeEventEmitter', function () { |
| 5 | + beforeEach(() => { |
| 6 | + jest.clearAllMocks(); |
| 7 | + }); |
| 8 | + |
| 9 | + describe('NativeAppModule TurboModule', function () { |
| 10 | + it('should be defined', function () { |
| 11 | + expect(NativeAppModule).toBeDefined(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should have eventsNotifyReady method', function () { |
| 15 | + expect(NativeAppModule.eventsNotifyReady).toBeDefined(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should have eventsAddListener method', function () { |
| 19 | + expect(NativeAppModule.eventsAddListener).toBeDefined(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should have eventsRemoveListener method', function () { |
| 23 | + expect(NativeAppModule.eventsRemoveListener).toBeDefined(); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('addListener', function () { |
| 28 | + it('calls eventsNotifyReady on first listener', function () { |
| 29 | + const emitter = GoogleMobileAdsNativeEventEmitter; |
| 30 | + // Reset ready state for testing |
| 31 | + (emitter as any).ready = false; |
| 32 | + |
| 33 | + const listener = jest.fn(); |
| 34 | + emitter.addListener('test_event', listener); |
| 35 | + |
| 36 | + expect(NativeAppModule.eventsNotifyReady).toHaveBeenCalledWith(true); |
| 37 | + expect(NativeAppModule.eventsAddListener).toHaveBeenCalledWith('test_event'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('does not call eventsNotifyReady on subsequent listeners', function () { |
| 41 | + const emitter = GoogleMobileAdsNativeEventEmitter; |
| 42 | + // Set ready state to true |
| 43 | + (emitter as any).ready = true; |
| 44 | + |
| 45 | + jest.clearAllMocks(); |
| 46 | + |
| 47 | + const listener = jest.fn(); |
| 48 | + emitter.addListener('another_event', listener); |
| 49 | + |
| 50 | + expect(NativeAppModule.eventsNotifyReady).not.toHaveBeenCalled(); |
| 51 | + expect(NativeAppModule.eventsAddListener).toHaveBeenCalledWith('another_event'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns a subscription with remove function', function () { |
| 55 | + const emitter = GoogleMobileAdsNativeEventEmitter; |
| 56 | + const listener = jest.fn(); |
| 57 | + const subscription = emitter.addListener('remove_test', listener); |
| 58 | + |
| 59 | + expect(subscription).toBeDefined(); |
| 60 | + expect(typeof subscription.remove).toBe('function'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('removeAllListeners', function () { |
| 65 | + it('calls eventsRemoveListener with all flag', function () { |
| 66 | + const emitter = GoogleMobileAdsNativeEventEmitter; |
| 67 | + emitter.removeAllListeners('cleanup_event'); |
| 68 | + |
| 69 | + expect(NativeAppModule.eventsRemoveListener).toHaveBeenCalledWith('cleanup_event', true); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments