Skip to content

Commit a169a8f

Browse files
committed
Add unit tests
1 parent d1703f7 commit a169a8f

File tree

5 files changed

+244
-38
lines changed

5 files changed

+244
-38
lines changed

src/api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ export interface AtomicProperties {
111111
/** For page view events. The page URL. */
112112
url?: string;
113113
/** For page view events. The page title. */
114-
title?: string | null;
114+
title?: string;
115115
/** For page view events. The referrer URL. */
116116
referrer?: string;
117117
/** For structured events. Name for the group of objects you want to track. */
118-
category: string;
118+
category?: string;
119119
/** For structured events. Defines the type of user interaction for the web object. */
120-
action: string;
120+
action?: string;
121121
/** For structured events. Identifies the specific object being actioned. */
122122
label?: string;
123123
/** For structured events. Describes the object or the action performed on it. */

src/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function trackWebViewEvent(
9090
atomicProperties: AtomicProperties,
9191
event?: SelfDescribingEvent | null,
9292
entities?: Array<SelfDescribingJson> | null,
93-
trackers?: Array<string>
93+
trackers?: Array<string> | null
9494
) {
9595
const stringifiedAtomicProperties = JSON.stringify(atomicProperties);
9696
const stringifiedEvent = serializeSelfDescribingEvent(event);
@@ -105,7 +105,7 @@ export function trackWebViewEvent(
105105
);
106106
});
107107

