Skip to content

Commit fb07fc5

Browse files
committed
fix tests
1 parent 166359b commit fb07fc5

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

src/cookie.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import { setCookie, getCookie, generateId, getAnalyticsCookie } from './cookie.j
22

33
describe('Cookie Utilities', () => {
44
beforeEach(() => {
5-
document.cookie = ''; // Clear all cookies
5+
// Explicitly clear all cookies by expiring them
6+
document.cookie.split(';').forEach(cookie => {
7+
const [name] = cookie.split('=');
8+
document.cookie = `${name.trim()}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
9+
});
10+
// Extra safety: ensure specific cookies are cleared
11+
document.cookie = 'ajs_test=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
12+
document.cookie = 'rl_test=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
13+
document.cookie = 'fallback=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
614
});
715

816
test('setCookie and getCookie work together', () => {

src/http.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ describe('HTTP Utility', () => {
55
global.fetch = jest.fn(() => Promise.resolve({ ok: true }));
66
});
77

8-
test('sendEvent makes a POST request', async () => {
8+
test('sendEvent makes a POST request with the correcy endpoint', async () => {
99
const result = await sendEvent('http://example.com', 'key', { type: 'test' });
1010
expect(fetch).toHaveBeenCalledWith(
11-
'http://example.com',
11+
'http://example.com/test',
1212
expect.objectContaining({
1313
method: 'POST',
14-
headers: expect.any(Object),
14+
headers: expect.objectContaining({
15+
'Authorization': expect.stringMatching(/^Basic /),
16+
'Content-Type': 'application/json'
17+
}),
1518
body: JSON.stringify({ type: 'test' })
1619
})
1720
);

src/index.test.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,60 @@ describe('TinyTag', () => {
88
document.cookie = '';
99
global.fetch = jest.fn(() => Promise.resolve({ ok: true }));
1010
tt = new TinyTag();
11+
jest.spyOn(tt.queue, 'push'); // Spy on queue.push to check events
1112
tt.init({ writeKey: 'testkey', dataPlaneUrl: 'http://example.com' });
1213
});
1314

15+
afterEach(() => {
16+
jest.restoreAllMocks();
17+
});
18+
1419
test('page queues an event with context', () => {
1520
tt.page('Home');
16-
expect(tt.queue.queue[0]).toMatchObject({
17-
type: 'page',
18-
name: 'Home',
19-
context: expect.objectContaining({
20-
userAgent: navigator.userAgent
21+
expect(tt.queue.push).toHaveBeenCalledWith(
22+
expect.objectContaining({
23+
type: 'page',
24+
name: 'Home',
25+
context: expect.objectContaining({
26+
userAgent: navigator.userAgent
27+
})
2128
})
22-
});
29+
);
2330
});
2431

2532
test('track queues an event', () => {
2633
tt.track('Button Clicked', { button: 'test' });
27-
expect(tt.queue.queue[0]).toMatchObject({
28-
type: 'track',
29-
event: 'Button Clicked',
30-
properties: { button: 'test' }
31-
});
34+
expect(tt.queue.push).toHaveBeenCalledWith(
35+
expect.objectContaining({
36+
type: 'track',
37+
event: 'Button Clicked',
38+
properties: { button: 'test' }
39+
})
40+
);
3241
});
3342

3443
test('identify sets userId and merges traits', () => {
3544
setCookie('rl_trait', JSON.stringify({ email: '[email protected]' }));
3645
tt.identify('user123', { name: 'New User' });
37-
expect(tt.queue.queue[0]).toMatchObject({
38-
type: 'identify',
39-
userId: 'user123',
40-
traits: { email: '[email protected]', name: 'New User' }
41-
});
46+
expect(tt.queue.push).toHaveBeenCalledWith(
47+
expect.objectContaining({
48+
type: 'identify',
49+
userId: 'user123',
50+
traits: { email: '[email protected]', name: 'New User' }
51+
})
52+
);
4253
expect(document.cookie).toContain('userId=user123');
4354
});
4455

4556
test('group queues an event', () => {
4657
tt.group('group456', { name: 'Test Group' });
47-
expect(tt.queue.queue[0]).toMatchObject({
48-
type: 'group',
49-
groupId: 'group456',
50-
traits: { name: 'Test Group' }
51-
});
58+
expect(tt.queue.push).toHaveBeenCalledWith(
59+
expect.objectContaining({
60+
type: 'group',
61+
groupId: 'group456',
62+
traits: { name: 'Test Group' }
63+
})
64+
);
5265
});
5366

5467
test('getAnonymousId uses Segment cookie if present', () => {

0 commit comments

Comments
 (0)