12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- DefaultSchemaValidator,
- ISettingRegistry,
- SettingRegistry,
- Settings
- } from '@jupyterlab/settingregistry';
- import { StateDB } from '@jupyterlab/statedb';
- import { signalToPromise } from '@jupyterlab/testutils';
- import { JSONObject } from '@lumino/coreutils';
- class TestConnector extends StateDB {
- schemas: { [key: string]: ISettingRegistry.ISchema } = {};
- async fetch(id: string): Promise<ISettingRegistry.IPlugin | undefined> {
- const fetched = await super.fetch(id);
- if (!fetched && !this.schemas[id]) {
- return undefined;
- }
- const schema: ISettingRegistry.ISchema = this.schemas[id] || {
- type: 'object'
- };
- const composite = {};
- const user = {};
- const raw = (fetched as string) || '{ }';
- const version = 'test';
- return { id, data: { composite, user }, raw, schema, version };
- }
- async list(): Promise<any> {
- return Promise.reject('list method not implemented');
- }
- }
- describe('@jupyterlab/settingregistry', () => {
- describe('DefaultSchemaValidator', () => {
- describe('#constructor()', () => {
- it('should create a new schema validator', () => {
- const validator = new DefaultSchemaValidator();
- expect(validator).toBeInstanceOf(DefaultSchemaValidator);
- });
- });
- describe('#validateData()', () => {
- it('should validate data against a schema', () => {
- const id = 'foo';
- const validator = new DefaultSchemaValidator();
- const schema: ISettingRegistry.ISchema = {
- additionalProperties: false,
- properties: {
- bar: { type: 'string' }
- },
- type: 'object'
- };
- const composite = {};
- const user = {};
- const raw = '{ "bar": "baz" }';
- const version = 'test';
- const plugin = { id, data: { composite, user }, raw, schema, version };
- const errors = validator.validateData(plugin);
- expect(errors).toBe(null);
- });
- it('should return errors if the data fails to validate', () => {
- const id = 'foo';
- const validator = new DefaultSchemaValidator();
- const schema: ISettingRegistry.ISchema = {
- additionalProperties: false,
- properties: {
- bar: { type: 'string' }
- },
- type: 'object'
- };
- const composite = {};
- const user = {};
- const raw = '{ "baz": "qux" }';
- const version = 'test';
- const plugin = { id, data: { composite, user }, raw, schema, version };
- const errors = validator.validateData(plugin);
- expect(errors).not.toBe(null);
- });
- it('should populate the composite data', () => {
- const id = 'foo';
- const validator = new DefaultSchemaValidator();
- const schema: ISettingRegistry.ISchema = {
- additionalProperties: false,
- properties: {
- bar: { type: 'string', default: 'baz' }
- },
- type: 'object'
- };
- const composite = {} as JSONObject;
- const user = {} as JSONObject;
- const raw = '{ }';
- const version = 'test';
- const plugin = { id, data: { composite, user }, raw, schema, version };
- const errors = validator.validateData(plugin);
- expect(errors).toBe(null);
- expect(plugin.data.user.bar).toBeUndefined();
- expect(plugin.data.composite.bar).toBe(schema.properties!.bar.default);
- });
- });
- });
- describe('SettingRegistry', () => {
- const connector = new TestConnector();
- const timeout = 500;
- let registry: SettingRegistry;
- afterEach(() => {
- connector.schemas = {};
- return connector.clear();
- });
- beforeEach(() => {
- registry = new SettingRegistry({ connector, timeout });
- });
- describe('#constructor()', () => {
- it('should create a new setting registry', () => {
- expect(registry).toBeInstanceOf(SettingRegistry);
- });
- });
- describe('#pluginChanged', () => {
- it('should emit when a plugin changes', async () => {
- const id = 'foo';
- const key = 'bar';
- const value = 'baz';
- connector.schemas[id] = { type: 'object' };
- let called = false;
- registry.pluginChanged.connect((sender: any, plugin: string) => {
- expect(id).toBe(plugin);
- called = true;
- });
- await registry.load(id);
- await registry.set(id, key, value);
- expect(called).toBe(true);
- });
- });
- describe('#plugins', () => {
- it('should return a list of registered plugins in registry', async () => {
- const one = 'foo';
- const two = 'bar';
- expect(Object.keys(registry.plugins)).toHaveLength(0);
- connector.schemas[one] = { type: 'object' };
- connector.schemas[two] = { type: 'object' };
- await registry.load(one);
- expect(Object.keys(registry.plugins)).toHaveLength(1);
- await registry.load(two);
- expect(Object.keys(registry.plugins)).toHaveLength(2);
- });
- });
- describe('#get()', () => {
- it('should get a setting item from a loaded plugin', async () => {
- const id = 'foo';
- const key = 'bar';
- const value = 'baz';
- connector.schemas[id] = { type: 'object' };
- await connector.save(id, JSON.stringify({ [key]: value }));
- (await registry.load(id)) as Settings;
- const saved = await registry.get(id, key);
- expect(saved.user).toBe(value);
- });
- it('should get a setting item from a plugin that is not loaded', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- connector.schemas[id] = { type: 'object' };
- await connector.save(id, JSON.stringify({ [key]: value }));
- const saved = await registry.get(id, key);
- expect(saved.composite).toBe(value);
- });
- it('should use schema default if user data not available', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: {
- type: typeof value as ISettingRegistry.Primitive,
- default: value
- }
- }
- });
- const saved = await registry.get(id, key);
- expect(saved.composite).toBe(schema.properties![key].default);
- expect(saved.composite).not.toBe(saved.user);
- });
- it('should let user value override schema default', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: {
- type: typeof value as ISettingRegistry.Primitive,
- default: 'delta'
- }
- }
- });
- await connector.save(id, JSON.stringify({ [key]: value }));
- const saved = await registry.get(id, key);
- expect(saved.composite).toBe(value);
- expect(saved.user).toBe(value);
- expect(saved.composite).not.toBe(schema.properties![key].default);
- expect(saved.user).not.toBe(schema.properties![key].default);
- });
- it('should reject if a plugin does not exist', async () => {
- let failed = false;
- try {
- await registry.get('foo', 'bar');
- } catch (e) {
- failed = true;
- }
- expect(failed).toBe(true);
- });
- it('should resolve `undefined` if a key does not exist', async () => {
- const id = 'foo';
- const key = 'bar';
- connector.schemas[id] = { type: 'object' };
- const saved = await registry.get(id, key);
- expect(saved.composite).toBeUndefined();
- expect(saved.user).toBeUndefined();
- });
- });
- describe('#load()', () => {
- it(`should resolve a registered plugin's settings`, async () => {
- const id = 'foo';
- expect(Object.keys(registry.plugins)).toHaveLength(0);
- connector.schemas[id] = { type: 'object' };
- const settings = (await registry.load(id)) as Settings;
- expect(settings.id).toBe(id);
- });
- it(`should reject if a plugin transformation times out`, async () => {
- const id = 'foo';
- let failed = false;
- connector.schemas[id] = {
- 'jupyter.lab.transform': true,
- type: 'object'
- };
- try {
- await registry.load(id);
- } catch (e) {
- failed = true;
- }
- expect(failed).toBe(true);
- });
- it('should reject if a plugin does not exist', async () => {
- let failed = false;
- try {
- await registry.load('foo');
- } catch (e) {
- failed = true;
- }
- expect(failed).toBe(true);
- });
- });
- describe('#reload()', () => {
- it(`should load a registered plugin's settings`, async () => {
- const id = 'foo';
- expect(Object.keys(registry.plugins)).toHaveLength(0);
- connector.schemas[id] = { type: 'object' };
- const settings = await registry.reload(id);
- expect(settings.id).toBe(id);
- });
- it(`should replace a registered plugin's settings`, async () => {
- const id = 'foo';
- const first = 'Foo';
- const second = 'Bar';
- expect(Object.keys(registry.plugins)).toHaveLength(0);
- connector.schemas[id] = { type: 'object', title: first };
- let settings = await registry.reload(id);
- expect(settings.schema.title).toBe(first);
- await Promise.resolve(undefined);
- connector.schemas[id].title = second;
- settings = await registry.reload(id);
- expect(settings.schema.title).toBe(second);
- });
- it('should reject if a plugin does not exist', async () => {
- let failed = false;
- try {
- await registry.reload('foo');
- } catch (e) {
- failed = true;
- }
- expect(failed).toBe(true);
- });
- });
- describe('#transform()', () => {
- it(`should transform a plugin during the fetch phase`, async () => {
- const id = 'foo';
- const version = 'transform-test';
- expect(Object.keys(registry.plugins)).toHaveLength(0);
- connector.schemas[id] = {
- 'jupyter.lab.transform': true,
- type: 'object'
- };
- registry.transform(id, {
- fetch: plugin => {
- plugin.version = version;
- return plugin;
- }
- });
- expect((await registry.load(id)).version).toBe(version);
- });
- it(`should transform a plugin during the compose phase`, async () => {
- const id = 'foo';
- const composite = { a: 1 };
- expect(Object.keys(registry.plugins)).toHaveLength(0);
- connector.schemas[id] = {
- 'jupyter.lab.transform': true,
- type: 'object'
- };
- registry.transform(id, {
- compose: plugin => {
- plugin.data = { user: plugin.data.user, composite };
- return plugin;
- }
- });
- expect((await registry.load(id)).composite).toEqual(composite);
- });
- it(`should disallow a transform that changes the plugin ID`, async () => {
- const id = 'foo';
- let failed = false;
- expect(Object.keys(registry.plugins)).toHaveLength(0);
- connector.schemas[id] = {
- 'jupyter.lab.transform': true,
- type: 'object'
- };
- registry.transform(id, {
- compose: plugin => {
- plugin.id = 'bar';
- return plugin;
- }
- });
- try {
- await registry.load(id);
- } catch (e) {
- failed = true;
- }
- expect(failed).toBe(true);
- });
- });
- });
- describe('reconcileMenus', () => {
- it('should merge menu tree', () => {
- const a: ISettingRegistry.IMenu[] = [
- {
- id: '1',
- items: [{ command: 'a' }]
- },
- {
- id: '2',
- items: [{ command: 'b' }]
- },
- {
- id: '4',
- items: [
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [{ command: 'sub-1' }]
- }
- }
- ]
- }
- ];
- const b: ISettingRegistry.IMenu[] = [
- {
- id: '2',
- items: [
- { command: 'b', disabled: true },
- { command: 'b', args: { input: 'hello' } },
- { command: 'b', args: { input: 'world' } },
- { command: 'c' }
- ]
- },
- {
- id: '3',
- items: [{ command: 'd' }]
- },
- {
- id: '4',
- items: [
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [
- { command: 'sub-1', disabled: true },
- { command: 'sub-2' }
- ]
- }
- }
- ]
- }
- ];
- const merged = SettingRegistry.reconcileMenus(a, b);
- expect(merged).toHaveLength(4);
- expect(merged[0].id).toEqual('1');
- expect(merged[0].items).toHaveLength(1);
- expect(merged[1].id).toEqual('2');
- expect(merged[1].items).toHaveLength(4);
- expect(merged[1].items![0].command).toEqual('b');
- expect(merged[1].items![0].args).toBeUndefined();
- expect(merged[1].items![0].disabled).toEqual(true);
- expect(merged[1].items![1].command).toEqual('b');
- expect(merged[1].items![1].args?.input).toEqual('hello');
- expect(merged[1].items![2].command).toEqual('b');
- expect(merged[1].items![2].args?.input).toEqual('world');
- expect(merged[2].id).toEqual('4');
- expect(merged[2].items).toHaveLength(1);
- expect(merged[2].items![0].submenu?.items).toHaveLength(2);
- expect(merged[2].items![0].submenu?.items![0].command).toEqual('sub-1');
- expect(merged[2].items![0].submenu?.items![0].disabled).toEqual(true);
- expect(merged[3].id).toEqual('3');
- expect(merged[3].items).toHaveLength(1);
- });
- it('should merge menu tree without adding new items', () => {
- const a: ISettingRegistry.IMenu[] = [
- {
- id: '1',
- items: [{ command: 'a' }]
- },
- {
- id: '2',
- items: [{ command: 'b' }]
- },
- {
- id: '4',
- items: [
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [{ command: 'sub-1' }]
- }
- }
- ]
- }
- ];
- const b: ISettingRegistry.IMenu[] = [
- {
- id: '2',
- items: [
- { command: 'b', disabled: true },
- { command: 'b', args: { input: 'hello' } },
- { command: 'b', args: { input: 'world' } },
- { command: 'c' }
- ]
- },
- {
- id: '3',
- items: [{ command: 'd' }]
- },
- {
- id: '4',
- items: [
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [
- { command: 'sub-1', disabled: true },
- { command: 'sub-2' }
- ]
- }
- }
- ],
- disabled: true
- }
- ];
- const merged = SettingRegistry.reconcileMenus(a, b, false, false);
- expect(merged).toHaveLength(3);
- expect(merged![0].id).toEqual('1');
- expect(merged![0].items).toHaveLength(1);
- expect(merged![1].id).toEqual('2');
- expect(merged![1].items).toHaveLength(1);
- expect(merged[1].items![0].command).toEqual('b');
- expect(merged[1].items![0].args).toBeUndefined();
- expect(merged[1].items![0].disabled).toEqual(true);
- expect(merged[2].id).toEqual('4');
- expect(merged[2].items).toHaveLength(1);
- expect(merged[2].items![0].submenu?.items).toHaveLength(1);
- expect(merged[2].items![0].submenu?.items![0].command).toEqual('sub-1');
- expect(merged[2].items![0].submenu?.items![0].disabled).toEqual(true);
- expect(merged[2].disabled).toEqual(true);
- });
- });
- describe('reconcileItems', () => {
- it('should merge items list', () => {
- const a: ISettingRegistry.IContextMenuItem[] = [
- { command: 'a', selector: '.a' },
- { command: 'b', selector: '.b' },
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [{ command: 'sub-1' }]
- },
- selector: '.sub'
- }
- ];
- const b: ISettingRegistry.IContextMenuItem[] = [
- { command: 'b', selector: '.b', disabled: true },
- { command: 'b', selector: '.bb' },
- { command: 'b', selector: '.b1', args: { input: 'hello' } },
- { command: 'b', selector: '.b2', args: { input: 'world' } },
- { command: 'c', selector: '.c' },
- { command: 'd', selector: '.d' },
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
- },
- selector: '.s'
- }
- ];
- const merged = SettingRegistry.reconcileItems(a, b);
- expect(merged).toHaveLength(8);
- expect(merged![1].command).toEqual('b');
- expect(merged![1].selector).toEqual('.b');
- expect(merged![1].disabled).toEqual(true);
- expect(merged![2].submenu?.items).toHaveLength(2);
- expect(merged![3].command).toEqual('b');
- expect(merged![3].selector).toEqual('.bb');
- expect(merged![4].command).toEqual('b');
- expect(merged![4].args?.input).toEqual('hello');
- expect(merged![5].command).toEqual('b');
- expect(merged![5].args?.input).toEqual('world');
- });
- it('should merge items list without adding new ones', () => {
- const a: ISettingRegistry.IContextMenuItem[] = [
- { command: 'a', selector: '.a' },
- { command: 'b', selector: '.b' },
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [{ command: 'sub-1' }]
- },
- selector: '.sub'
- }
- ];
- const b: ISettingRegistry.IContextMenuItem[] = [
- { command: 'b', selector: '.b', disabled: true },
- { command: 'b', selector: '.b1', args: { input: 'hello' } },
- { command: 'b', selector: '.b2', args: { input: 'world' } },
- { command: 'c', selector: '.c' },
- { command: 'd', selector: '.d' },
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
- },
- selector: '.s'
- }
- ];
- const merged = SettingRegistry.reconcileItems(a, b, false, false);
- expect(merged).toHaveLength(3);
- expect(merged![1].command).toEqual('b');
- expect(merged![1].selector).toEqual('.b');
- expect(merged![1].disabled).toEqual(true);
- expect(merged![2].submenu?.items).toHaveLength(1);
- expect(merged![2].submenu?.items![0].command).toEqual('sub-1');
- expect(merged![2].submenu?.items![0].disabled).toEqual(true);
- });
- });
- describe('filterDisabledItems', () => {
- it('should remove disabled menu item', () => {
- const a: ISettingRegistry.IContextMenuItem[] = [
- { command: 'a', selector: '.a' },
- { type: 'separator', selector: '.a' },
- { command: 'b', disabled: true, selector: '.a' },
- {
- type: 'submenu',
- submenu: {
- id: 'sub',
- items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
- },
- selector: '.s'
- }
- ];
- const filtered = SettingRegistry.filterDisabledItems(a);
- expect(filtered).toHaveLength(3);
- expect(filtered[0]?.command).toEqual('a');
- expect(filtered[1]?.type).toEqual('separator');
- expect(filtered[2]?.type).toEqual('submenu');
- expect(filtered[2]?.submenu?.items).toHaveLength(1);
- expect(filtered[2]?.submenu?.items![0].command).toEqual('sub-2');
- });
- });
- describe('Settings', () => {
- const connector = new TestConnector();
- let registry: SettingRegistry;
- let settings: Settings | null;
- 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: ISettingRegistry.ISchema = { type: 'object' };
- const raw = '{ }';
- const version = 'test';
- const plugin = { id, data, raw, schema, version };
- settings = new Settings({ plugin, registry });
- expect(settings).toBeInstanceOf(Settings);
- });
- });
- describe('#changed', () => {
- it('should emit when a plugin changes', async () => {
- const id = 'alpha';
- const schema: ISettingRegistry.ISchema = { type: 'object' };
- connector.schemas[id] = schema;
- settings = (await registry.load(id)) as Settings;
- const promise = signalToPromise(settings.changed);
- await settings.set('foo', 'bar');
- await expect(promise).resolves.toContain(settings);
- });
- });
- describe('#composite', () => {
- it('should contain the merged user and default data', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: {
- type: typeof value as ISettingRegistry.Primitive,
- default: value
- }
- }
- });
- connector.schemas[id] = schema;
- settings = (await registry.load(id)) as Settings;
- expect(settings.composite[key]).toBe(value);
- });
- it('should privilege user data', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: {
- type: typeof value as ISettingRegistry.Primitive,
- default: 'delta'
- }
- }
- });
- connector.schemas[id] = schema;
- settings = (await registry.load(id)) as Settings;
- await settings.set(key, value);
- expect(settings.composite[key]).toBe(value);
- });
- });
- describe('#id', () => {
- it('should expose the plugin ID', () => {
- const id = 'alpha';
- const data = { composite: {}, user: {} };
- const schema: ISettingRegistry.ISchema = { type: 'object' };
- const raw = '{ }';
- const version = 'test';
- const plugin = { id, data, raw, schema, version };
- settings = new Settings({ plugin, registry });
- expect(settings.id).toBe(id);
- });
- });
- describe('#isDisposed', () => {
- it('should test whether the settings object is disposed', () => {
- const id = 'alpha';
- const data = { composite: {}, user: {} };
- const schema: ISettingRegistry.ISchema = { type: 'object' };
- const raw = '{ }';
- const version = 'test';
- const plugin = { id, data, raw, schema, version };
- settings = new Settings({ plugin, registry });
- expect(settings.isDisposed).toBe(false);
- settings.dispose();
- expect(settings.isDisposed).toBe(true);
- });
- });
- describe('#schema', () => {
- it('should expose the plugin schema', async () => {
- const id = 'alpha';
- const schema: ISettingRegistry.ISchema = { type: 'object' };
- connector.schemas[id] = schema;
- settings = (await registry.load(id)) as Settings;
- expect(settings.schema).toEqual(schema);
- });
- });
- describe('#user', () => {
- it('should privilege user data', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: {
- type: typeof value as ISettingRegistry.Primitive,
- default: 'delta'
- }
- }
- });
- connector.schemas[id] = schema;
- settings = (await registry.load(id)) as Settings;
- await settings.set(key, value);
- expect(settings.user[key]).toBe(value);
- });
- });
- describe('#registry', () => {
- it('should expose the setting registry', () => {
- const id = 'alpha';
- const data = { composite: {}, user: {} };
- const schema: ISettingRegistry.ISchema = { type: 'object' };
- const raw = '{ }';
- const version = 'test';
- const plugin = { id, data, raw, schema, version };
- settings = new Settings({ plugin, registry });
- expect(settings.registry).toBe(registry);
- });
- });
- describe('#dispose()', () => {
- it('should dispose the settings object', () => {
- const id = 'alpha';
- const data = { composite: {}, user: {} };
- const schema: ISettingRegistry.ISchema = { type: 'object' };
- const raw = '{ }';
- const version = 'test';
- const plugin = { id, data, raw, schema, version };
- settings = new Settings({ plugin, registry });
- expect(settings.isDisposed).toBe(false);
- settings.dispose();
- expect(settings.isDisposed).toBe(true);
- });
- });
- describe('#default()', () => {
- it('should return a fully extrapolated schema default', async () => {
- const id = 'omicron';
- const defaults = {
- foo: 'one',
- bar: 100,
- baz: {
- qux: 'two',
- quux: 'three',
- quuz: {
- corge: { grault: 200 }
- }
- }
- };
- connector.schemas[id] = {
- type: 'object',
- properties: {
- foo: { type: 'string', default: defaults.foo },
- bar: { type: 'number', default: defaults.bar },
- baz: {
- type: 'object',
- default: {},
- properties: {
- qux: { type: 'string', default: defaults.baz.qux },
- quux: { type: 'string', default: defaults.baz.quux },
- quuz: {
- type: 'object',
- default: {},
- properties: {
- corge: {
- type: 'object',
- default: {},
- properties: {
- grault: {
- type: 'number',
- default: defaults.baz.quuz.corge.grault
- }
- }
- }
- }
- }
- }
- },
- 'nonexistent-default': { type: 'string' }
- }
- };
- settings = (await registry.load(id)) as Settings;
- expect(settings.default('nonexistent-key')).toBeUndefined();
- expect(settings.default('foo')).toBe(defaults.foo);
- expect(settings.default('bar')).toBe(defaults.bar);
- expect(settings.default('baz')).toEqual(defaults.baz);
- expect(settings.default('nonexistent-default')).toBeUndefined();
- });
- });
- describe('#get()', () => {
- it('should get a setting item', async () => {
- const id = 'foo';
- const key = 'bar';
- const value = 'baz';
- connector.schemas[id] = { type: 'object' };
- await connector.save(id, JSON.stringify({ [key]: value }));
- settings = (await registry.load(id)) as Settings;
- const saved = settings.get(key);
- expect(saved.user).toBe(value);
- });
- it('should use schema default if user data not available', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: {
- type: typeof value as ISettingRegistry.Primitive,
- default: value
- }
- }
- });
- settings = (await registry.load(id)) as Settings;
- const saved = settings.get(key);
- expect(saved.composite).toBe(schema.properties![key].default);
- expect(saved.composite).not.toBe(saved.user);
- });
- it('should let user value override schema default', async () => {
- const id = 'alpha';
- const key = 'beta';
- const value = 'gamma';
- const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
- type: 'object',
- properties: {
- [key]: {
- type: typeof value as ISettingRegistry.Primitive,
- default: 'delta'
- }
- }
- });
- await connector.save(id, JSON.stringify({ [key]: value }));
- settings = (await registry.load(id)) as Settings;
- const saved = settings.get(key);
- expect(saved.composite).toBe(value);
- expect(saved.user).toBe(value);
- expect(saved.composite).not.toBe(schema.properties![key].default);
- expect(saved.user).not.toBe(schema.properties![key].default);
- });
- it('should be `undefined` if a key does not exist', async () => {
- const id = 'foo';
- const key = 'bar';
- connector.schemas[id] = { type: 'object' };
- settings = (await registry.load(id)) as Settings;
- const saved = settings.get(key);
- expect(saved.composite).toBeUndefined();
- expect(saved.user).toBeUndefined();
- });
- });
- describe('#remove()', () => {
- it('should remove a setting item', async () => {
- const id = 'foo';
- const key = 'bar';
- const value = 'baz';
- connector.schemas[id] = { type: 'object' };
- await connector.save(id, JSON.stringify({ [key]: value }));
- settings = (await registry.load(id)) as Settings;
- let saved = settings.get(key);
- expect(saved.user).toBe(value);
- await settings.remove(key);
- saved = settings.get(key);
- expect(saved.composite).toBeUndefined();
- expect(saved.user).toBeUndefined();
- });
- });
- describe('#save()', () => {
- it('should save user setting data', async () => {
- const id = 'foo';
- const one = 'one';
- const two = 'two';
- connector.schemas[id] = { type: 'object' };
- settings = (await registry.load(id)) as Settings;
- await settings.save(JSON.stringify({ one, two }));
- let saved = settings.get('one');
- expect(saved.composite).toBe(one);
- expect(saved.user).toBe(one);
- saved = settings.get('two');
- expect(saved.composite).toBe(two);
- expect(saved.user).toBe(two);
- });
- });
- describe('#set()', () => {
- it('should set a user setting item', async () => {
- const id = 'foo';
- const one = 'one';
- connector.schemas[id] = { type: 'object' };
- settings = (await registry.load(id)) as Settings;
- await settings.set('one', one);
- const saved = settings.get('one');
- expect(saved.composite).toBe(one);
- expect(saved.user).toBe(one);
- });
- });
- });
- });
|