This repository was archived by the owner on Apr 8, 2021. It is now read-only.
forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.ts
More file actions
121 lines (102 loc) · 3.77 KB
/
Copy pathpayload.ts
File metadata and controls
121 lines (102 loc) · 3.77 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* JavaScript tracker for Snowplow: tests/payload.ts
*
* Copyright (c) 2014-2020 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
import test from 'ava';
import { isJson, isNonEmptyJson, payloadBuilder } from '../../src/payload';
const sampleJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0',
data: [
{
schema: 'iglu:com.example_company/page/jsonschema/1-2-1',
data: {
pageType: 'test',
lastUpdated: new Date(Date.UTC(2014, 1, 26)),
},
},
{
schema: 'iglu:com.example_company/user/jsonschema/2-0-0',
data: {
userType: 'tester',
},
},
],
};
const expectedPayloads = [
{
co:
'{"schema":"iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0","data":[{"schema":"iglu:com.example_company/page/jsonschema/1-2-1","data":{"pageType":"test","lastUpdated":"2014-02-26T00:00:00.000Z"}},{"schema":"iglu:com.example_company/user/jsonschema/2-0-0","data":{"userType":"tester"}}]}',
},
{
cx:
'eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3sic2NoZW1hIjoiaWdsdTpjb20uZXhhbXBsZV9jb21wYW55L3BhZ2UvanNvbnNjaGVtYS8xLTItMSIsImRhdGEiOnsicGFnZVR5cGUiOiJ0ZXN0IiwibGFzdFVwZGF0ZWQiOiIyMDE0LTAyLTI2VDAwOjAwOjAwLjAwMFoifX0seyJzY2hlbWEiOiJpZ2x1OmNvbS5leGFtcGxlX2NvbXBhbnkvdXNlci9qc29uc2NoZW1hLzItMC0wIiwiZGF0YSI6eyJ1c2VyVHlwZSI6InRlc3RlciJ9fV19',
},
];
test('JSON Payload identifies as JSON', t => {
const json = {
name: 'john',
properties: {
age: 30,
languages: ['English', 'French'],
},
};
t.is(isJson(json), true);
});
test('non-JSON Payload doesnt identify as JSON', t => {
const nonJson = function() {
return {};
};
t.is(isJson(nonJson), false);
});
test('Empty Payload identifies as an empty JSON', t => {
const emptyJson = {};
t.is(isNonEmptyJson(emptyJson), false);
});
test('Build a payload', t => {
const sb = payloadBuilder(false);
sb.add('e', 'pv');
sb.add('tv', 'js-2.0.0');
t.deepEqual(sb.build(), { e: 'pv', tv: 'js-2.0.0' });
});
test('Do not add undefined values to a payload', t => {
const sb = payloadBuilder(false);
sb.add('e', undefined);
t.deepEqual(sb.build(), {}, 'Undefined values should not be added to the payload');
});
test('Do not add null values to a payload', t => {
const sb = payloadBuilder(false);
sb.add('e', undefined);
t.deepEqual(sb.build(), {}, 'Null values should not be added to the payload');
});
test('Add a dictionary of name-value pairs to the payload', t => {
const sb = payloadBuilder(false);
sb.addDict({
e: 'pv',
tv: 'js-2.0.0',
});
t.deepEqual(
sb.build(),
{ e: 'pv', tv: 'js-2.0.0' },
'A dictionary of name-value pairs should be added to the payload'
);
});
test('Add a JSON to the payload', t => {
const sb = payloadBuilder(false);
sb.addJson('cx', 'co', sampleJson);
t.deepEqual(sb.build(), expectedPayloads[0], 'JSON should be added correctly');
});
test('Add a base 64 encoded JSON to the payload', t => {
const sb = payloadBuilder(true);
sb.addJson('cx', 'co', sampleJson);
t.deepEqual(sb.build(), expectedPayloads[1], 'JSON should be encoded correctly');
});