-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathtrack.test.ts
More file actions
90 lines (75 loc) · 2.29 KB
/
track.test.ts
File metadata and controls
90 lines (75 loc) · 2.29 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
83
84
85
86
87
88
89
90
const mockLogEvent = jest.fn();
jest.mock('@react-native-firebase/analytics', () => ({
getAnalytics: jest.fn().mockImplementation(() => ({
logEvent: mockLogEvent,
})),
isString: (a: unknown) => typeof a === 'string',
}));
jest.mock('@react-native-firebase/app', () => ({
getApp: jest.fn(),
}));
jest.mock('uuid');
import { EventType, TrackEventType } from '@segment/analytics-react-native';
import track from '../track';
describe('#track', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('forwards a track event with name only', async () => {
const event = {
type: EventType.TrackEvent,
event: 'test_event',
anonymousId: 'anon',
messageId: 'message-id',
timestamp: '00000',
properties: {},
} as TrackEventType;
await track(event);
expect(mockLogEvent).toHaveBeenCalledTimes(1);
expect(mockLogEvent).toHaveBeenCalledWith('test_event', {});
});
it('forwards a track event with name and properties', async () => {
const event = {
type: EventType.TrackEvent,
event: 'another_test_event',
anonymousId: 'anon',
messageId: 'message-id',
timestamp: '00000',
properties: { foo: 'bar' },
} as TrackEventType;
await track(event);
expect(mockLogEvent).toHaveBeenCalledTimes(1);
expect(mockLogEvent).toHaveBeenCalledWith('another_test_event', {
foo: 'bar',
});
});
it('removes non-alphanumeric characters from', async () => {
const event = {
type: EventType.TrackEvent,
event: 'yet another!!test$%^&event-CAPS',
anonymousId: 'anon',
messageId: 'message-id',
timestamp: '00000',
properties: {},
} as TrackEventType;
await track(event);
expect(mockLogEvent).toHaveBeenCalledTimes(1);
expect(mockLogEvent).toHaveBeenCalledWith(
'yet_another__test____event_CAPS',
{}
);
});
it('converts the event name to firebase event when applicable', async () => {
const event = {
type: EventType.TrackEvent,
event: 'Order Refunded',
anonymousId: 'anon',
messageId: 'message-id',
timestamp: '00000',
properties: {},
} as TrackEventType;
await track(event);
expect(mockLogEvent).toHaveBeenCalledTimes(1);
expect(mockLogEvent).toHaveBeenCalledWith('refund', {});
});
});