@amplytools/react-native-amply-sdk 0.14.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AmplyReactNative.podspec +1 -1
- package/README.md +41 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/tools/amply/sdk/reactnative/AmplyModule.kt +22 -0
- package/android/src/main/java/tools/amply/sdk/reactnative/core/AmplyClient.kt +11 -0
- package/android/src/main/java/tools/amply/sdk/reactnative/core/DefaultAmplyClient.kt +29 -0
- package/android/src/main/java/tools/amply/sdk/reactnative/model/CampaignTouchPayload.kt +12 -0
- package/android/src/newarch/java/tools/amply/sdk/reactnative/NativeAmplyModuleSpec.java +8 -0
- package/android/src/newarch/jni/AmplyReactNative-generated.cpp +12 -0
- package/dist/cjs/campaignTouch.js +44 -0
- package/dist/cjs/campaignTouch.js.map +1 -0
- package/dist/cjs/hooks/useAmplySystemEvents.js +60 -0
- package/dist/cjs/hooks/useAmplySystemEvents.js.map +1 -0
- package/dist/cjs/index.js +723 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/nativeModule.js +55 -0
- package/dist/cjs/nativeModule.js.map +1 -0
- package/dist/cjs/nativeSpecs/NativeAmplyModule.js +5 -0
- package/dist/cjs/nativeSpecs/NativeAmplyModule.js.map +1 -0
- package/dist/cjs/systemEventUtils.js +47 -0
- package/dist/cjs/systemEventUtils.js.map +1 -0
- package/dist/cjs/systemEvents.js +27 -0
- package/dist/cjs/systemEvents.js.map +1 -0
- package/dist/src/campaignTouch.d.ts +36 -0
- package/dist/src/campaignTouch.d.ts.map +1 -0
- package/dist/src/campaignTouch.js +39 -0
- package/dist/src/index.d.ts +35 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +120 -0
- package/dist/src/nativeSpecs/NativeAmplyModule.d.ts +28 -0
- package/dist/src/nativeSpecs/NativeAmplyModule.d.ts.map +1 -1
- package/dist/src/systemEventUtils.d.ts.map +1 -1
- package/dist/src/systemEventUtils.js +3 -1
- package/docs/ARCHITECTURE.md +28 -19
- package/docs/DEVELOPMENT.md +2 -1
- package/ios/Sources/AmplyReactNative/AmplyModule.mm +56 -0
- package/ios/Sources/AmplyReactNative/AmplyReactNative/AmplyReactNative-generated.mm +14 -0
- package/ios/Sources/AmplyReactNative/AmplyReactNative/AmplyReactNative.h +4 -0
- package/package.json +12 -6
- package/scripts/check-package.js +159 -0
- package/src/campaignTouch.ts +74 -0
- package/src/index.ts +124 -0
- package/src/nativeSpecs/NativeAmplyModule.ts +29 -0
- package/src/systemEventUtils.ts +3 -1
- package/dist/index.js +0 -272
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -234
- package/dist/index.mjs.map +0 -1
- package/dist/plugin/index.d.ts +0 -6
- package/dist/plugin/index.d.ts.map +0 -1
- package/dist/plugin/index.js +0 -186
- package/dist/plugin/index.js.map +0 -1
- package/dist/plugin/index.mjs +0 -169
- package/dist/plugin/index.mjs.map +0 -1
- package/dist/plugin/withAmply.d.ts +0 -12
- package/dist/plugin/withAmply.d.ts.map +0 -1
- package/dist/src/__tests__/appLanguage.test.d.ts +0 -43
- package/dist/src/__tests__/appLanguage.test.d.ts.map +0 -1
- package/dist/src/__tests__/appLanguage.test.js +0 -211
- package/dist/src/__tests__/campaignResolved.test.d.ts +0 -2
- package/dist/src/__tests__/campaignResolved.test.d.ts.map +0 -1
- package/dist/src/__tests__/campaignResolved.test.js +0 -208
- package/dist/src/__tests__/gateRegistrationLifecycle.test.d.ts +0 -2
- package/dist/src/__tests__/gateRegistrationLifecycle.test.d.ts.map +0 -1
- package/dist/src/__tests__/gateRegistrationLifecycle.test.js +0 -215
- package/dist/src/__tests__/index.test.d.ts +0 -2
- package/dist/src/__tests__/index.test.d.ts.map +0 -1
- package/dist/src/__tests__/index.test.js +0 -204
- package/dist/src/__tests__/systemEventUtils.test.d.ts +0 -2
- package/dist/src/__tests__/systemEventUtils.test.d.ts.map +0 -1
- package/dist/src/__tests__/systemEventUtils.test.js +0 -56
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/plugin/tsconfig.tsbuildinfo +0 -1
- package/src/__tests__/appLanguage.test.ts +0 -296
- package/src/__tests__/campaignResolved.test.ts +0 -294
- package/src/__tests__/gateRegistrationLifecycle.test.ts +0 -281
- package/src/__tests__/index.test.ts +0 -258
- package/src/__tests__/systemEventUtils.test.ts +0 -75
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
import * as Amply from '../index';
|
|
2
|
-
import {__setNativeModule} from '../nativeModule';
|
|
3
|
-
|
|
4
|
-
type CampaignPresentEvent = {
|
|
5
|
-
url: string;
|
|
6
|
-
params: Record<string, unknown>;
|
|
7
|
-
info: Record<string, unknown>;
|
|
8
|
-
mediationId: string;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
// Drains the microtask queue a few times so promise-chained presenter resolutions
|
|
12
|
-
// (sync or async) settle before assertions run.
|
|
13
|
-
async function flushMicrotasks(): Promise<void> {
|
|
14
|
-
for (let i = 0; i < 5; i++) {
|
|
15
|
-
await Promise.resolve();
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const mockNativeModule = {
|
|
20
|
-
initialize: jest.fn(),
|
|
21
|
-
isInitialized: jest.fn(() => true),
|
|
22
|
-
track: jest.fn(),
|
|
23
|
-
trackGated: jest.fn(),
|
|
24
|
-
resolveCampaign: jest.fn(),
|
|
25
|
-
getRecentEvents: jest.fn(),
|
|
26
|
-
getDataSetSnapshot: jest.fn(),
|
|
27
|
-
registerDeepLinkListener: jest.fn(),
|
|
28
|
-
registerGate: jest.fn(),
|
|
29
|
-
unregisterGate: jest.fn(),
|
|
30
|
-
addListener: jest.fn(),
|
|
31
|
-
removeListeners: jest.fn(),
|
|
32
|
-
onSystemEvent: jest.fn(),
|
|
33
|
-
onDeepLink: jest.fn(),
|
|
34
|
-
onCampaignPresent: jest.fn(),
|
|
35
|
-
setCustomProperties: jest.fn(),
|
|
36
|
-
getCustomProperty: jest.fn(),
|
|
37
|
-
removeCustomProperty: jest.fn(),
|
|
38
|
-
clearCustomProperties: jest.fn(),
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
describe('Amply gate API (trackGated + registerGate)', () => {
|
|
42
|
-
afterEach(() => {
|
|
43
|
-
jest.clearAllMocks();
|
|
44
|
-
Amply.removeAllListeners();
|
|
45
|
-
__setNativeModule(null);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
beforeEach(() => {
|
|
49
|
-
__setNativeModule(mockNativeModule as never);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe('trackEvent (fire-and-forget)', () => {
|
|
53
|
-
it('forwards to native track and never gates', async () => {
|
|
54
|
-
await Amply.trackEvent('SaveTapped', {foo: 'bar'});
|
|
55
|
-
|
|
56
|
-
expect(mockNativeModule.track).toHaveBeenCalledWith({
|
|
57
|
-
name: 'SaveTapped',
|
|
58
|
-
properties: {foo: 'bar'},
|
|
59
|
-
});
|
|
60
|
-
expect(mockNativeModule.trackGated).not.toHaveBeenCalled();
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('defaults missing properties to an empty object', async () => {
|
|
64
|
-
await Amply.trackEvent('Ping');
|
|
65
|
-
|
|
66
|
-
expect(mockNativeModule.track).toHaveBeenCalledWith({
|
|
67
|
-
name: 'Ping',
|
|
68
|
-
properties: {},
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe('trackGated', () => {
|
|
74
|
-
it('resolves proceed/completed when the gate is satisfied', async () => {
|
|
75
|
-
mockNativeModule.trackGated.mockResolvedValueOnce({
|
|
76
|
-
outcome: 'proceed',
|
|
77
|
-
reason: 'completed',
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const decision = await Amply.trackGated('SaveTapped', {foo: 'bar'});
|
|
81
|
-
|
|
82
|
-
expect(mockNativeModule.trackGated).toHaveBeenCalledWith('SaveTapped', {
|
|
83
|
-
foo: 'bar',
|
|
84
|
-
});
|
|
85
|
-
expect(decision).toEqual({outcome: 'proceed', reason: 'completed'});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('resolves proceed/failOpen when the native layer reports fail-open', async () => {
|
|
89
|
-
mockNativeModule.trackGated.mockResolvedValueOnce({
|
|
90
|
-
outcome: 'proceed',
|
|
91
|
-
reason: 'failOpen',
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const decision = await Amply.trackGated('SaveTapped', {});
|
|
95
|
-
|
|
96
|
-
expect(decision).toEqual({outcome: 'proceed', reason: 'failOpen'});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('resolves cancelled on a user dismiss under onAbort=cancel', async () => {
|
|
100
|
-
mockNativeModule.trackGated.mockResolvedValueOnce({outcome: 'cancelled'});
|
|
101
|
-
|
|
102
|
-
const decision = await Amply.trackGated('SaveTapped', undefined);
|
|
103
|
-
|
|
104
|
-
expect(mockNativeModule.trackGated).toHaveBeenCalledWith('SaveTapped', {});
|
|
105
|
-
expect(decision).toEqual({outcome: 'cancelled'});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('never rejects — fails open to proceed/failOpen when native rejects', async () => {
|
|
109
|
-
mockNativeModule.trackGated.mockRejectedValueOnce(new Error('boom'));
|
|
110
|
-
|
|
111
|
-
const decision = await Amply.trackGated('SaveTapped', {});
|
|
112
|
-
|
|
113
|
-
expect(decision).toEqual({outcome: 'proceed', reason: 'failOpen'});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('fails open on an unexpected/malformed native decision', async () => {
|
|
117
|
-
mockNativeModule.trackGated.mockResolvedValueOnce({outcome: 'wat'} as never);
|
|
118
|
-
|
|
119
|
-
const decision = await Amply.trackGated('SaveTapped', {});
|
|
120
|
-
|
|
121
|
-
expect(decision).toEqual({outcome: 'proceed', reason: 'failOpen'});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('normalizes an unknown proceed reason to failOpen', async () => {
|
|
125
|
-
mockNativeModule.trackGated.mockResolvedValueOnce({
|
|
126
|
-
outcome: 'proceed',
|
|
127
|
-
reason: 'something-else',
|
|
128
|
-
} as never);
|
|
129
|
-
|
|
130
|
-
const decision = await Amply.trackGated('SaveTapped', {});
|
|
131
|
-
|
|
132
|
-
expect(decision).toEqual({outcome: 'proceed', reason: 'failOpen'});
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
describe('registerGate', () => {
|
|
137
|
-
it('registers on the native module and subscribes to onCampaignPresent', async () => {
|
|
138
|
-
const remove = jest.fn();
|
|
139
|
-
mockNativeModule.onCampaignPresent.mockReturnValue({remove});
|
|
140
|
-
|
|
141
|
-
const presenter = jest.fn();
|
|
142
|
-
const unsubscribe = await Amply.registerGate('stillframe://ad', presenter);
|
|
143
|
-
|
|
144
|
-
expect(mockNativeModule.registerGate).toHaveBeenCalledTimes(1);
|
|
145
|
-
expect(mockNativeModule.registerGate).toHaveBeenCalledWith('stillframe://ad', 'cancel', 0);
|
|
146
|
-
expect(mockNativeModule.onCampaignPresent).toHaveBeenCalledTimes(1);
|
|
147
|
-
|
|
148
|
-
unsubscribe();
|
|
149
|
-
expect(remove).toHaveBeenCalledTimes(1);
|
|
150
|
-
expect(mockNativeModule.unregisterGate).toHaveBeenCalledWith('stillframe://ad');
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
// This used to assert the opposite — that a second registration re-subscribed but did
|
|
154
|
-
// NOT re-register natively. That encoded the defect: the second subscriber joined the
|
|
155
|
-
// shared onCampaignPresent channel while the first stayed on it, so one presentation was
|
|
156
|
-
// handed to both presenters, and the second call's onAbort/timeout were silently dropped.
|
|
157
|
-
// See https://trello.com/c/Gq589zwz; the full behaviour is covered in
|
|
158
|
-
// gateRegistrationLifecycle.test.ts.
|
|
159
|
-
it('replaces the previous registration when the same baseUrl is registered again', async () => {
|
|
160
|
-
mockNativeModule.onCampaignPresent.mockReturnValue({remove: jest.fn()});
|
|
161
|
-
|
|
162
|
-
await Amply.registerGate('stillframe://ad', jest.fn());
|
|
163
|
-
await Amply.registerGate('stillframe://ad', jest.fn(), {timeoutMs: 5000});
|
|
164
|
-
|
|
165
|
-
expect(mockNativeModule.unregisterGate).toHaveBeenCalledWith('stillframe://ad');
|
|
166
|
-
expect(mockNativeModule.registerGate).toHaveBeenLastCalledWith(
|
|
167
|
-
'stillframe://ad',
|
|
168
|
-
'cancel',
|
|
169
|
-
5000,
|
|
170
|
-
);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it('passes through onAbort and timeoutMs options', async () => {
|
|
174
|
-
mockNativeModule.onCampaignPresent.mockReturnValue({remove: jest.fn()});
|
|
175
|
-
|
|
176
|
-
await Amply.registerGate('stillframe://ad', jest.fn(), {
|
|
177
|
-
onAbort: 'proceed',
|
|
178
|
-
timeoutMs: 8000,
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
expect(mockNativeModule.registerGate).toHaveBeenCalledWith('stillframe://ad', 'proceed', 8000);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('delivers params + info to the presenter and forwards completed', async () => {
|
|
185
|
-
let emit: ((e: CampaignPresentEvent) => void) | undefined;
|
|
186
|
-
mockNativeModule.onCampaignPresent.mockImplementation(
|
|
187
|
-
(listener: (e: CampaignPresentEvent) => void) => {
|
|
188
|
-
emit = listener;
|
|
189
|
-
return {remove: jest.fn()};
|
|
190
|
-
},
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
const presenter = jest.fn(
|
|
194
|
-
(_params, _info, resolution: Amply.GateResolution) => resolution.completed(),
|
|
195
|
-
);
|
|
196
|
-
await Amply.registerGate('stillframe://ad', presenter);
|
|
197
|
-
|
|
198
|
-
expect(emit).toBeDefined();
|
|
199
|
-
emit!({
|
|
200
|
-
url: 'stillframe://ad?adUnit=rewarded_main',
|
|
201
|
-
params: {adUnit: 'rewarded_main'},
|
|
202
|
-
info: {campaignId: 'c1'},
|
|
203
|
-
mediationId: 'm1',
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
await flushMicrotasks();
|
|
207
|
-
|
|
208
|
-
expect(presenter).toHaveBeenCalledWith(
|
|
209
|
-
{adUnit: 'rewarded_main'},
|
|
210
|
-
{campaignId: 'c1'},
|
|
211
|
-
expect.any(Object),
|
|
212
|
-
);
|
|
213
|
-
expect(mockNativeModule.resolveCampaign).toHaveBeenCalledWith('m1', 'Completed');
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('forwards a dismissed resolution', async () => {
|
|
217
|
-
let emit: ((e: CampaignPresentEvent) => void) | undefined;
|
|
218
|
-
mockNativeModule.onCampaignPresent.mockImplementation(
|
|
219
|
-
(listener: (e: CampaignPresentEvent) => void) => {
|
|
220
|
-
emit = listener;
|
|
221
|
-
return {remove: jest.fn()};
|
|
222
|
-
},
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
await Amply.registerGate('stillframe://ad', (_p, _i, resolution) =>
|
|
226
|
-
resolution.dismissed(),
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
emit!({url: 'stillframe://ad', params: {}, info: {}, mediationId: 'm2'});
|
|
230
|
-
await flushMicrotasks();
|
|
231
|
-
|
|
232
|
-
expect(mockNativeModule.resolveCampaign).toHaveBeenCalledWith('m2', 'Dismissed');
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('reports Unavailable (fail-open) when the presenter throws', async () => {
|
|
236
|
-
let emit: ((e: CampaignPresentEvent) => void) | undefined;
|
|
237
|
-
mockNativeModule.onCampaignPresent.mockImplementation(
|
|
238
|
-
(listener: (e: CampaignPresentEvent) => void) => {
|
|
239
|
-
emit = listener;
|
|
240
|
-
return {remove: jest.fn()};
|
|
241
|
-
},
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
await Amply.registerGate('stillframe://ad', () => {
|
|
245
|
-
throw new Error('presenter blew up');
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
emit!({url: 'stillframe://ad', params: {}, info: {}, mediationId: 'm3'});
|
|
249
|
-
await flushMicrotasks();
|
|
250
|
-
|
|
251
|
-
expect(mockNativeModule.resolveCampaign).toHaveBeenCalledWith('m3', 'Unavailable');
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
it('resolves at most once even if the presenter reports twice', async () => {
|
|
255
|
-
let emit: ((e: CampaignPresentEvent) => void) | undefined;
|
|
256
|
-
mockNativeModule.onCampaignPresent.mockImplementation(
|
|
257
|
-
(listener: (e: CampaignPresentEvent) => void) => {
|
|
258
|
-
emit = listener;
|
|
259
|
-
return {remove: jest.fn()};
|
|
260
|
-
},
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
await Amply.registerGate('stillframe://ad', (_p, _i, resolution) => {
|
|
264
|
-
resolution.completed();
|
|
265
|
-
resolution.dismissed();
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
emit!({url: 'stillframe://ad', params: {}, info: {}, mediationId: 'm4'});
|
|
269
|
-
await flushMicrotasks();
|
|
270
|
-
|
|
271
|
-
expect(mockNativeModule.resolveCampaign).toHaveBeenCalledTimes(1);
|
|
272
|
-
expect(mockNativeModule.resolveCampaign).toHaveBeenCalledWith('m4', 'Completed');
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
it('ignores dispatches whose url does not match the registered baseUrl', async () => {
|
|
276
|
-
let emit: ((e: CampaignPresentEvent) => void) | undefined;
|
|
277
|
-
mockNativeModule.onCampaignPresent.mockImplementation(
|
|
278
|
-
(listener: (e: CampaignPresentEvent) => void) => {
|
|
279
|
-
emit = listener;
|
|
280
|
-
return {remove: jest.fn()};
|
|
281
|
-
},
|
|
282
|
-
);
|
|
283
|
-
|
|
284
|
-
const presenter = jest.fn();
|
|
285
|
-
await Amply.registerGate('stillframe://ad', presenter);
|
|
286
|
-
|
|
287
|
-
emit!({url: 'other://thing', params: {}, info: {}, mediationId: 'm5'});
|
|
288
|
-
await flushMicrotasks();
|
|
289
|
-
|
|
290
|
-
expect(presenter).not.toHaveBeenCalled();
|
|
291
|
-
expect(mockNativeModule.resolveCampaign).not.toHaveBeenCalled();
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
});
|
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
import * as Amply from '../index';
|
|
2
|
-
import {__setNativeModule} from '../nativeModule';
|
|
3
|
-
|
|
4
|
-
type CampaignPresentEvent = {
|
|
5
|
-
url: string;
|
|
6
|
-
params: Record<string, unknown>;
|
|
7
|
-
info: Record<string, unknown>;
|
|
8
|
-
mediationId: string;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
async function flushMicrotasks(): Promise<void> {
|
|
12
|
-
for (let i = 0; i < 5; i++) {
|
|
13
|
-
await Promise.resolve();
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The registration lifecycle of a gate: who receives a presentation, what withdrawal
|
|
19
|
-
* actually withdraws, and what happens to a registration made before the SDK is up.
|
|
20
|
-
*
|
|
21
|
-
* All three were found on 2026-07-31 — the first two by reading the bridge after a field
|
|
22
|
-
* report on Happens, the third by the field report itself. They share one cause: the JS side
|
|
23
|
-
* keeps its own record of what is registered natively, and that record was written
|
|
24
|
-
* optimistically and never reconciled.
|
|
25
|
-
*
|
|
26
|
-
* Trello: gate eviction https://trello.com/c/Gq589zwz, withdrawal
|
|
27
|
-
* https://trello.com/c/saf8I9c6, pre-init registration https://trello.com/c/TZCixnYi
|
|
28
|
-
*/
|
|
29
|
-
describe('gate registration lifecycle', () => {
|
|
30
|
-
/** Every listener currently attached to the shared onCampaignPresent channel. */
|
|
31
|
-
let emitters: Array<(e: CampaignPresentEvent) => void>;
|
|
32
|
-
|
|
33
|
-
const mockNativeModule = {
|
|
34
|
-
initialize: jest.fn(),
|
|
35
|
-
isInitialized: jest.fn(() => true),
|
|
36
|
-
track: jest.fn(),
|
|
37
|
-
trackGated: jest.fn(),
|
|
38
|
-
resolveCampaign: jest.fn(),
|
|
39
|
-
getRecentEvents: jest.fn(),
|
|
40
|
-
getDataSetSnapshot: jest.fn(),
|
|
41
|
-
registerDeepLinkListener: jest.fn(),
|
|
42
|
-
registerGate: jest.fn(),
|
|
43
|
-
unregisterGate: jest.fn(),
|
|
44
|
-
addListener: jest.fn(),
|
|
45
|
-
removeListeners: jest.fn(),
|
|
46
|
-
onSystemEvent: jest.fn(),
|
|
47
|
-
onDeepLink: jest.fn(),
|
|
48
|
-
onCampaignPresent: jest.fn(),
|
|
49
|
-
setCustomProperties: jest.fn(),
|
|
50
|
-
getCustomProperty: jest.fn(),
|
|
51
|
-
removeCustomProperty: jest.fn(),
|
|
52
|
-
clearCustomProperties: jest.fn(),
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
beforeEach(() => {
|
|
56
|
-
emitters = [];
|
|
57
|
-
mockNativeModule.isInitialized.mockReturnValue(true);
|
|
58
|
-
mockNativeModule.onCampaignPresent.mockImplementation(
|
|
59
|
-
(listener: (e: CampaignPresentEvent) => void) => {
|
|
60
|
-
emitters.push(listener);
|
|
61
|
-
// `remove` must actually detach, as a real RN subscription does — otherwise this
|
|
62
|
-
// harness reports a listener as live after it was removed, and every eviction and
|
|
63
|
-
// withdrawal assertion below would be measuring the mock rather than the code.
|
|
64
|
-
const remove = jest.fn(() => {
|
|
65
|
-
const at = emitters.indexOf(listener);
|
|
66
|
-
if (at >= 0) {
|
|
67
|
-
emitters.splice(at, 1);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
return {remove};
|
|
71
|
-
},
|
|
72
|
-
);
|
|
73
|
-
mockNativeModule.onDeepLink.mockReturnValue({remove: jest.fn()});
|
|
74
|
-
__setNativeModule(mockNativeModule as never);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
afterEach(() => {
|
|
78
|
-
Amply.removeAllListeners();
|
|
79
|
-
jest.clearAllMocks();
|
|
80
|
-
__setNativeModule(null);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
/** Delivers one presentation to every listener currently subscribed to the channel. */
|
|
84
|
-
function emitPresentation(mediationId = 'm1'): void {
|
|
85
|
-
const event: CampaignPresentEvent = {
|
|
86
|
-
url: 'stillframe://ad?adUnit=main',
|
|
87
|
-
params: {adUnit: 'main'},
|
|
88
|
-
info: {campaignId: 'c1'},
|
|
89
|
-
mediationId,
|
|
90
|
-
};
|
|
91
|
-
emitters.forEach(emit => emit(event));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
describe('a second registration on the same baseUrl (Gq589zwz)', () => {
|
|
95
|
-
it('leaves exactly one live presenter, not two', async () => {
|
|
96
|
-
const first = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
97
|
-
const second = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
98
|
-
|
|
99
|
-
await Amply.registerGate('stillframe://ad', first);
|
|
100
|
-
await Amply.registerGate('stillframe://ad', second);
|
|
101
|
-
|
|
102
|
-
emitPresentation();
|
|
103
|
-
await flushMicrotasks();
|
|
104
|
-
|
|
105
|
-
// Both presenters showing UI for one presentation is the defect: the outcome is then
|
|
106
|
-
// decided by whichever reported first, and the loser's UI is orphaned on screen.
|
|
107
|
-
expect(second).toHaveBeenCalledTimes(1);
|
|
108
|
-
expect(first).not.toHaveBeenCalled();
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('applies the options of the newest registration', async () => {
|
|
112
|
-
await Amply.registerGate('stillframe://ad', jest.fn(), {
|
|
113
|
-
onAbort: 'cancel',
|
|
114
|
-
timeoutMs: 1000,
|
|
115
|
-
});
|
|
116
|
-
await Amply.registerGate('stillframe://ad', jest.fn(), {
|
|
117
|
-
onAbort: 'proceed',
|
|
118
|
-
timeoutMs: 8000,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
// Previously the second call was a no-op natively, so its onAbort and timeout were
|
|
122
|
-
// silently discarded and the gate kept running under the first call's policy.
|
|
123
|
-
expect(mockNativeModule.registerGate).toHaveBeenLastCalledWith(
|
|
124
|
-
'stillframe://ad',
|
|
125
|
-
'proceed',
|
|
126
|
-
8000,
|
|
127
|
-
);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('keeps gates on different baseUrls independent', async () => {
|
|
131
|
-
const ad = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
132
|
-
const paywall = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
133
|
-
|
|
134
|
-
await Amply.registerGate('stillframe://ad', ad);
|
|
135
|
-
await Amply.registerGate('stillframe://paywall', paywall);
|
|
136
|
-
|
|
137
|
-
emitPresentation();
|
|
138
|
-
await flushMicrotasks();
|
|
139
|
-
|
|
140
|
-
// Only the URL-matching presenter runs; evicting by baseUrl must not evict a sibling.
|
|
141
|
-
expect(ad).toHaveBeenCalledTimes(1);
|
|
142
|
-
expect(paywall).not.toHaveBeenCalled();
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
describe('withdrawal (saf8I9c6)', () => {
|
|
147
|
-
it('withdraws the native gate, not just the JS subscription', async () => {
|
|
148
|
-
const unsubscribe = await Amply.registerGate('stillframe://ad', jest.fn());
|
|
149
|
-
|
|
150
|
-
unsubscribe();
|
|
151
|
-
|
|
152
|
-
// Without this the gate outlives its presenter and parks the next gated call on that
|
|
153
|
-
// URL for the whole fail-open timeout — silently, with no crash and no console trace.
|
|
154
|
-
expect(mockNativeModule.unregisterGate).toHaveBeenCalledWith('stillframe://ad');
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('lets the same baseUrl be registered again after withdrawal', async () => {
|
|
158
|
-
const unsubscribe = await Amply.registerGate('stillframe://ad', jest.fn());
|
|
159
|
-
unsubscribe();
|
|
160
|
-
|
|
161
|
-
const presenter = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
162
|
-
await Amply.registerGate('stillframe://ad', presenter);
|
|
163
|
-
|
|
164
|
-
emitPresentation();
|
|
165
|
-
await flushMicrotasks();
|
|
166
|
-
|
|
167
|
-
expect(presenter).toHaveBeenCalledTimes(1);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Found by codex, not by me. Withdrawal was keyed by URL alone, so an unsubscribe handed
|
|
172
|
-
* out for a registration that has since been evicted still withdrew whatever holds that
|
|
173
|
-
* URL now — killing its replacement. A stale handle must be inert, not destructive.
|
|
174
|
-
*/
|
|
175
|
-
it('an evicted registration\'s unsubscribe does not withdraw its replacement', async () => {
|
|
176
|
-
const withdrawFirst = await Amply.registerGate('stillframe://ad', jest.fn());
|
|
177
|
-
|
|
178
|
-
const second = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
179
|
-
await Amply.registerGate('stillframe://ad', second);
|
|
180
|
-
mockNativeModule.unregisterGate.mockClear();
|
|
181
|
-
|
|
182
|
-
withdrawFirst();
|
|
183
|
-
|
|
184
|
-
expect(mockNativeModule.unregisterGate).not.toHaveBeenCalled();
|
|
185
|
-
|
|
186
|
-
emitPresentation();
|
|
187
|
-
await flushMicrotasks();
|
|
188
|
-
expect(second).toHaveBeenCalledTimes(1);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it('is idempotent — a second call withdraws nothing further', async () => {
|
|
192
|
-
const unsubscribe = await Amply.registerGate('stillframe://ad', jest.fn());
|
|
193
|
-
|
|
194
|
-
unsubscribe();
|
|
195
|
-
unsubscribe();
|
|
196
|
-
|
|
197
|
-
expect(mockNativeModule.unregisterGate).toHaveBeenCalledTimes(1);
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
it('removeAllListeners withdraws every native gate too', async () => {
|
|
201
|
-
await Amply.registerGate('stillframe://ad', jest.fn());
|
|
202
|
-
await Amply.registerGate('stillframe://paywall', jest.fn());
|
|
203
|
-
|
|
204
|
-
Amply.removeAllListeners();
|
|
205
|
-
|
|
206
|
-
// It used to clear the JS bookkeeping only, leaving JS believing nothing was
|
|
207
|
-
// registered while both gates were still live natively.
|
|
208
|
-
expect(mockNativeModule.unregisterGate).toHaveBeenCalledWith('stillframe://ad');
|
|
209
|
-
expect(mockNativeModule.unregisterGate).toHaveBeenCalledWith('stillframe://paywall');
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
describe('registration before initialize (TZCixnYi)', () => {
|
|
214
|
-
it('replays a gate registration once the SDK is up', async () => {
|
|
215
|
-
mockNativeModule.isInitialized.mockReturnValue(false);
|
|
216
|
-
|
|
217
|
-
const presenter = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
218
|
-
await Amply.registerGate('stillframe://ad', presenter);
|
|
219
|
-
|
|
220
|
-
// Native refuses while uninitialised; JS must not record that as done.
|
|
221
|
-
expect(mockNativeModule.registerGate).not.toHaveBeenCalled();
|
|
222
|
-
|
|
223
|
-
mockNativeModule.isInitialized.mockReturnValue(true);
|
|
224
|
-
await Amply.initialize({appId: 'a', apiKeyPublic: 'k', apiKeySecret: 's'});
|
|
225
|
-
|
|
226
|
-
expect(mockNativeModule.registerGate).toHaveBeenCalledWith('stillframe://ad', 'cancel', 0);
|
|
227
|
-
|
|
228
|
-
emitPresentation();
|
|
229
|
-
await flushMicrotasks();
|
|
230
|
-
expect(presenter).toHaveBeenCalledTimes(1);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it('replays a deeplink listener once the SDK is up', async () => {
|
|
234
|
-
mockNativeModule.isInitialized.mockReturnValue(false);
|
|
235
|
-
|
|
236
|
-
await Amply.addDeepLinkListener(jest.fn());
|
|
237
|
-
expect(mockNativeModule.registerDeepLinkListener).not.toHaveBeenCalled();
|
|
238
|
-
|
|
239
|
-
mockNativeModule.isInitialized.mockReturnValue(true);
|
|
240
|
-
await Amply.initialize({appId: 'a', apiKeyPublic: 'k', apiKeySecret: 's'});
|
|
241
|
-
|
|
242
|
-
expect(mockNativeModule.registerDeepLinkListener).toHaveBeenCalledTimes(1);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Found by self-review, not by the original pass. Eviction ran at `registerGate()` time,
|
|
247
|
-
* but a queued registration is not in the registry yet, so there was nothing to evict —
|
|
248
|
-
* both attached at replay and the second overwrote the map entry without removing the
|
|
249
|
-
* first subscription. That is the duplicate-presenter defect again, on the pre-init path.
|
|
250
|
-
*/
|
|
251
|
-
it('two registrations on one baseUrl before init still leave one live presenter', async () => {
|
|
252
|
-
mockNativeModule.isInitialized.mockReturnValue(false);
|
|
253
|
-
|
|
254
|
-
const first = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
255
|
-
const second = jest.fn((_p, _i, r: Amply.GateResolution) => r.completed());
|
|
256
|
-
await Amply.registerGate('stillframe://ad', first);
|
|
257
|
-
await Amply.registerGate('stillframe://ad', second);
|
|
258
|
-
|
|
259
|
-
mockNativeModule.isInitialized.mockReturnValue(true);
|
|
260
|
-
await Amply.initialize({appId: 'a', apiKeyPublic: 'k', apiKeySecret: 's'});
|
|
261
|
-
|
|
262
|
-
emitPresentation();
|
|
263
|
-
await flushMicrotasks();
|
|
264
|
-
|
|
265
|
-
expect(second).toHaveBeenCalledTimes(1);
|
|
266
|
-
expect(first).not.toHaveBeenCalled();
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it('withdrawing a queued gate cancels it instead of replaying it', async () => {
|
|
270
|
-
mockNativeModule.isInitialized.mockReturnValue(false);
|
|
271
|
-
|
|
272
|
-
const unsubscribe = await Amply.registerGate('stillframe://ad', jest.fn());
|
|
273
|
-
unsubscribe();
|
|
274
|
-
|
|
275
|
-
mockNativeModule.isInitialized.mockReturnValue(true);
|
|
276
|
-
await Amply.initialize({appId: 'a', apiKeyPublic: 'k', apiKeySecret: 's'});
|
|
277
|
-
|
|
278
|
-
expect(mockNativeModule.registerGate).not.toHaveBeenCalled();
|
|
279
|
-
});
|
|
280
|
-
});
|
|
281
|
-
});
|