File size: 3,677 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
127
128
129
// Copied from https://github.com/Automattic/wp-calypso/blob/6755acc862c0387b7061e13f99fd22c5dd22e458/packages/create-calypso-config/src/test/index.js
// This makes sure our implementation is compatible with the original one.
import createConfig from '../create-odyssey-config';

describe( 'index', () => {
	describe( 'config without data', () => {
		const config = createConfig( {} );

		test( 'has to return false when the feature flags are not specified', () => {
			const result = config.isEnabled( 'flagA' );

			expect( result ).toBe( false );
		} );
	} );

	describe( 'config with data', () => {
		const config = createConfig( {
			keyA: 'value',
			features: {
				flagA: false,
				flagB: false,
				flagC: true,
			},
		} );

		test( 'has to return value of the provided key', () => {
			const result = config( 'keyA' );

			expect( result ).toEqual( 'value' );
		} );

		test( 'has to return false when the provided feature flag is disabled', () => {
			const result = config.isEnabled( 'flagA' );

			expect( result ).toBe( false );
		} );

		test( 'has to return false when the provided feature flag is enabled', () => {
			const result = config.isEnabled( 'flagC' );

			expect( result ).toBe( true );
		} );

		describe( 'error cases', () => {
			const NODE_ENV = process.env.NODE_ENV;
			const fakeKey = 'where did all the errors go?';

			afterEach( () => ( process.env.NODE_ENV = NODE_ENV ) );

			test( "should throw an error when given key doesn't exist (NODE_ENV == development)", () => {
				process.env.NODE_ENV = 'development';

				expect( () => config( fakeKey ) ).toThrow( ReferenceError );
			} );

			test( "should not throw an error when given key doesn't exist (NODE_ENV != development)", () => {
				const envs = [ 'client', 'desktop', 'horizon', 'production', 'stage', 'test', 'wpcalypso' ];

				envs.forEach( ( env ) => {
					process.env.NODE_ENV = env;

					expect( process.env.NODE_ENV ).toEqual( env );
					expect( () => config( fakeKey ) ).not.toThrow( Error );
				} );
			} );
		} );
	} );

	describe( 'config utilities', () => {
		let config;

		beforeEach( () => {
			config = createConfig( {
				features: {
					flagA: false,
					flagB: false,
					flagC: true,
				},
			} );
		} );

		afterEach( () => {
			config = null;
		} );

		describe( 'isEnabled', () => {
			test( 'it correctly reports status of features', () => {
				expect( config.isEnabled( 'flagA' ) ).toBe( false );
				expect( config.isEnabled( 'flagC' ) ).toBe( true );
			} );

			test( 'it defaults to "false" when feature is not defined', () => {
				expect( config.isEnabled( 'flagXYZ' ) ).toBe( false );
			} );
		} );

		describe( 'enable', () => {
			test( 'it can enable features which are not yet set', () => {
				config.enable( 'flagD' );
				config.enable( 'flagE' );
				expect( config.isEnabled( 'flagD' ) ).toBe( true );
				expect( config.isEnabled( 'flagE' ) ).toBe( true );
			} );

			test( 'it can toggle existing features to enable them', () => {
				config.enable( 'flagA' );
				expect( config.isEnabled( 'flagA' ) ).toBe( true );
			} );
		} );

		describe( 'disable', () => {
			test( 'it can toggle existing features to disable them', () => {
				config.disable( 'flagC' );
				expect( config.isEnabled( 'flagC' ) ).toBe( false );
			} );

			test( 'it retains existing disable setting for features that are already disabled', () => {
				config.disable( 'flagA' );
				expect( config.isEnabled( 'flagA' ) ).toBe( false );
			} );

			test( 'it will handle setting new features to a initial disabled state', () => {
				config.disable( 'flagZXY' );
				expect( config.isEnabled( 'flagZXY' ) ).toBe( false );
			} );
		} );
	} );
} );