-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathinkeep.test.ts
More file actions
75 lines (62 loc) · 2.71 KB
/
inkeep.test.ts
File metadata and controls
75 lines (62 loc) · 2.71 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
import * as scriptLoader from './utils';
import inkeepChat, { inkeepChatIdentifyUser } from './inkeep';
describe('inkeepChat', () => {
beforeEach(() => {
document.body.innerHTML = `<script></script>`;
});
it('does not load Inkeep when configuration is not present', () => {
const spy = jest.spyOn(scriptLoader, 'scriptLoader');
inkeepChat(undefined);
expect(spy).not.toHaveBeenCalled();
});
it('load Inkeep when configuration is present', () => {
const spy = jest.spyOn(scriptLoader, 'scriptLoader');
inkeepChat('apiKey', 'http://localhost:3000/api/conversations');
expect(spy).toHaveBeenCalled();
});
describe('inkeepChatIdentifyUser', () => {
let spy: jest.SpyInstance;
beforeEach(() => {
(window as any).inkeepWidget = { update: jest.fn() };
spy = jest.spyOn((window as any).inkeepWidget, 'update');
// Remove any device_id meta tags before each test
document.querySelectorAll('meta[name="device_id"]').forEach((el) => el.remove());
});
it('returns when the user is undefined and no deviceId', () => {
inkeepChatIdentifyUser({});
expect(spy).not.toHaveBeenCalled();
});
it('sets the Inkeep userId, email, and cohorts from user', () => {
inkeepChatIdentifyUser({ user: { id: '123', email: '[email protected]', companyName: 'Acme' } });
expect(spy).toHaveBeenCalledWith({
baseSettings: {
userProperties: {
id: '123',
email: '[email protected]',
cohorts: ['Company: Acme'],
},
},
});
});
it('sets the Inkeep userId and email from user without companyName', () => {
inkeepChatIdentifyUser({ user: { id: '123', email: '[email protected]' } });
expect(spy).toHaveBeenCalledWith({ baseSettings: { userProperties: { id: '123', email: '[email protected]' } } });
});
it('sets the Inkeep userId from device_id meta tag when user is not provided', () => {
const metaTag = document.createElement('meta');
metaTag.name = 'device_id';
metaTag.content = 'device123';
document.head.appendChild(metaTag);
inkeepChatIdentifyUser({});
expect(spy).toHaveBeenCalledWith({ baseSettings: { userProperties: { id: 'device123', email: undefined } } });
});
it('sets the Inkeep userId from user id, even if device_id meta tag exists', () => {
const metaTag = document.createElement('meta');
metaTag.name = 'device_id';
metaTag.content = 'device123';
document.head.appendChild(metaTag);
inkeepChatIdentifyUser({ user: { id: 'user123', email: '[email protected]' } });
expect(spy).toHaveBeenCalledWith({ baseSettings: { userProperties: { id: 'user123', email: '[email protected]' } } });
});
});
});