forked from jupyterlab/jupyterlab
/
config.spec.ts
199 lines (178 loc) · 6.35 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { UUID } from '@phosphor/coreutils';
import { JSONObject } from '@phosphor/coreutils';
import { ConfigSection, ConfigWithDefaults } from '@jupyterlab/services';
import { expectFailure } from '@jupyterlab/testutils';
import { handleRequest, makeSettings, getRequestHandler } from '../utils';
/**
* Generate a random config section name.
*
* #### Notes
* Config sections cannot have dashes (see
* https://github.com/jupyter/notebook/blob/b2edf8963cc017733f264cca35fd6584f328c8b6/notebook/services/config/handlers.py#L36),
* so we remove the dashes.
*/
function randomName() {
return UUID.uuid4().replace(/-/g, '');
}
describe('config', () => {
describe('ConfigSection.create()', () => {
it('should load a config', async () => {
const config = await ConfigSection.create({ name: randomName() });
expect(Object.keys(config.data).length).to.equal(0);
});
it('should accept server settings', async () => {
const serverSettings = makeSettings();
const config = await ConfigSection.create({
name: randomName(),
serverSettings
});
expect(Object.keys(config.data).length).to.equal(0);
});
it('should fail for an incorrect response', async () => {
const serverSettings = getRequestHandler(201, {});
const configPromise = ConfigSection.create({
name: randomName(),
serverSettings
});
await expectFailure(configPromise, 'Invalid response: 201 Created');
});
});
describe('#update()', () => {
it('should update a config', async () => {
const config = await ConfigSection.create({ name: randomName() });
const data: any = await config.update({ foo: 'baz', spam: 'eggs' });
expect(data.foo).to.equal('baz');
expect(config.data['foo']).to.equal('baz');
expect(data['spam']).to.equal('eggs');
expect(config.data['spam']).to.equal('eggs');
});
it('should accept server settings', async () => {
const serverSettings = makeSettings();
const config = await ConfigSection.create({
name: randomName(),
serverSettings
});
const data: any = await config.update({ foo: 'baz', spam: 'eggs' });
expect(data.foo).to.equal('baz');
expect(config.data['foo']).to.equal('baz');
expect(data['spam']).to.equal('eggs');
expect(config.data['spam']).to.equal('eggs');
});
it('should fail for an incorrect response', async () => {
const config = await ConfigSection.create({ name: randomName() });
handleRequest(config, 201, {});
const update = config.update({ foo: 'baz' });
await expectFailure(update, 'Invalid response: 201 Created');
});
});
});
describe('jupyter.services - ConfigWithDefaults', () => {
describe('#constructor()', () => {
it('should complete properly', async () => {
const defaults: JSONObject = { spam: 'eggs' };
const className = 'testclass';
const section = await ConfigSection.create({
name: randomName()
});
const config = new ConfigWithDefaults({
section,
defaults,
className
});
expect(config).to.be.an.instanceof(ConfigWithDefaults);
});
});
describe('#get()', () => {
it('should get a new config value', async () => {
const defaults: JSONObject = { foo: 'bar' };
const className = 'testclass';
const section = await ConfigSection.create({
name: randomName()
});
const config = new ConfigWithDefaults({
section,
defaults,
className
});
const data = config.get('foo');
expect(data).to.equal('bar');
});
it('should get a default config value', async () => {
const defaults: JSONObject = { spam: 'eggs' };
const className = 'testclass';
const section = await ConfigSection.create({
name: randomName()
});
const config = new ConfigWithDefaults({
section,
defaults,
className
});
const data = config.get('spam');
expect(data).to.equal('eggs');
});
it('should get a default config value with no class', async () => {
const defaults: JSONObject = { spam: 'eggs' };
const className = 'testclass';
const section = await ConfigSection.create({
name: randomName()
});
const config = new ConfigWithDefaults({
section,
defaults,
className
});
const data = config.get('spam');
expect(data).to.equal('eggs');
});
it('should get a falsey value', async () => {
const defaults: JSONObject = { foo: true };
const className = 'testclass';
const serverSettings = getRequestHandler(200, { foo: false });
const section = await ConfigSection.create({
name: randomName(),
serverSettings
});
const config = new ConfigWithDefaults({
section,
defaults,
className
});
const data = config.get('foo');
expect(data).to.not.be.ok;
});
});
describe('#set()', () => {
it('should set a value in a class immediately', async () => {
const className = 'testclass';
const section = await ConfigSection.create({ name: randomName() });
const config = new ConfigWithDefaults({ section, className });
let data: any = await config.set('foo', 'bar');
data = section.data['testclass'] as JSONObject;
expect(data['foo']).to.equal('bar');
});
it('should set a top level value', async () => {
const section = await ConfigSection.create({ name: randomName() });
const config = new ConfigWithDefaults({ section });
const set = config.set('foo', 'bar');
expect(section.data['foo']).to.equal('bar');
await set;
expect(section.data['foo']).to.equal('bar');
});
it('should fail for an invalid response', async () => {
const serverSettings = getRequestHandler(200, {});
const section = await ConfigSection.create({
name: randomName(),
serverSettings
});
handleRequest(section, 201, { foo: 'bar' });
const config = new ConfigWithDefaults({ section });
const set = config.set('foo', 'bar');
expect(section.data['foo']).to.equal('bar');
await expectFailure(set, 'Invalid response: 201 Created');
});
});
});