Skip to content

Commit 480b1d6

Browse files
author
Sameeksha Katiyar
committed
test: verify boundary robustness of tracking event dispatchers JhaSourav07#1550
1 parent 9b64cab commit 480b1d6

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

utils/tracking.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,20 @@ describe('trackUser', () => {
6262

6363
expect(fetchMock).toHaveBeenCalledTimes(1);
6464
});
65+
66+
it('gracefully bypasses tracking when user metric logs are empty or falsy', () => {
67+
const fetchMock = vi.fn().mockResolvedValue({});
68+
vi.stubGlobal('fetch', fetchMock);
69+
70+
const sendBeaconMock = vi.fn().mockReturnValue(true);
71+
Object.defineProperty(navigator, 'sendBeacon', {
72+
value: sendBeaconMock,
73+
configurable: true,
74+
});
75+
76+
trackUser('');
77+
78+
expect(sendBeaconMock).not.toHaveBeenCalled();
79+
expect(fetchMock).not.toHaveBeenCalled();
80+
});
6581
});

utils/tracking.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export function trackUser(username: string) {
2+
if (!username || username.trim() === '') {
3+
return;
4+
}
25
if (typeof navigator === 'undefined' || typeof window === 'undefined') return;
36

47
const payload = JSON.stringify({ username });

0 commit comments

Comments
 (0)