123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import expect = require('expect.js');
- import {
- IDataConnector, ISettingRegistry, SettingRegistry, Settings, StateDB
- } from '@jupyterlab/coreutils';
- import {
- JSONObject
- } from '@phosphor/coreutils';
- export
- class TestConnector extends StateDB implements IDataConnector<ISettingRegistry.IPlugin, JSONObject> {
- constructor(public schemas: { [key: string]: ISettingRegistry.ISchema } = { }) {
- super({ namespace: 'setting-registry-tests' });
- }
- fetch(id: string): Promise<ISettingRegistry.IPlugin | null> {
- return super.fetch(id).then(user => {
- if (!user && !this.schemas[id]) {
- return null;
- }
- user = user || { };
- const schema = this.schemas[id] || { type: 'object' };
- const result = { data: { composite: { }, user }, id, schema };
- return result;
- });
- }
- }
- describe('@jupyterlab/coreutils', () => {
- describe('SettingRegistry', () => {
- const connector = new TestConnector();
- let registry: SettingRegistry;
- afterEach(() => {
- connector.schemas = { };
- return connector.clear();
- });
- beforeEach(() => { registry = new SettingRegistry({ connector }); });
- describe('#constructor()', () => {
- it('should create a new setting registry', () => {
- expect(registry).to.be.a(SettingRegistry);
- });
- });
- describe('#pluginChanged', () => {
- it('should emit when a plugin changes', done => {
- const id = 'foo';
- const key = 'bar';
- const value = 'baz';
- connector.schemas[id] = { type: 'object' };
- registry.pluginChanged.connect((sender: any, plugin: string) => {
- expect(id).to.be(plugin);
- done();
- });
- registry.load(id).then(() => registry.set(id, key, value)).catch(done);
- });
- });
- describe('#plugins', () => {
- it('should return a list of registered plugins in registry', done => {
- const one = 'foo';
- const two = 'bar';
- expect(registry.plugins).to.be.empty();
- connector.schemas[one] = { type: 'object' };
- connector.schemas[two] = { type: 'object' };
- registry.load(one)
- .then(() => { expect(registry.plugins).to.have.length(1); })
- .then(() => registry.load(two))
- .then(() => { expect(registry.plugins).to.have.length(2); })
- .then(done)
- .catch(done);
- });
- });
- describe('#get()', () => {
- it('should get a setting item from a loaded plugin', done => {
- const id = 'foo';
- const key = 'bar';
- const value = 'baz';
- connector.schemas[id] = { type: 'object' };
- connector.save(id, { [key]: value })
- .then(() => registry.load(id))
- .then(() => registry.get(id, key))
- .then(saved => { expect(saved.user).to.be(value); })
- .then(done)
- .catch(done);
- });
- it('should get a setting item from a plugin that is not loaded', done => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- connector.schemas[id] = { type: 'object' };
- connector.save(id, { [key]: value })
- .then(() => registry.get(id, key))
- .then(saved => { expect(saved.composite).to.be(value); })
- .then(done)
- .catch(done);
- });
- it('should use schema default if user data not available', done => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema = connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: { type: typeof value, default: value }
- }
- };
- registry.get(id, key)
- .then(saved => {
- expect(saved.composite).to.be(schema.properties[key].default);
- expect(saved.composite).to.not.be(saved.user);
- }).then(done)
- .catch(done);
- });
- it('should let user value override schema default', done => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema = connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: { type: typeof value, default: 'delta' }
- }
- };
- connector.save(id, { [key]: value })
- .then(() => registry.get(id, key))
- .then(saved => {
- expect(saved.composite).to.be(value);
- expect(saved.user).to.be(value);
- expect(saved.composite).to.not.be(schema.properties[key].default);
- expect(saved.user).to.not.be(schema.properties[key].default);
- }).then(done)
- .catch(done);
- });
- it('should reject if a plugin does not exist', done => {
- registry.get('foo', 'bar')
- .then(saved => { done('should not resolve'); })
- .catch(reason => { done(); });
- });
- it('should resolve `undefined` if a key does not exist', done => {
- const id = 'foo';
- const key = 'bar';
- connector.schemas[id] = { type: 'object' };
- registry.get(id, key)
- .then(saved => {
- expect(saved.composite).to.be(void 0);
- expect(saved.user).to.be(void 0);
- }).then(done)
- .catch(done);
- });
- });
- describe('#load()', () => {
- it(`should resolve a registered plugin's settings`, done => {
- const id = 'foo';
- expect(registry.plugins).to.be.empty();
- connector.schemas[id] = { type: 'object' };
- registry.load(id)
- .then(settings => { expect(settings.plugin).to.be(id); })
- .then(done)
- .catch(done);
- });
- it('should reject if a plugin does not exist', done => {
- registry.load('foo')
- .then(settings => { done('should not resolve'); })
- .catch(reason => { done(); });
- });
- });
- describe('#reload()', () => {
- it(`should load a registered plugin's settings`, done => {
- const id = 'foo';
- expect(registry.plugins).to.be.empty();
- connector.schemas[id] = { type: 'object' };
- registry.reload(id)
- .then(settings => { expect(settings.plugin).to.be(id); })
- .then(done)
- .catch(done);
- });
- it(`should replace a registered plugin's settings`, done => {
- const id = 'foo';
- const first = 'Foo';
- const second = 'Bar';
- expect(registry.plugins).to.be.empty();
- connector.schemas[id] = { type: 'object', title: first};
- registry.reload(id)
- .then(settings => { expect(settings.schema.title).to.be(first); })
- .then(() => { connector.schemas[id].title = second; })
- .then(() => registry.reload(id))
- .then(settings => { expect(settings.schema.title).to.be(second); })
- .then(done)
- .catch(done);
- });
- it('should reject if a plugin does not exist', done => {
- registry.reload('foo')
- .then(settings => { done('should not resolve'); })
- .catch(reason => { done(); });
- });
- });
- });
- describe('Settings', () => {
- const connector = new TestConnector();
- let registry: SettingRegistry;
- let settings: Settings;
- afterEach(() => {
- if (settings) {
- settings.dispose();
- settings = null;
- }
- connector.schemas = { };
- return connector.clear();
- });
- beforeEach(() => { registry = new SettingRegistry({ connector }); });
- describe('#constructor()', () => {
- it('should create a new settings object for a plugin', () => {
- const id = 'alpha';
- const data = { composite: { }, user: { } };
- const schema = { type: 'object' };
- const plugin = { id, data, schema };
- settings = new Settings({ plugin, registry });
- expect(settings).to.be.a(Settings);
- });
- });
- describe('#changed', () => {
- it('should emit when a plugin changes', done => {
- const id = 'alpha';
- const schema = { type: 'object' };
- connector.schemas[id] = schema;
- registry.load(id).then(s => { settings = s as Settings; })
- .then(() => {
- settings.changed.connect(() => { done(); });
- return settings.set('foo', 'bar');
- }).catch(done);
- });
- });
- describe('#composite', () => {
- it('should contain the merged user and default data', done => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema = connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: { type: typeof value, default: value }
- }
- };
- connector.schemas[id] = schema;
- registry.load(id).then(s => { settings = s as Settings; })
- .then(() => { expect(settings.composite[key]).to.equal(value); })
- .then(done).catch(done);
- });
- it('should privilege user data', done => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema = connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: { type: typeof value, default: 'delta' }
- }
- };
- connector.schemas[id] = schema;
- registry.load(id).then(s => { settings = s as Settings; })
- .then(() => settings.set(key, value))
- .then(() => { expect(settings.composite[key]).to.equal(value); })
- .then(done).catch(done);
- });
- });
- describe('#isDisposed', () => {
- it('should test whether the settings object is disposed', () => {
- const id = 'alpha';
- const data = { composite: { }, user: { } };
- const schema = { type: 'object' };
- const plugin = { id, data, schema };
- settings = new Settings({ plugin, registry });
- expect(settings.isDisposed).to.be(false);
- settings.dispose();
- expect(settings.isDisposed).to.be(true);
- });
- });
- describe('#schema', () => {
- it('should expose the plugin schema', () => {
- const id = 'alpha';
- const data = { composite: { }, user: { } };
- const schema = { type: 'object' };
- const plugin = { id, data, schema };
- settings = new Settings({ plugin, registry });
- expect(settings.schema).to.eql(schema);
- });
- });
- describe('#user', () => {
- it('should privilege user data', done => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema = connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: { type: typeof value, default: 'delta' }
- }
- };
- connector.schemas[id] = schema;
- registry.load(id).then(s => { settings = s as Settings; })
- .then(() => settings.set(key, value))
- .then(() => { expect(settings.user[key]).to.equal(value); })
- .then(done).catch(done);
- });
- });
- });
- });
|