-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_test.js
More file actions
67 lines (56 loc) · 1.87 KB
/
user_test.js
File metadata and controls
67 lines (56 loc) · 1.87 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
// MIT License
// Copyright (c) 2026 Open2b
// See the LICENSE file for full text.
import { assert, assertEquals } from '@std/assert'
import * as uuid from '@std/uuid/v4'
import Storage from './storage.js'
import User from './user.js'
import Options from './options.js'
const writeKey = 'rq6JJg5ENWK28NHfxSwJZmzeIvDC8GQO'
Deno.test('User', () => {
localStorage.clear()
globalThis.document = {
visibilityState: 'visible',
addEventListener: addEventListener.bind(globalThis),
}
const user = new User(new Storage(writeKey, new Options()))
assertEquals(user.id(), null)
assert(uuid.validate(user.anonymousId()))
assertEquals(user.traits(), {})
assertEquals(user.id('8g1emx962iR'), '8g1emx962iR')
assertEquals(user.id(), '8g1emx962iR')
assertEquals(user.id('e4X9L6mcA18'), 'e4X9L6mcA18')
assertEquals(user.id(null), null)
assertEquals(user.id(), null)
assertEquals(user.anonymousId('mC592p0Gn3z1Ld'), 'mC592p0Gn3z1Ld')
assertEquals(user.anonymousId(), 'mC592p0Gn3z1Ld')
assertEquals(user.anonymousId(null), null)
assert(uuid.validate(user.anonymousId()))
const rec = {}
rec.boo = rec
// Apply the following changes to traits consecutively and test the results of each step.
const changes = [
{ set: { foo: true }, expect: { foo: true } },
{ set: undefined, expect: { foo: true } },
{ set: null, expect: {} },
{ set: { foo: false }, expect: { foo: false } },
{ set: 'foo', expect: { foo: false } },
{ set: { foo: {} }, expect: { foo: {} } },
{ set: { foo: 5n, boo: true }, expect: { foo: {} } },
{ set: { foo: undefined, boo: 5 }, expect: { boo: 5 } },
{ set: { foo: rec }, expect: { boo: 5 } },
{
set: {
foo: () => {
},
boo: true,
},
expect: { boo: true },
},
]
for (let i = 0; i < changes.length; i++) {
const change = changes[i]
assertEquals(user.traits(change.set), change.expect)
assertEquals(user.traits(), change.expect)
}
})