-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathdebug.test.ts
More file actions
82 lines (67 loc) · 2.06 KB
/
debug.test.ts
File metadata and controls
82 lines (67 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type { Message } from '@imtbl/audience-core';
import { DebugLogger } from './debug';
import { LOG_PREFIX } from './config';
describe('DebugLogger', () => {
let logSpy: jest.SpyInstance;
let warnSpy: jest.SpyInstance;
beforeEach(() => {
logSpy = jest.spyOn(console, 'log').mockImplementation();
warnSpy = jest.spyOn(console, 'warn').mockImplementation();
});
afterEach(() => {
logSpy.mockRestore();
warnSpy.mockRestore();
});
const stubMessage: Message = {
type: 'track',
messageId: 'msg-1',
eventTimestamp: '2026-01-01T00:00:00Z',
anonymousId: 'anon-1',
surface: 'web',
context: { library: 'test', libraryVersion: '0.0.0' },
eventName: 'click',
properties: {},
};
it('should not log when disabled', () => {
const logger = new DebugLogger(false);
logger.logEvent('track', stubMessage);
logger.logFlush(true, 1);
logger.logConsent('none', 'full');
logger.logWarning('test');
expect(logSpy).not.toHaveBeenCalled();
expect(warnSpy).not.toHaveBeenCalled();
});
it('should log events when enabled', () => {
const logger = new DebugLogger(true);
logger.logEvent('track', stubMessage);
expect(logSpy).toHaveBeenCalledWith(
`${LOG_PREFIX} track`,
stubMessage,
);
});
it('should log flush status', () => {
const logger = new DebugLogger(true);
logger.logFlush(true, 5);
expect(logSpy).toHaveBeenCalledWith(
`${LOG_PREFIX} flush ok (5 messages)`,
);
logger.logFlush(false, 3);
expect(logSpy).toHaveBeenCalledWith(
`${LOG_PREFIX} flush failed (3 messages)`,
);
});
it('should log consent transitions', () => {
const logger = new DebugLogger(true);
logger.logConsent('none', 'full');
expect(logSpy).toHaveBeenCalledWith(
`${LOG_PREFIX} consent none \u2192 full`,
);
});
it('should log warnings', () => {
const logger = new DebugLogger(true);
logger.logWarning('something went wrong');
expect(warnSpy).toHaveBeenCalledWith(
`${LOG_PREFIX} something went wrong`,
);
});
});