Skip to content

Commit bf46fda

Browse files
Merge pull request #470 from DataDog/louiszawadzki/stop-session-support
Kiosk apps support
2 parents 1e13495 + 6cd05f0 commit bf46fda

File tree

10 files changed

+72
-28
lines changed

10 files changed

+72
-28
lines changed

packages/core/__mocks__/react-native.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ actualRN.NativeModules.DdRum = {
101101
) as jest.MockedFunction<DdRumType['addError']>,
102102
addTiming: jest.fn().mockImplementation(
103103
() => new Promise<void>(resolve => resolve())
104-
) as jest.MockedFunction<DdRumType['addTiming']>
104+
) as jest.MockedFunction<DdRumType['addTiming']>,
105+
stopSession: jest.fn().mockImplementation(
106+
() => new Promise<void>(resolve => resolve())
107+
) as jest.MockedFunction<DdRumType['stopSession']>
105108
};
106109

107110
module.exports = actualRN;

packages/core/android/src/main/kotlin/com/datadog/reactnative/DdRum.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ class DdRum(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(
255255
promise.resolve(null)
256256
}
257257

258+
/**
259+
* Stops the current RUM Session.
260+
*/
261+
@ReactMethod
262+
fun stopSession(promise: Promise) {
263+
GlobalRum.get().stopSession()
264+
promise.resolve(null)
265+
}
266+
258267
// region Internal
259268

260269
private fun String.asRumActionType(): RumActionType {

packages/core/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,14 @@ internal class DdRumTest {
410410
// Then
411411
verify(mockRumMonitor).addTiming(timing)
412412
}
413+
414+
@Test
415+
fun `M call stopSession W stopSession()`() {
416+
417+
// When
418+
testedDdRum.stopSession(mockPromise)
419+
420+
// Then
421+
verify(mockRumMonitor).stopSession()
422+
}
413423
}

packages/core/ios/Sources/DdRum.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,7 @@ @interface RCT_EXTERN_MODULE(DdRum, NSObject)
7171
withResolver:(RCTPromiseResolveBlock)resolve
7272
withRejecter:(RCTPromiseRejectBlock)reject)
7373

74+
RCT_EXTERN_METHOD(stopSession:(RCTPromiseResolveBlock)resolve
75+
withRejecter:(RCTPromiseRejectBlock)reject)
76+
7477
@end

