File size: 1,867 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @ts-nocheck - TODO: Fix TypeScript issues
import * as ExPlatClient from '../index';

const mockLogError = jest.fn();
jest.mock( '../internals/log-error', () => ( {
	...jest.requireActual( '../internals/log-error' ),
	// We get a runtime type error if we don't do this:
	logError: ( ...args ) => mockLogError( ...args ),
} ) );

beforeEach( () => {
	jest.resetAllMocks();
} );

describe( 'ExPlatClient', () => {
	test( 'should loadExperimentAssignment without crashing in SSR', async () => {
		const startNow = Date.now();
		const experimentAssignment = await ExPlatClient.loadExperimentAssignment( 'experiment_a' );
		expect( experimentAssignment ).toMatchObject( {
			experimentName: 'experiment_a',
			isFallbackExperimentAssignment: true,
			ttl: 60,
			variationName: null,
		} );
		expect( experimentAssignment.retrievedTimestamp ).toBeGreaterThanOrEqual( startNow );
		expect( mockLogError.mock.calls ).toMatchInlineSnapshot( `
		Array [
		  Array [
		    Object {
		      "experimentName": "experiment_a",
		      "message": "Attempting to load ExperimentAssignment in SSR context",
		    },
		  ],
		]
	` );
	} );

	test( 'should dangerouslyGetExperimentAssignment without crashing in SSR', async () => {
		const startNow = Date.now();
		const experimentAssignment = ExPlatClient.dangerouslyGetExperimentAssignment( 'experiment_b' );
		expect( experimentAssignment ).toMatchObject( {
			experimentName: 'experiment_b',
			isFallbackExperimentAssignment: true,
			ttl: 60,
			variationName: null,
		} );
		expect( experimentAssignment.retrievedTimestamp ).toBeGreaterThanOrEqual( startNow );
		expect( mockLogError.mock.calls ).toMatchInlineSnapshot( `
		Array [
		  Array [
		    Object {
		      "experimentName": "experiment_b",
		      "message": "Attempting to dangerously get ExperimentAssignment in SSR context",
		    },
		  ],
		]
	` );
	} );
} );