|
| 1 | +var test = require('ava') |
| 2 | +var AssertionError = require('assert').AssertionError |
| 3 | +var validate = require('../../src/loosely-validate-event') |
| 4 | + |
| 5 | +test('requires "anonymousId" to be a string or number', t => { |
| 6 | + const event = { |
| 7 | + type: 'track', |
| 8 | + anonymousId: { foo: 'bar' } |
| 9 | + } |
| 10 | + t.throws(() => { |
| 11 | + validate(event) |
| 12 | + }, '"anonymousId" must be a string or number.') |
| 13 | +}) |
| 14 | + |
| 15 | +test('requires "category" to be a string', t => { |
| 16 | + const event = { |
| 17 | + type: 'track', |
| 18 | + category: true |
| 19 | + } |
| 20 | + t.throws(() => { |
| 21 | + validate(event) |
| 22 | + }, '"category" must be a string.') |
| 23 | +}) |
| 24 | + |
| 25 | +test('requires "integrations" to be an object', t => { |
| 26 | + const event = { |
| 27 | + type: 'track', |
| 28 | + integrations: true |
| 29 | + } |
| 30 | + t.throws(() => { |
| 31 | + validate(event) |
| 32 | + }, '"integrations" must be an object.') |
| 33 | +}) |
| 34 | + |
| 35 | +test('requires an event type', t => { |
| 36 | + t.throws(() => { |
| 37 | + validate({}) |
| 38 | + }, AssertionError) |
| 39 | + |
| 40 | + t.throws(() => { |
| 41 | + validate({ type: '' }, null) |
| 42 | + }, AssertionError) |
| 43 | +}) |
| 44 | + |
| 45 | +test('requires a valid event type', t => { |
| 46 | + t.throws(() => { |
| 47 | + validate({ type: 'banana' }) |
| 48 | + }, 'Invalid event type: "banana"') |
| 49 | +}) |
| 50 | + |
| 51 | +test('requires anonymousId or userId on track events', t => { |
| 52 | + t.throws(() => { |
| 53 | + validate({ |
| 54 | + type: 'track', |
| 55 | + event: 'Did Something' |
| 56 | + }) |
| 57 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 58 | + |
| 59 | + t.throws(() => { |
| 60 | + validate({ |
| 61 | + type: 'track', |
| 62 | + event: 'Did Something', |
| 63 | + fooId: 'banana' |
| 64 | + }) |
| 65 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 66 | + |
| 67 | + t.notThrows(() => { |
| 68 | + validate({ |
| 69 | + event: 'Did Something', |
| 70 | + anonymousId: 'banana' |
| 71 | + }, 'track') |
| 72 | + }) |
| 73 | + |
| 74 | + t.notThrows(() => { |
| 75 | + validate({ |
| 76 | + type: 'track', |
| 77 | + event: 'Did Something', |
| 78 | + userId: 'banana' |
| 79 | + }) |
| 80 | + }) |
| 81 | +}) |
| 82 | + |
| 83 | +test('requires event on track events', t => { |
| 84 | + t.throws(() => { |
| 85 | + validate({ |
| 86 | + type: 'track', |
| 87 | + userId: 'banana' |
| 88 | + }) |
| 89 | + }, 'You must pass an "event".') |
| 90 | + |
| 91 | + t.notThrows(() => { |
| 92 | + validate({ |
| 93 | + type: 'track', |
| 94 | + event: 'Did Something', |
| 95 | + userId: 'banana' |
| 96 | + }) |
| 97 | + }) |
| 98 | +}) |
| 99 | + |
| 100 | +test('requires anonymousId or userId on group events', t => { |
| 101 | + t.throws(() => { |
| 102 | + validate({ |
| 103 | + type: 'group', |
| 104 | + groupId: 'foo' |
| 105 | + }) |
| 106 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 107 | + |
| 108 | + t.throws(() => { |
| 109 | + validate({ |
| 110 | + type: 'group', |
| 111 | + groupId: 'foo', |
| 112 | + fooId: 'banana' |
| 113 | + }) |
| 114 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 115 | + |
| 116 | + t.notThrows(() => { |
| 117 | + validate({ |
| 118 | + type: 'group', |
| 119 | + groupId: 'foo', |
| 120 | + anonymousId: 'banana' |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + t.notThrows(() => { |
| 125 | + validate({ |
| 126 | + type: 'group', |
| 127 | + groupId: 'foo', |
| 128 | + userId: 'banana' |
| 129 | + }) |
| 130 | + }) |
| 131 | +}) |
| 132 | + |
| 133 | +test('requires groupId on group events', t => { |
| 134 | + t.throws(() => { |
| 135 | + validate({ |
| 136 | + type: 'group', |
| 137 | + userId: 'banana' |
| 138 | + }) |
| 139 | + }, 'You must pass a "groupId".') |
| 140 | + |
| 141 | + t.notThrows(() => { |
| 142 | + validate({ |
| 143 | + type: 'group', |
| 144 | + groupId: 'foo', |
| 145 | + userId: 'banana' |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
| 149 | + |
| 150 | +test('requires anonymousId or userId on identify events', t => { |
| 151 | + t.throws(() => { |
| 152 | + validate({ |
| 153 | + type: 'identify' |
| 154 | + }) |
| 155 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 156 | + |
| 157 | + t.throws(() => { |
| 158 | + validate({ |
| 159 | + type: 'identify', |
| 160 | + fooId: 'banana' |
| 161 | + }) |
| 162 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 163 | + |
| 164 | + t.notThrows(() => { |
| 165 | + validate({ |
| 166 | + type: 'identify', |
| 167 | + anonymousId: 'banana' |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + t.notThrows(() => { |
| 172 | + validate({ |
| 173 | + type: 'identify', |
| 174 | + userId: 'banana' |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
| 178 | + |
| 179 | +test('requires anonymousId or userId on page events', t => { |
| 180 | + t.throws(() => { |
| 181 | + validate({ |
| 182 | + type: 'page' |
| 183 | + }) |
| 184 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 185 | + |
| 186 | + t.throws(() => { |
| 187 | + validate({ |
| 188 | + type: 'page', |
| 189 | + fooId: 'banana' |
| 190 | + }) |
| 191 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 192 | + |
| 193 | + t.notThrows(() => { |
| 194 | + validate({ |
| 195 | + type: 'page', |
| 196 | + anonymousId: 'banana' |
| 197 | + }) |
| 198 | + }) |
| 199 | + |
| 200 | + t.notThrows(() => { |
| 201 | + validate({ |
| 202 | + type: 'page', |
| 203 | + userId: 'banana' |
| 204 | + }) |
| 205 | + }) |
| 206 | +}) |
| 207 | + |
| 208 | +test('requires anonymousId or userId on screen events', t => { |
| 209 | + t.throws(() => { |
| 210 | + validate({ |
| 211 | + type: 'screen' |
| 212 | + }) |
| 213 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 214 | + |
| 215 | + t.throws(() => { |
| 216 | + validate({ |
| 217 | + type: 'screen', |
| 218 | + fooId: 'banana' |
| 219 | + }) |
| 220 | + }, 'You must pass either an "anonymousId" or a "userId".') |
| 221 | + |
| 222 | + t.notThrows(() => { |
| 223 | + validate({ |
| 224 | + type: 'screen', |
| 225 | + anonymousId: 'banana' |
| 226 | + }) |
| 227 | + }) |
| 228 | + |
| 229 | + t.notThrows(() => { |
| 230 | + validate({ |
| 231 | + type: 'screen', |
| 232 | + userId: 'banana' |
| 233 | + }) |
| 234 | + }) |
| 235 | +}) |
| 236 | + |
| 237 | +test('requires userId on alias events', t => { |
| 238 | + t.throws(() => { |
| 239 | + validate({ |
| 240 | + type: 'alias' |
| 241 | + }) |
| 242 | + }, 'You must pass a "userId".') |
| 243 | + |
| 244 | + t.throws(() => { |
| 245 | + validate({ |
| 246 | + type: 'alias', |
| 247 | + fooId: 'banana' |
| 248 | + }) |
| 249 | + }, 'You must pass a "userId".') |
| 250 | + |
| 251 | + t.notThrows(() => { |
| 252 | + validate({ |
| 253 | + type: 'alias', |
| 254 | + userId: 'banana', |
| 255 | + previousId: 'apple' |
| 256 | + }) |
| 257 | + }) |
| 258 | +}) |
| 259 | + |
| 260 | +test('requires events to be < 32kb', t => { |
| 261 | + t.throws(() => { |
| 262 | + var event = { |
| 263 | + type: 'track', |
| 264 | + event: 'Did Something', |
| 265 | + userId: 'banana', |
| 266 | + properties: {} |
| 267 | + } |
| 268 | + for (var i = 0; i < 10000; i++) { |
| 269 | + event.properties[i] = 'a' |
| 270 | + } |
| 271 | + validate(event) |
| 272 | + }, 'Your message must be < 32kb.') |
| 273 | + |
| 274 | + t.notThrows(() => { |
| 275 | + validate({ |
| 276 | + type: 'track', |
| 277 | + event: 'Did Something', |
| 278 | + userId: 'banana' |
| 279 | + }) |
| 280 | + }) |
| 281 | +}) |
0 commit comments