forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.spec.ts
104 lines (84 loc) · 3.41 KB
/
config.spec.ts
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
import {CliConfig} from './config';
import * as fs from 'fs';
import * as path from 'path';
import {CliConfig as ConfigInterface} from './spec-schema';
describe('Config', () => {
let schema = JSON.parse(fs.readFileSync(path.join(__dirname, 'spec-schema.json'), 'utf-8'));
it('works', () => {
const cliConfig = new CliConfig(null, schema, <ConfigInterface>{
requiredKey: 1,
stringKey: 'stringValue'
});
const config = cliConfig.config;
expect(config.requiredKey).toEqual(1);
expect(config.stringKey).toEqual('stringValue');
expect(config.stringKeyDefault).toEqual('defaultValue');
expect(config.booleanKey).toEqual(undefined);
expect(config.arrayKey1).toEqual(undefined);
expect(() => config.arrayKey1[0]).toThrow();
expect(config.numberKey).toEqual(undefined);
config.numberKey = 33;
expect(config.numberKey).toEqual(33);
});
describe('Get', () => {
it('works', () => {
const config = new CliConfig(null, schema, <ConfigInterface>{
requiredKey: 1,
stringKey: 'stringValue'
});
expect(config.get('requiredKey')).toEqual(1);
expect(config.get('stringKey')).toEqual('stringValue');
expect(config.get('booleanKey')).toEqual(undefined);
});
it('will never throw', () => {
const config = new CliConfig(null, schema, <ConfigInterface>{
requiredKey: 1
});
expect(config.get('arrayKey1')).toEqual(undefined);
expect(config.get('arrayKey2[0]')).toEqual(undefined);
expect(config.get('arrayKey2[0].stringKey')).toEqual(undefined);
expect(config.get('arrayKey2[0].stringKey.a.b.c.d')).toEqual(undefined);
});
});
it('handles fallback values', () => {
const cliConfig = new CliConfig(null, schema,
<ConfigInterface>{ requiredKey: 1 },
[
<ConfigInterface>{ requiredKey: 1, stringKey: 'stringValue' },
<ConfigInterface>{ requiredKey: 1, numberKey: 1 }
]
);
// Check on string.
expect(cliConfig.isDefined('stringKey')).toEqual(false);
expect(cliConfig.config.stringKey).toEqual('stringValue');
cliConfig.config.stringKey = 'stringValue2';
expect(cliConfig.isDefined('stringKey')).toEqual(true);
expect(cliConfig.config.stringKey).toEqual('stringValue2');
cliConfig.deletePath('stringKey');
expect(cliConfig.isDefined('stringKey')).toEqual(false);
expect(cliConfig.config.stringKey).toEqual('stringValue');
// Check on number (which is 2 fallbacks behind)
expect(cliConfig.isDefined('numberKey')).toEqual(false);
expect(cliConfig.config.numberKey).toEqual(1);
cliConfig.config.numberKey = 2;
expect(cliConfig.isDefined('numberKey')).toEqual(true);
expect(cliConfig.config.numberKey).toEqual(2);
cliConfig.deletePath('numberKey');
expect(cliConfig.isDefined('numberKey')).toEqual(false);
expect(cliConfig.config.numberKey).toEqual(1);
});
it('saves', () => {
const jsonObject = {
requiredKey: 1,
arrayKey2: [{ stringKey: 'value1' }, { stringKey: 'value2' }]
};
const cliConfig = new CliConfig(null, schema,
<ConfigInterface>jsonObject, [
<ConfigInterface>{ requiredKey: 1, stringKey: 'stringValue' },
<ConfigInterface>{ requiredKey: 1, numberKey: 1 }
]
);
expect(cliConfig.config.arrayKey2[0].stringKey).toEqual('value1');
expect(JSON.stringify(JSON.parse(cliConfig.serialize()))).toEqual(JSON.stringify(jsonObject));
});
});