packages/core/ios/Sources/DdRum.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal protocol NativeRUM {
1818
func stopUserAction(type: RUMUserActionType, name: String?, attributes: [String: Encodable])
1919
func addUserAction(type: RUMUserActionType, name: String, attributes: [String: Encodable])
2020
func addTiming(name: String)
21+
func stopSession()
2122
func addResourceMetrics(resourceKey: String,
2223
fetch: (start: Date, end: Date),
2324
redirection: (start: Date, end: Date)?,
@@ -187,6 +188,12 @@ class RNDdRum: NSObject {
187188
resolve(nil)
188189
}
189190

191+
@objc(stopSession:withRejecter:)
192+
func stopSession(resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
193+
nativeRUM.stopSession()
194+
resolve(nil)
195+
}
196+
190197
// MARK: - Private methods
191198

192199
private func attributes(from context: NSDictionary, with timestampMs: Int64) -> [String: Encodable] {

packages/core/ios/Tests/DdRumTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ internal class DdRumTests: XCTestCase {
249249
XCTAssertEqual(mockNativeRUM.receivedAttributes.count, 0)
250250
}
251251

252+
func testStopSession() throws {
253+
rum.stopSession(resolve: mockResolve, reject: mockReject)
254+
255+
XCTAssertEqual(mockNativeRUM.calledMethods.count, 1)
256+
XCTAssertEqual(mockNativeRUM.calledMethods.last, .stopSession())
257+
XCTAssertEqual(mockNativeRUM.receivedAttributes.count, 0)
258+
}
259+
252260
func testRumErrorSourceMapping() throws {
253261
XCTAssertEqual(RUMErrorSource(from: "source"), RUMErrorSource.source)
254262
XCTAssertEqual(RUMErrorSource(from: "network"), RUMErrorSource.network)
@@ -278,6 +286,7 @@ private class MockNativeRUM: NativeRUM {
278286
case stopUserAction(type: RUMUserActionType, name: String?)
279287
case addUserAction(type: RUMUserActionType, name: String)
280288
case addTiming(name: String)
289+
case stopSession(_: Int? = nil) // We need an attribute for the case to be Equatable
281290
case addResourceMetrics(resourceKey: String,
282291
fetch: Interval,
283292
redirection: Interval,
@@ -331,6 +340,9 @@ private class MockNativeRUM: NativeRUM {
331340
func addTiming(name: String) {
332341
calledMethods.append(.addTiming(name: name))
333342
}
343+
func stopSession() {
344+
calledMethods.append(.stopSession())
345+
}
334346
func addResourceMetrics(
335347
resourceKey: String,
336348
fetch: (start: Date, end: Date),

packages/core/src/nativeModulesTypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ export type DdNativeRumType = {
322322
* @param name: The name of the new custom timing attribute. Timings can be nested up to 8 levels deep. Names using more than 8 levels will be sanitized by SDK.
323323
*/
324324
addTiming(name: string): Promise<void>;
325+
326+
/**
327+
* Stops the current RUM Session.
328+
*/
329+
stopSession(): Promise<void>;
325330
};
326331

327332
type ActionType = 'TAP' | 'SCROLL' | 'SWIPE' | 'BACK' | 'CUSTOM';

packages/core/src/rum/DdRum.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ class DdRumWrapper implements DdRumType {
326326
return bufferVoidNativeCall(() => this.nativeRum.addTiming(name));
327327
}
328328

329+
stopSession(): Promise<void> {
330+
InternalLog.log('Stopping RUM Session', SdkVerbosity.DEBUG);
331+
return bufferVoidNativeCall(() => this.nativeRum.stopSession());
332+
}
333+
329334
registerErrorEventMapper(errorEventMapper: ErrorEventMapper) {
330335
this.errorEventMapper = generateErrorEventMapper(errorEventMapper);
331336
}

packages/core/src/rum/__tests__/DdRum.test.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ jest.mock('../../TimeProvider', () => {
2424
});
2525

2626
describe('DdRum', () => {
27-
describe('DdRum.stopAction', () => {
28-
beforeEach(() => {
29-
jest.clearAllMocks();
30-
BufferSingleton.onInitialization();
31-
});
27+
beforeEach(() => {
28+
jest.clearAllMocks();
29+
BufferSingleton.onInitialization();
30+
});
3231

32+
describe('DdRum.stopAction', () => {
3333
test('calls the native SDK when called with new API', async () => {
3434
await DdRum.stopAction('scroll', 'page', { user: 'me' }, 123);
3535
expect(NativeModules.DdRum.stopAction).toHaveBeenCalledWith(
@@ -151,11 +151,6 @@ describe('DdRum', () => {
151151
});
152152

153153
describe('DdRum.addError', () => {
154-
beforeEach(() => {
155-
jest.clearAllMocks();
156-
DdRum.unregisterErrorEventMapper();
157-
});
158-
159154
it('registers event mapper and maps error', async () => {
160155
const errorEventMapper: ErrorEventMapper = error => {
161156
error.message = 'New message';
@@ -196,12 +191,6 @@ describe('DdRum', () => {
196191
});
197192

198193
describe('DdRum.stopResource', () => {
199-
beforeEach(() => {
200-
jest.clearAllMocks();
201-
DdRum.unregisterResourceEventMapper();
202-
BufferSingleton.onInitialization();
203-
});
204-
205194
it('registers event mapper and maps resource', async () => {
206195
const resourceEventMapper: ResourceEventMapper = resource => {
207196
resource.context = { retryAttempts: 3 };
@@ -270,11 +259,6 @@ describe('DdRum', () => {
270259
});
271260

272261
describe('DdRum.addAction', () => {
273-
beforeEach(() => {
274-
jest.clearAllMocks();
275-
DdRum.unregisterActionEventMapper();
276-
});
277-
278262
it('registers event mapper and maps action', async () => {
279263
const actionEventMapper: ActionEventMapper = action => {
280264
action.context = { frustration: true };
@@ -318,12 +302,6 @@ describe('DdRum', () => {
318302
});
319303

320304
describe('DdRum.stopAction', () => {
321-
beforeEach(() => {
322-
jest.clearAllMocks();
323-
DdRum.unregisterActionEventMapper();
324-
BufferSingleton.onInitialization();
325-
});
326-
327305
it('registers event mapper and maps action', async () => {
328306
const actionEventMapper: ActionEventMapper = action => {
329307
action.context = { frustration: true };
@@ -384,6 +362,13 @@ describe('DdRum', () => {
384362
});
385363
});
386364

365+
describe('DdRum.stopSession', () => {
366+
it('calls the native API', async () => {
367+
await DdRum.stopSession();
368+
expect(NativeModules.DdRum.stopSession).toHaveBeenCalledWith();
369+
});
370+
});
371+
387372
describe('PropagatorTypes', () => {
388373
it('matches with the native name of propagators', () => {
389374
/**

packages/core/src/rum/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ export type DdRumType = {
142142
* @param name: The name of the new custom timing attribute. Timings can be nested up to 8 levels deep. Names using more than 8 levels will be sanitized by SDK.
143143
*/
144144
addTiming(name: string): Promise<void>;
145+
146+
/**
147+
* Stops the current RUM Session.
148+
*/
149+
stopSession(): Promise<void>;
145150
};
146151

147152
/**

0 commit comments

Comments
 (0)