File size: 4,152 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { validExperimentAssignment, validFallbackExperimentAssignment } from '../test-common';
import * as Validations from '../validations';

describe( 'isObject', () => {
	it( 'returns true for an object', () => {
		expect( Validations.isObject( {} ) ).toBe( true );
		expect( Validations.isObject( { key: 'value' } ) ).toBe( true );
		expect( Validations.isObject( new Error( 'foo' ) ) ).toBe( true );
	} );
	it( 'returns false for a non object', () => {
		expect( Validations.isObject( undefined ) ).toBe( false );
		expect( Validations.isObject( null ) ).toBe( false );
		expect( Validations.isObject( 1 ) ).toBe( false );
		expect( Validations.isObject( 'object' ) ).toBe( false );
	} );
} );

describe( 'isName', () => {
	it( 'returns true for a valid name', () => {
		expect( Validations.isName( 'a' ) ).toBe( true );
		expect( Validations.isName( '1' ) ).toBe( true );
		expect( Validations.isName( 'a1' ) ).toBe( true );
		expect( Validations.isName( 'experiment' ) ).toBe( true );
		expect( Validations.isName( 'experiment_name' ) ).toBe( true );
		expect( Validations.isName( 'experiment_name_v1' ) ).toBe( true );
	} );
	it( 'returns false for an invalid name', () => {
		expect( Validations.isName( '' ) ).toBe( false );
		expect( Validations.isName( 123 ) ).toBe( false );
		expect( Validations.isName( {} ) ).toBe( false );
		expect( Validations.isName( undefined ) ).toBe( false );
		expect( Validations.isName( null ) ).toBe( false );
		expect( Validations.isName( '!@#$%^&*(IO)_asdfkjn.;```' ) ).toBe( false );
		expect( Validations.isName( 'asdfkjhkjh09182lk_asdf[-123=@#$%^' ) ).toBe( false );
		expect( Validations.isName( 'experiment_V1' ) ).toBe( false );
	} );
} );

describe( 'isExperimentAssignment', () => {
	it( 'returns true for a valid ExperimentAssignment', () => {
		expect( Validations.isExperimentAssignment( validExperimentAssignment ) ).toBe( true );
		expect( Validations.isExperimentAssignment( validFallbackExperimentAssignment ) ).toBe( true );
	} );
	it( 'returns false for an invalid ExperimentAssignment', () => {
		expect(
			Validations.isExperimentAssignment( {
				...validExperimentAssignment,
				experimentName: undefined,
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validFallbackExperimentAssignment,
				experimentName: null,
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validFallbackExperimentAssignment,
				experimentName: 1,
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validExperimentAssignment,
				variationName: undefined,
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validFallbackExperimentAssignment,
				variationName: 0,
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validExperimentAssignment,
				retrievedTimestamp: undefined,
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validFallbackExperimentAssignment,
				retrievedTimestamp: 'string',
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validExperimentAssignment,
				ttl: undefined,
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validFallbackExperimentAssignment,
				ttl: 'string',
			} )
		).toBe( false );
		expect(
			Validations.isExperimentAssignment( {
				...validFallbackExperimentAssignment,
				ttl: 0,
			} )
		).toBe( false );
	} );
} );

describe( 'validateExperimentAssignment', () => {
	it( 'returns a valid ExperimentAssignment', () => {
		expect( Validations.validateExperimentAssignment( validExperimentAssignment ) ).toEqual(
			validExperimentAssignment
		);
		expect( Validations.validateExperimentAssignment( validFallbackExperimentAssignment ) ).toEqual(
			validFallbackExperimentAssignment
		);
	} );
	it( 'throws if there is an invalid ExperimentAssignment', () => {
		expect( () => {
			Validations.validateExperimentAssignment( {
				...validFallbackExperimentAssignment,
				experimentName: null,
			} );
		} ).toThrowErrorMatchingInlineSnapshot( '"Invalid ExperimentAssignment"' );
	} );
} );