Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Dec 18, 2024
1 parent 902c4b3 commit 20e1a7c
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions packages/experiment-tag/test/experiment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,134 @@ describe('initializeExperiment', () => {
);
expect(mockExposure).not.toHaveBeenCalled();
});

test('preview - should set sessionStorage', () => {
const mockGlobal = {
localStorage: {
getItem: jest.fn().mockReturnValue(undefined),
setItem: jest.fn(),
},
sessionStorage: {
setItem: jest.fn(),
},
location: {
href: 'http://test.com/2',
replace: jest.fn(),
search: '?test=treatment&PREVIEW=true',
},
document: { referrer: '' },
history: { replaceState: jest.fn() },
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
mockGetGlobalScope.mockReturnValue(mockGlobal);

initializeExperiment(
'session',
JSON.stringify([
{
key: 'test',
metadata: {
deployed: true,
evaluationMode: 'local',
flagType: 'experiment',
deliveryMethod: 'web',
},
segments: [],
variants: {
control: {
key: 'control',
payload: [],
value: 'control',
},
off: {
key: 'off',
metadata: {
default: true,
},
},
treatment: {
key: 'treatment',
payload: [],
value: 'treatment',
},
},
},
]),
);

expect(mockGlobal.sessionStorage.setItem).toHaveBeenCalledWith(
'EXP_session_PREVIEW',
JSON.stringify({ test: 'treatment' }),
);
});

test('preview - sessionStorage persistence', () => {
const mockGlobal = {
localStorage: {
getItem: jest.fn().mockReturnValue(undefined),
setItem: jest.fn(),
},
sessionStorage: {
getItem: jest
.fn()
.mockReturnValue(JSON.stringify({ test: 'treatment' })),
},
location: {
href: 'http://test.com/2',
replace: jest.fn(),
},
document: { referrer: '' },
history: { replaceState: jest.fn() },
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
mockGetGlobalScope.mockReturnValue(mockGlobal);

initializeExperiment(
'session_persistence',
JSON.stringify([
{
key: 'test',
metadata: {
deployed: true,
evaluationMode: 'local',
flagType: 'experiment',
deliveryMethod: 'web',
},
segments: [
{
metadata: {
segmentName: 'All Other Users',
},
variant: 'off',
},
],
variants: {
control: {
key: 'control',
payload: [],
value: 'control',
},
off: {
key: 'off',
metadata: {
default: true,
trackExposure: false,
},
},
treatment: {
key: 'treatment',
payload: [],
value: 'treatment',
},
},
},
]),
);

expect(mockExposure).toHaveBeenCalledWith('test');
});
});

test('feature experiment on global Experiment object', () => {
Expand Down

0 comments on commit 20e1a7c

Please sign in to comment.