108-
const getMessage = () => {
108+
const getMessageIOS = () => {
109109
return {
110110
atomicProperties: stringifiedAtomicProperties,
111111
selfDescribingEventData: stringifiedEvent,
@@ -115,11 +115,23 @@ export function trackWebViewEvent(
115115
};
116116

117117
withIOSInterfaceV2((messageHandler) => {
118-
messageHandler.postMessage(getMessage());
118+
messageHandler.postMessage(getMessageIOS());
119119
});
120120

121+
const getMessageRN = () => {
122+
return {
123+
command: 'trackWebViewEvent',
124+
event: {
125+
selfDescribingEventData: event,
126+
...atomicProperties
127+
},
128+
context: entities,
129+
trackers: trackers,
130+
};
131+
};
132+
121133
withReactNativeInterface((rnInterface) => {
122-
rnInterface.postMessage(JSON.stringify(getMessage()));
134+
rnInterface.postMessage(JSON.stringify(getMessageRN()));
123135
});
124136
}
125137

test/androidV2.test.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import {
1313
trackWebViewEvent,
1414
} from '../src';
15-
import { AtomicProperties } from '../src/api';
1615

1716
describe('Android interface', () => {
1817
let windowSpy: any;
@@ -32,50 +31,63 @@ describe('Android interface', () => {
3231
windowSpy.mockRestore();
3332
});
3433

35-
it('track a webview event', () => {
36-
const atomic: AtomicProperties = {
34+
it('tracks a webview primitive event', () => {
35+
const atomic = {
3736
eventName: 'se',
3837
trackerVersion: 'webview-0.3.0',
3938
category: 'cat',
4039
action: 'act',
4140
};
42-
const stringifiedAtomicProperties = JSON.stringify(atomic);
4341

4442
trackWebViewEvent(atomic, null, null, ['ns1', 'ns2']);
4543

4644
expect(trackWebViewStub).toHaveBeenCalledWith(
47-
stringifiedAtomicProperties,
45+
JSON.stringify(atomic),
4846
null,
4947
null,
5048
['ns1', 'ns2']
5149
);
5250
});
5351

54-
// it('adds context entities', () => {
55-
// trackStructEvent(
56-
// {
57-
// category: 'cat',
58-
// action: 'act',
59-
// context: [
60-
// {
61-
// schema: 'schema',
62-
// data: {
63-
// abc: 1,
64-
// },
65-
// },
66-
// ],
67-
// },
68-
// undefined
69-
// );
52+
it('tracks a webview self-describing event', () => {
53+
const atomic = {
54+
eventName: 'ue',
55+
trackerVersion: 'webview-0.3.0',
56+
};
57+
const event = {
58+
event: {
59+
schema: 'schema',
60+
data: {
61+
abc: 1,
62+
},
63+
},
64+
}
65+
66+
trackWebViewEvent(atomic, event, null, null);
67+
68+
expect(trackWebViewStub).toHaveBeenCalledWith(
69+
JSON.stringify(atomic),
70+
JSON.stringify(event),
71+
null,
72+
null
73+
);
74+
});
7075

71-
// expect(trackStructEventStub).toHaveBeenCalledWith(
72-
// 'cat',
73-
// 'act',
74-
// null,
75-
// null,
76-
// null,
77-
// '[{"schema":"schema","data":{"abc":1}}]',
78-
// null
79-
// );
80-
// });
76+
it('adds context entities', () => {
77+
const entity = {
78+
schema: 'iglu:schema',
79+
data: {
80+
abc: 1,
81+
},
82+
};
83+
84+
trackWebViewEvent({}, null, [entity], null);
85+
86+
expect(trackWebViewStub).toHaveBeenCalledWith(
87+
'{}',
88+
null,
89+
JSON.stringify([entity]),
90+
null
91+
);
92+
});
8193
});

test/iosV2.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2022 Snowplow Analytics Ltd. All rights reserved.
2+
//
3+
// This program is licensed to you under the Apache License Version 2.0,
4+
// and you may not use this file except in compliance with the Apache License Version 2.0.
5+
// You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
6+
//
7+
// Unless required by applicable law or agreed to in writing,
8+
// software distributed under the Apache License Version 2.0 is distributed on an
9+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
11+
12+
import {
13+
trackWebViewEvent,
14+
} from '../src';
15+
16+
describe('iOS interface', () => {
17+
let windowSpy: any;
18+
let messageHandler = jest.fn();
19+
20+
beforeEach(() => {
21+
windowSpy = jest.spyOn(window, 'window', 'get');
22+
windowSpy.mockImplementation(() => ({
23+
location: { href: 'http://test.com' },
24+
webkit: {
25+
messageHandlers: {
26+
snowplowV2: {
27+
postMessage: messageHandler,
28+
},
29+
},
30+
},
31+
}));
32+
});
33+
34+
afterEach(() => {
35+
windowSpy.mockRestore();
36+
});
37+
38+
it('tracks a webview primitive event', () => {
39+
const atomic = {
40+
eventName: 'pv',
41+
trackerVersion: 'webview',
42+
url: 'http://test.com',
43+
title: 'test title',
44+
};
45+
46+
trackWebViewEvent(atomic, null, null, ['ns1', 'ns2']);
47+
48+
expect(messageHandler).toHaveBeenCalledWith({
49+
atomicProperties: JSON.stringify(atomic),
50+
selfDescribingEventData: null,
51+
entities: null,
52+
trackers: ['ns1', 'ns2']
53+
});
54+
});
55+
56+
it('tracks a webview self-describing event', () => {
57+
const atomic = {
58+
eventName: 'ue',
59+
trackerVersion: 'webview-0.3.0',
60+
};
61+
const event = {
62+
event: {
63+
schema: 'schema',
64+
data: {
65+
abc: 1,
66+
},
67+
},
68+
}
69+
70+
trackWebViewEvent(atomic, event, null, null);
71+
72+
expect(messageHandler).toHaveBeenCalledWith({
73+
atomicProperties: JSON.stringify(atomic),
74+
selfDescribingEventData: JSON.stringify(event),
75+
entities: null,
76+
trackers: null
77+
});
78+
});
79+
80+
it('adds context entities', () => {
81+
const entity = {
82+
schema: 'iglu:schema',
83+
data: {
84+
abc: 1,
85+
},
86+
};
87+
88+
trackWebViewEvent({}, null, [entity], null);
89+
90+
expect(messageHandler).toHaveBeenCalledWith({
91+
atomicProperties: '{}',
92+
selfDescribingEventData: null,
93+
entities: JSON.stringify([entity]),
94+
trackers: null
95+
});
96+
});
97+
});

test/reactNative.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
trackScreenView,
1515
trackSelfDescribingEvent,
1616
trackStructEvent,
17+
trackWebViewEvent,
1718
} from '../src';
1819

1920
describe('React Native interface', () => {
@@ -34,6 +35,90 @@ describe('React Native interface', () => {
3435
windowSpy.mockRestore();
3536
});
3637

38+
it('track a webview primitive event', () => {
39+
const atomic = {
40+
eventName: 'pp',
41+
trackerVersion: 'webview',
42+
url: 'http://test.com',
43+
minXOffset: 20,
44+
maxXOffset: 30,
45+
minYOffset: 40,
46+
maxYOffset: 50,
47+
};
48+
49+
trackWebViewEvent(atomic, null, null, ['ns1', 'ns2']);
50+
51+
expect(messageHandler).toHaveBeenCalledWith(
52+
JSON.stringify({
53+
command: 'trackWebViewEvent',
54+
event: {
55+
selfDescribingEventData: null,
56+
eventName: 'pp',
57+
trackerVersion: 'webview',
58+
url: 'http://test.com',
59+
minXOffset: 20,
60+
maxXOffset: 30,
61+
minYOffset: 40,
62+
maxYOffset: 50,
63+
},
64+
context: null,
65+
trackers: ['ns1', 'ns2'],
66+
})
67+
);
68+
});
69+
70+
it('tracks a webview self-describing event', () => {
71+
const atomic = {
72+
eventName: 'ue',
73+
trackerVersion: 'webview',
74+
};
75+
const event = {
76+
event: {
77+
schema: 'schema',
78+
data: {
79+
abc: 1,
80+
},
81+
},
82+
}
83+
84+
trackWebViewEvent(atomic, event, null, null);
85+
86+
expect(messageHandler).toHaveBeenCalledWith(
87+
JSON.stringify({
88+
command: 'trackWebViewEvent',
89+
event: {
90+
selfDescribingEventData: event,
91+
eventName: 'ue',
92+
trackerVersion: 'webview',
93+
},
94+
context: null,
95+
trackers: null,
96+
})
97+
);
98+
});
99+
100+
it('tracks a webview event with entities', () => {
101+
const entity = {
102+
schema: 'iglu:schema',
103+
data: {
104+
abc: 1,
105+
},
106+
};
107+
108+
trackWebViewEvent({}, null, [entity], null);
109+
110+
expect(messageHandler).toHaveBeenCalledWith(
111+
JSON.stringify({
112+
command: 'trackWebViewEvent',
113+
event: {
114+
selfDescribingEventData: null,
115+
},
116+
context: [entity],
117+
trackers: null,
118+
})
119+
);
120+
});
121+
37122
it('track a structured view', () => {
38123
trackStructEvent({ category: 'cat', action: 'act' }, ['ns1', 'ns2']);
39124
expect(messageHandler).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)