|
| 1 | +import { NativeModules } from 'react-native'; |
| 2 | + |
| 3 | +import { InternalLog } from '../../InternalLog'; |
| 4 | +import { SdkVerbosity } from '../../SdkVerbosity'; |
| 5 | +import { BufferSingleton } from '../../sdk/DatadogProvider/Buffer/BufferSingleton'; |
| 6 | +import { DdTrace } from '../DdTrace'; |
| 7 | + |
| 8 | +jest.mock('../../utils/time-provider/DefaultTimeProvider', () => { |
| 9 | + return { |
| 10 | + DefaultTimeProvider: jest.fn().mockImplementation(() => { |
| 11 | + return { now: jest.fn().mockReturnValue(456) }; |
| 12 | + }) |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +jest.mock('../../InternalLog', () => { |
| 17 | + return { |
| 18 | + InternalLog: { |
| 19 | + log: jest.fn() |
| 20 | + }, |
| 21 | + DATADOG_MESSAGE_PREFIX: 'DATADOG:' |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +describe('DdTrace', () => { |
| 26 | + beforeEach(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + BufferSingleton.onInitialization(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('Context validation', () => { |
| 32 | + describe('DdTrace.startSpan', () => { |
| 33 | + test('uses given context when context is valid', async () => { |
| 34 | + const context = { |
| 35 | + testA: 123, |
| 36 | + testB: 'ok' |
| 37 | + }; |
| 38 | + await DdTrace.startSpan('operation', context); |
| 39 | + |
| 40 | + expect(NativeModules.DdTrace.startSpan).toHaveBeenCalledWith( |
| 41 | + 'operation', |
| 42 | + context, |
| 43 | + expect.anything() |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + test('uses empty context with error when context is invalid or null', async () => { |
| 48 | + const context: any = 123; |
| 49 | + await DdTrace.startSpan('operation', context); |
| 50 | + |
| 51 | + expect(InternalLog.log).toHaveBeenNthCalledWith( |
| 52 | + 1, |
| 53 | + expect.anything(), |
| 54 | + SdkVerbosity.ERROR |
| 55 | + ); |
| 56 | + |
| 57 | + expect(NativeModules.DdTrace.startSpan).toHaveBeenCalledWith( |
| 58 | + 'operation', |
| 59 | + {}, |
| 60 | + expect.anything() |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + test('nests given context in new object when context is array', async () => { |
| 65 | + const context: any = [123, '456']; |
| 66 | + await DdTrace.startSpan('operation', context); |
| 67 | + |
| 68 | + expect(InternalLog.log).toHaveBeenNthCalledWith( |
| 69 | + 1, |
| 70 | + expect.anything(), |
| 71 | + SdkVerbosity.WARN |
| 72 | + ); |
| 73 | + |
| 74 | + expect(NativeModules.DdTrace.startSpan).toHaveBeenCalledWith( |
| 75 | + 'operation', |
| 76 | + { context }, |
| 77 | + expect.anything() |
| 78 | + ); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('DdTrace.finishSpan', () => { |
| 83 | + test('uses given context when context is valid', async () => { |
| 84 | + const context = { |
| 85 | + testA: 123, |
| 86 | + testB: 'ok' |
| 87 | + }; |
| 88 | + |
| 89 | + const spanId = await DdTrace.startSpan('operation', {}); |
| 90 | + await DdTrace.finishSpan(spanId, context); |
| 91 | + |
| 92 | + expect(NativeModules.DdTrace.finishSpan).toHaveBeenCalledWith( |
| 93 | + spanId, |
| 94 | + context, |
| 95 | + expect.anything() |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + test('uses empty context with error when context is invalid or null', async () => { |
| 100 | + const context: any = 123; |
| 101 | + await DdTrace.startSpan('operation', context); |
| 102 | + |
| 103 | + const spanId = await DdTrace.startSpan('operation', {}); |
| 104 | + await DdTrace.finishSpan(spanId, context); |
| 105 | + |
| 106 | + expect(InternalLog.log).toHaveBeenNthCalledWith( |
| 107 | + 1, |
| 108 | + expect.anything(), |
| 109 | + SdkVerbosity.ERROR |
| 110 | + ); |
| 111 | + |
| 112 | + expect(NativeModules.DdTrace.finishSpan).toHaveBeenCalledWith( |
| 113 | + spanId, |
| 114 | + {}, |
| 115 | + expect.anything() |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + test('nests given context in new object when context is array', async () => { |
| 120 | + const context: any = [123, '456']; |
| 121 | + |
| 122 | + const spanId = await DdTrace.startSpan('operation', {}); |
| 123 | + await DdTrace.finishSpan(spanId, context); |
| 124 | + |
| 125 | + expect(InternalLog.log).toHaveBeenNthCalledWith( |
| 126 | + 3, |
| 127 | + expect.anything(), |
| 128 | + SdkVerbosity.WARN |
| 129 | + ); |
| 130 | + |
| 131 | + expect(NativeModules.DdTrace.finishSpan).toHaveBeenCalledWith( |
| 132 | + spanId, |
| 133 | + { context }, |
| 134 | + expect.anything() |
| 135 | + ); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments