Skip to content

Commit 4d78447

Browse files
author
George Haberis
committed
add unit tests for EnhanceAffiliateLinks
1 parent 025484e commit 4d78447

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import '@testing-library/jest-dom';
2+
import { render } from '@testing-library/react';
3+
import { useBetaAB } from '../lib/useAB';
4+
import { EnhanceAffiliateLinks } from './EnhanceAffiliateLinks.importable';
5+
6+
// Mock the useAB module
7+
jest.mock('../lib/useAB', () => ({
8+
useBetaAB: jest.fn(),
9+
}));
10+
11+
describe('EnhanceAffiliateLinks', () => {
12+
beforeEach(() => {
13+
// Clear the DOM before each test
14+
document.body.innerHTML = '';
15+
jest.restoreAllMocks();
16+
});
17+
18+
it('should not modify links if no Skimlinks are present', () => {
19+
document.body.innerHTML = `
20+
<a href="https://example.com">Not a Skimlink</a>
21+
`;
22+
23+
render(<EnhanceAffiliateLinks />);
24+
25+
const link = document.querySelector('a');
26+
expect(link?.href).toBe('https://example.com/');
27+
});
28+
29+
it('should append xcust parameter to Skimlinks with refferer set to none if unavailable', () => {
30+
Object.defineProperty(document, 'referrer', {
31+
value: '',
32+
configurable: true,
33+
});
34+
35+
document.body.innerHTML = `
36+
<a href="https://go.skimresources.com/?id=12345">Skimlink</a>
37+
`;
38+
39+
render(<EnhanceAffiliateLinks />);
40+
41+
const link = document.querySelector('a');
42+
expect(link?.href).toContain(
43+
'xcust=referrer%7Cnone%7CaccountId%7C12345',
44+
);
45+
});
46+
47+
it('should append xcust parameter to Skimlinks with refferer set if available', () => {
48+
Object.defineProperty(document, 'referrer', {
49+
value: 'https://foo.com',
50+
configurable: true,
51+
});
52+
53+
document.body.innerHTML = `
54+
<a href="https://go.skimresources.com/?id=12345">Skimlink</a>
55+
`;
56+
57+
render(<EnhanceAffiliateLinks />);
58+
59+
const link = document.querySelector('a');
60+
expect(link?.href).toContain(
61+
'xcust=referrer%7Cfoo.com%7CaccountId%7C12345',
62+
);
63+
});
64+
65+
it('should include AB test participations in xcust if present', () => {
66+
document.body.innerHTML = `
67+
<a href="https://go.skimresources.com/?id=12345">Skimlink</a>
68+
`;
69+
70+
(useBetaAB as jest.Mock).mockReturnValue({
71+
getParticipations: () => ({
72+
test1: 'variantA',
73+
test2: 'variantB',
74+
}),
75+
});
76+
77+
render(<EnhanceAffiliateLinks />);
78+
79+
const link = document.querySelector('a');
80+
expect(link?.href).toContain(
81+
'xcust=referrer%7Cfoo.com%7CaccountId%7C12345%7CabTestParticipations%7Ctest1%3AvariantA%2Ctest2%3AvariantB',
82+
);
83+
});
84+
});

0 commit comments

Comments
 (0)