Skip to content

Commit fa79a00

Browse files
Pato9310claude
andcommitted
test: add unit and e2e tests for RNAppModule TurboModule support
- Add jest mock for NativeAppModule in jest.setup.ts - Add unit tests for GoogleMobileAdsNativeEventEmitter in __tests__/ - Add e2e test for module initialization in e2e/admob.e2e.js Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 713b229 commit fa79a00

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
});

e2e/admob.e2e.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
*/
1717

1818
describe('googleAds', function () {
19+
describe('module initialization', function () {
20+
it('should initialize without TurboModule errors', async function () {
21+
// This test verifies that the app initializes correctly with TurboModule support
22+
// for RNAppModule (fixes RN 0.83+ compatibility)
23+
await expect(element(by.text('Select test to run'))).toBeVisible();
24+
// If we reach this point, RNAppModule TurboModule loaded successfully
25+
});
26+
});
27+
1928
describe('setRequestConfiguration()', function () {
2029
it('should match text in the basic app element', async function () {
2130
await expect(element(by.text('Select test to run'))).toBeVisible();

jest.setup.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,26 @@ jest.doMock('./src/specs/modules/NativeInterstitialModule', () => {
6767
},
6868
};
6969
});
70+
jest.doMock('./src/specs/modules/NativeAppModule', () => {
71+
return {
72+
__esModule: true,
73+
default: {
74+
addListener: jest.fn(),
75+
removeListeners: jest.fn(),
76+
eventsAddListener: jest.fn(),
77+
eventsRemoveListener: jest.fn(),
78+
eventsNotifyReady: jest.fn(),
79+
initializeApp: jest.fn(),
80+
setAutomaticDataCollectionEnabled: jest.fn(),
81+
deleteApp: jest.fn(),
82+
eventsGetListeners: jest.fn(),
83+
eventsPing: jest.fn(),
84+
metaGetAll: jest.fn(),
85+
jsonGetAll: jest.fn(),
86+
preferencesSetBool: jest.fn(),
87+
preferencesSetString: jest.fn(),
88+
preferencesGetAll: jest.fn(),
89+
preferencesClearAll: jest.fn(),
90+
},
91+
};
92+
});

0 commit comments

Comments
 (0)