settingregistry.spec.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. DefaultSchemaValidator,
  5. ISettingRegistry,
  6. SettingRegistry,
  7. Settings
  8. } from '@jupyterlab/settingregistry';
  9. import { StateDB } from '@jupyterlab/statedb';
  10. import { signalToPromise } from '@jupyterlab/testutils';
  11. import { JSONObject } from '@lumino/coreutils';
  12. class TestConnector extends StateDB {
  13. schemas: { [key: string]: ISettingRegistry.ISchema } = {};
  14. async fetch(id: string): Promise<ISettingRegistry.IPlugin | undefined> {
  15. const fetched = await super.fetch(id);
  16. if (!fetched && !this.schemas[id]) {
  17. return undefined;
  18. }
  19. const schema: ISettingRegistry.ISchema = this.schemas[id] || {
  20. type: 'object'
  21. };
  22. const composite = {};
  23. const user = {};
  24. const raw = (fetched as string) || '{ }';
  25. const version = 'test';
  26. return { id, data: { composite, user }, raw, schema, version };
  27. }
  28. async list(): Promise<any> {
  29. return Promise.reject('list method not implemented');
  30. }
  31. }
  32. describe('@jupyterlab/settingregistry', () => {
  33. describe('DefaultSchemaValidator', () => {
  34. describe('#constructor()', () => {
  35. it('should create a new schema validator', () => {
  36. const validator = new DefaultSchemaValidator();
  37. expect(validator).toBeInstanceOf(DefaultSchemaValidator);
  38. });
  39. });
  40. describe('#validateData()', () => {
  41. it('should validate data against a schema', () => {
  42. const id = 'foo';
  43. const validator = new DefaultSchemaValidator();
  44. const schema: ISettingRegistry.ISchema = {
  45. additionalProperties: false,
  46. properties: {
  47. bar: { type: 'string' }
  48. },
  49. type: 'object'
  50. };
  51. const composite = {};
  52. const user = {};
  53. const raw = '{ "bar": "baz" }';
  54. const version = 'test';
  55. const plugin = { id, data: { composite, user }, raw, schema, version };
  56. const errors = validator.validateData(plugin);
  57. expect(errors).toBe(null);
  58. });
  59. it('should return errors if the data fails to validate', () => {
  60. const id = 'foo';
  61. const validator = new DefaultSchemaValidator();
  62. const schema: ISettingRegistry.ISchema = {
  63. additionalProperties: false,
  64. properties: {
  65. bar: { type: 'string' }
  66. },
  67. type: 'object'
  68. };
  69. const composite = {};
  70. const user = {};
  71. const raw = '{ "baz": "qux" }';
  72. const version = 'test';
  73. const plugin = { id, data: { composite, user }, raw, schema, version };
  74. const errors = validator.validateData(plugin);
  75. expect(errors).not.toBe(null);
  76. });
  77. it('should populate the composite data', () => {
  78. const id = 'foo';
  79. const validator = new DefaultSchemaValidator();
  80. const schema: ISettingRegistry.ISchema = {
  81. additionalProperties: false,
  82. properties: {
  83. bar: { type: 'string', default: 'baz' }
  84. },
  85. type: 'object'
  86. };
  87. const composite = {} as JSONObject;
  88. const user = {} as JSONObject;
  89. const raw = '{ }';
  90. const version = 'test';
  91. const plugin = { id, data: { composite, user }, raw, schema, version };
  92. const errors = validator.validateData(plugin);
  93. expect(errors).toBe(null);
  94. expect(plugin.data.user.bar).toBeUndefined();
  95. expect(plugin.data.composite.bar).toBe(schema.properties!.bar.default);
  96. });
  97. });
  98. });
  99. describe('SettingRegistry', () => {
  100. const connector = new TestConnector();
  101. const timeout = 500;
  102. let registry: SettingRegistry;
  103. afterEach(() => {
  104. connector.schemas = {};
  105. return connector.clear();
  106. });
  107. beforeEach(() => {
  108. registry = new SettingRegistry({ connector, timeout });
  109. });
  110. describe('#constructor()', () => {
  111. it('should create a new setting registry', () => {
  112. expect(registry).toBeInstanceOf(SettingRegistry);
  113. });
  114. });
  115. describe('#pluginChanged', () => {
  116. it('should emit when a plugin changes', async () => {
  117. const id = 'foo';
  118. const key = 'bar';
  119. const value = 'baz';
  120. connector.schemas[id] = { type: 'object' };
  121. let called = false;
  122. registry.pluginChanged.connect((sender: any, plugin: string) => {
  123. expect(id).toBe(plugin);
  124. called = true;
  125. });
  126. await registry.load(id);
  127. await registry.set(id, key, value);
  128. expect(called).toBe(true);
  129. });
  130. });
  131. describe('#plugins', () => {
  132. it('should return a list of registered plugins in registry', async () => {
  133. const one = 'foo';
  134. const two = 'bar';
  135. expect(Object.keys(registry.plugins)).toHaveLength(0);
  136. connector.schemas[one] = { type: 'object' };
  137. connector.schemas[two] = { type: 'object' };
  138. await registry.load(one);
  139. expect(Object.keys(registry.plugins)).toHaveLength(1);
  140. await registry.load(two);
  141. expect(Object.keys(registry.plugins)).toHaveLength(2);
  142. });
  143. });
  144. describe('#get()', () => {
  145. it('should get a setting item from a loaded plugin', async () => {
  146. const id = 'foo';
  147. const key = 'bar';
  148. const value = 'baz';
  149. connector.schemas[id] = { type: 'object' };
  150. await connector.save(id, JSON.stringify({ [key]: value }));
  151. (await registry.load(id)) as Settings;
  152. const saved = await registry.get(id, key);
  153. expect(saved.user).toBe(value);
  154. });
  155. it('should get a setting item from a plugin that is not loaded', async () => {
  156. const id = 'alpha';
  157. const key = 'beta';
  158. const value = 'gamma';
  159. connector.schemas[id] = { type: 'object' };
  160. await connector.save(id, JSON.stringify({ [key]: value }));
  161. const saved = await registry.get(id, key);
  162. expect(saved.composite).toBe(value);
  163. });
  164. it('should use schema default if user data not available', async () => {
  165. const id = 'alpha';
  166. const key = 'beta';
  167. const value = 'gamma';
  168. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  169. type: 'object',
  170. properties: {
  171. [key]: {
  172. type: typeof value as ISettingRegistry.Primitive,
  173. default: value
  174. }
  175. }
  176. });
  177. const saved = await registry.get(id, key);
  178. expect(saved.composite).toBe(schema.properties![key].default);
  179. expect(saved.composite).not.toBe(saved.user);
  180. });
  181. it('should let user value override schema default', async () => {
  182. const id = 'alpha';
  183. const key = 'beta';
  184. const value = 'gamma';
  185. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  186. type: 'object',
  187. properties: {
  188. [key]: {
  189. type: typeof value as ISettingRegistry.Primitive,
  190. default: 'delta'
  191. }
  192. }
  193. });
  194. await connector.save(id, JSON.stringify({ [key]: value }));
  195. const saved = await registry.get(id, key);
  196. expect(saved.composite).toBe(value);
  197. expect(saved.user).toBe(value);
  198. expect(saved.composite).not.toBe(schema.properties![key].default);
  199. expect(saved.user).not.toBe(schema.properties![key].default);
  200. });
  201. it('should reject if a plugin does not exist', async () => {
  202. let failed = false;
  203. try {
  204. await registry.get('foo', 'bar');
  205. } catch (e) {
  206. failed = true;
  207. }
  208. expect(failed).toBe(true);
  209. });
  210. it('should resolve `undefined` if a key does not exist', async () => {
  211. const id = 'foo';
  212. const key = 'bar';
  213. connector.schemas[id] = { type: 'object' };
  214. const saved = await registry.get(id, key);
  215. expect(saved.composite).toBeUndefined();
  216. expect(saved.user).toBeUndefined();
  217. });
  218. });
  219. describe('#load()', () => {
  220. it(`should resolve a registered plugin's settings`, async () => {
  221. const id = 'foo';
  222. expect(Object.keys(registry.plugins)).toHaveLength(0);
  223. connector.schemas[id] = { type: 'object' };
  224. const settings = (await registry.load(id)) as Settings;
  225. expect(settings.id).toBe(id);
  226. });
  227. it(`should reject if a plugin transformation times out`, async () => {
  228. const id = 'foo';
  229. let failed = false;
  230. connector.schemas[id] = {
  231. 'jupyter.lab.transform': true,
  232. type: 'object'
  233. };
  234. try {
  235. await registry.load(id);
  236. } catch (e) {
  237. failed = true;
  238. }
  239. expect(failed).toBe(true);
  240. });
  241. it('should reject if a plugin does not exist', async () => {
  242. let failed = false;
  243. try {
  244. await registry.load('foo');
  245. } catch (e) {
  246. failed = true;
  247. }
  248. expect(failed).toBe(true);
  249. });
  250. });
  251. describe('#reload()', () => {
  252. it(`should load a registered plugin's settings`, async () => {
  253. const id = 'foo';
  254. expect(Object.keys(registry.plugins)).toHaveLength(0);
  255. connector.schemas[id] = { type: 'object' };
  256. const settings = await registry.reload(id);
  257. expect(settings.id).toBe(id);
  258. });
  259. it(`should replace a registered plugin's settings`, async () => {
  260. const id = 'foo';
  261. const first = 'Foo';
  262. const second = 'Bar';
  263. expect(Object.keys(registry.plugins)).toHaveLength(0);
  264. connector.schemas[id] = { type: 'object', title: first };
  265. let settings = await registry.reload(id);
  266. expect(settings.schema.title).toBe(first);
  267. await Promise.resolve(undefined);
  268. connector.schemas[id].title = second;
  269. settings = await registry.reload(id);
  270. expect(settings.schema.title).toBe(second);
  271. });
  272. it('should reject if a plugin does not exist', async () => {
  273. let failed = false;
  274. try {
  275. await registry.reload('foo');
  276. } catch (e) {
  277. failed = true;
  278. }
  279. expect(failed).toBe(true);
  280. });
  281. });
  282. describe('#transform()', () => {
  283. it(`should transform a plugin during the fetch phase`, async () => {
  284. const id = 'foo';
  285. const version = 'transform-test';
  286. expect(Object.keys(registry.plugins)).toHaveLength(0);
  287. connector.schemas[id] = {
  288. 'jupyter.lab.transform': true,
  289. type: 'object'
  290. };
  291. registry.transform(id, {
  292. fetch: plugin => {
  293. plugin.version = version;
  294. return plugin;
  295. }
  296. });
  297. expect((await registry.load(id)).version).toBe(version);
  298. });
  299. it(`should transform a plugin during the compose phase`, async () => {
  300. const id = 'foo';
  301. const composite = { a: 1 };
  302. expect(Object.keys(registry.plugins)).toHaveLength(0);
  303. connector.schemas[id] = {
  304. 'jupyter.lab.transform': true,
  305. type: 'object'
  306. };
  307. registry.transform(id, {
  308. compose: plugin => {
  309. plugin.data = { user: plugin.data.user, composite };
  310. return plugin;
  311. }
  312. });
  313. expect((await registry.load(id)).composite).toEqual(composite);
  314. });
  315. it(`should disallow a transform that changes the plugin ID`, async () => {
  316. const id = 'foo';
  317. let failed = false;
  318. expect(Object.keys(registry.plugins)).toHaveLength(0);
  319. connector.schemas[id] = {
  320. 'jupyter.lab.transform': true,
  321. type: 'object'
  322. };
  323. registry.transform(id, {
  324. compose: plugin => {
  325. plugin.id = 'bar';
  326. return plugin;
  327. }
  328. });
  329. try {
  330. await registry.load(id);
  331. } catch (e) {
  332. failed = true;
  333. }
  334. expect(failed).toBe(true);
  335. });
  336. });
  337. });
  338. describe('reconcileMenus', () => {
  339. it('should merge menu tree', () => {
  340. const a: ISettingRegistry.IMenu[] = [
  341. {
  342. id: '1',
  343. items: [{ command: 'a' }]
  344. },
  345. {
  346. id: '2',
  347. items: [{ command: 'b' }]
  348. },
  349. {
  350. id: '4',
  351. items: [
  352. {
  353. type: 'submenu',
  354. submenu: {
  355. id: 'sub',
  356. items: [{ command: 'sub-1' }]
  357. }
  358. }
  359. ]
  360. }
  361. ];
  362. const b: ISettingRegistry.IMenu[] = [
  363. {
  364. id: '2',
  365. items: [
  366. { command: 'b' },
  367. { command: 'b', args: { input: 'hello' } },
  368. { command: 'b', args: { input: 'world' } },
  369. { command: 'c' }
  370. ]
  371. },
  372. {
  373. id: '3',
  374. items: [{ command: 'd' }]
  375. },
  376. {
  377. id: '4',
  378. items: [
  379. {
  380. type: 'submenu',
  381. submenu: {
  382. id: 'sub',
  383. items: [{ command: 'sub-2' }]
  384. }
  385. }
  386. ]
  387. }
  388. ];
  389. const merged = SettingRegistry.reconcileMenus(a, b);
  390. expect(merged).toHaveLength(4);
  391. expect(merged[0].id).toEqual('1');
  392. expect(merged[0].items).toHaveLength(1);
  393. expect(merged[1].id).toEqual('2');
  394. expect(merged[1].items).toHaveLength(4);
  395. expect((merged[1].items ?? [])[0].command).toEqual('b');
  396. expect((merged[1].items ?? [])[0].args).toBeUndefined();
  397. expect((merged[1].items ?? [])[1].command).toEqual('b');
  398. expect((merged[1].items ?? [])[1].args?.input).toEqual('hello');
  399. expect((merged[1].items ?? [])[2].command).toEqual('b');
  400. expect((merged[1].items ?? [])[2].args?.input).toEqual('world');
  401. expect(merged[2].id).toEqual('4');
  402. expect(merged[2].items).toHaveLength(1);
  403. expect((merged[2].items ?? [])[0].submenu?.items).toHaveLength(2);
  404. expect(merged[3].id).toEqual('3');
  405. expect(merged[3].items).toHaveLength(1);
  406. });
  407. it('should remove disabled menu', () => {
  408. const a: ISettingRegistry.IMenu[] = [
  409. {
  410. id: '1',
  411. items: [{ command: 'a' }]
  412. },
  413. {
  414. id: '2',
  415. items: [{ command: 'b' }]
  416. }
  417. ];
  418. const b: ISettingRegistry.IMenu[] = [
  419. {
  420. id: '2',
  421. disabled: true
  422. }
  423. ];
  424. const merged = SettingRegistry.reconcileMenus(a, b);
  425. expect(merged).toHaveLength(1);
  426. expect(merged[0].id).toEqual('1');
  427. expect(merged[0].items).toHaveLength(1);
  428. });
  429. it('should remove disabled menu item', () => {
  430. const a: ISettingRegistry.IMenu[] = [
  431. {
  432. id: '1',
  433. items: [{ command: 'a' }]
  434. },
  435. {
  436. id: '2',
  437. items: [{ command: 'b' }]
  438. },
  439. {
  440. id: '4',
  441. items: [
  442. {
  443. type: 'submenu',
  444. submenu: {
  445. id: 'sub',
  446. items: [{ command: 'sub-1' }, { command: 'sub-2' }]
  447. }
  448. }
  449. ]
  450. }
  451. ];
  452. const b: ISettingRegistry.IMenu[] = [
  453. {
  454. id: '2',
  455. items: [{ command: 'b', disabled: true }]
  456. },
  457. {
  458. id: '4',
  459. items: [
  460. {
  461. type: 'submenu',
  462. submenu: {
  463. id: 'sub',
  464. items: [{ command: 'sub-2', disabled: true }]
  465. }
  466. }
  467. ]
  468. }
  469. ];
  470. const merged = SettingRegistry.reconcileMenus(a, b);
  471. expect(merged).toHaveLength(3);
  472. expect(merged[0].id).toEqual('1');
  473. expect(merged[0].items).toHaveLength(1);
  474. expect(merged[1].id).toEqual('2');
  475. expect(merged[1].items).toHaveLength(0);
  476. expect(merged[2].id).toEqual('4');
  477. expect(merged[2].items).toHaveLength(1);
  478. expect((merged[2].items ?? [])[0].submenu?.items).toHaveLength(1);
  479. expect(
  480. ((merged[2].items ?? [])[0].submenu?.items ?? [])[0].command
  481. ).toEqual('sub-1');
  482. });
  483. });
  484. describe('Settings', () => {
  485. const connector = new TestConnector();
  486. let registry: SettingRegistry;
  487. let settings: Settings | null;
  488. afterEach(() => {
  489. if (settings) {
  490. settings.dispose();
  491. settings = null;
  492. }
  493. connector.schemas = {};
  494. return connector.clear();
  495. });
  496. beforeEach(() => {
  497. registry = new SettingRegistry({ connector });
  498. });
  499. describe('#constructor()', () => {
  500. it('should create a new settings object for a plugin', () => {
  501. const id = 'alpha';
  502. const data = { composite: {}, user: {} };
  503. const schema: ISettingRegistry.ISchema = { type: 'object' };
  504. const raw = '{ }';
  505. const version = 'test';
  506. const plugin = { id, data, raw, schema, version };
  507. settings = new Settings({ plugin, registry });
  508. expect(settings).toBeInstanceOf(Settings);
  509. });
  510. });
  511. describe('#changed', () => {
  512. it('should emit when a plugin changes', async () => {
  513. const id = 'alpha';
  514. const schema: ISettingRegistry.ISchema = { type: 'object' };
  515. connector.schemas[id] = schema;
  516. settings = (await registry.load(id)) as Settings;
  517. const promise = signalToPromise(settings.changed);
  518. await settings.set('foo', 'bar');
  519. await promise;
  520. });
  521. });
  522. describe('#composite', () => {
  523. it('should contain the merged user and default data', async () => {
  524. const id = 'alpha';
  525. const key = 'beta';
  526. const value = 'gamma';
  527. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  528. type: 'object',
  529. properties: {
  530. [key]: {
  531. type: typeof value as ISettingRegistry.Primitive,
  532. default: value
  533. }
  534. }
  535. });
  536. connector.schemas[id] = schema;
  537. settings = (await registry.load(id)) as Settings;
  538. expect(settings.composite[key]).toBe(value);
  539. });
  540. it('should privilege user data', async () => {
  541. const id = 'alpha';
  542. const key = 'beta';
  543. const value = 'gamma';
  544. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  545. type: 'object',
  546. properties: {
  547. [key]: {
  548. type: typeof value as ISettingRegistry.Primitive,
  549. default: 'delta'
  550. }
  551. }
  552. });
  553. connector.schemas[id] = schema;
  554. settings = (await registry.load(id)) as Settings;
  555. await settings.set(key, value);
  556. expect(settings.composite[key]).toBe(value);
  557. });
  558. });
  559. describe('#id', () => {
  560. it('should expose the plugin ID', () => {
  561. const id = 'alpha';
  562. const data = { composite: {}, user: {} };
  563. const schema: ISettingRegistry.ISchema = { type: 'object' };
  564. const raw = '{ }';
  565. const version = 'test';
  566. const plugin = { id, data, raw, schema, version };
  567. settings = new Settings({ plugin, registry });
  568. expect(settings.id).toBe(id);
  569. });
  570. });
  571. describe('#isDisposed', () => {
  572. it('should test whether the settings object is disposed', () => {
  573. const id = 'alpha';
  574. const data = { composite: {}, user: {} };
  575. const schema: ISettingRegistry.ISchema = { type: 'object' };
  576. const raw = '{ }';
  577. const version = 'test';
  578. const plugin = { id, data, raw, schema, version };
  579. settings = new Settings({ plugin, registry });
  580. expect(settings.isDisposed).toBe(false);
  581. settings.dispose();
  582. expect(settings.isDisposed).toBe(true);
  583. });
  584. });
  585. describe('#schema', () => {
  586. it('should expose the plugin schema', async () => {
  587. const id = 'alpha';
  588. const schema: ISettingRegistry.ISchema = { type: 'object' };
  589. connector.schemas[id] = schema;
  590. settings = (await registry.load(id)) as Settings;
  591. expect(settings.schema).toEqual(schema);
  592. });
  593. });
  594. describe('#user', () => {
  595. it('should privilege user data', async () => {
  596. const id = 'alpha';
  597. const key = 'beta';
  598. const value = 'gamma';
  599. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  600. type: 'object',
  601. properties: {
  602. [key]: {
  603. type: typeof value as ISettingRegistry.Primitive,
  604. default: 'delta'
  605. }
  606. }
  607. });
  608. connector.schemas[id] = schema;
  609. settings = (await registry.load(id)) as Settings;
  610. await settings.set(key, value);
  611. expect(settings.user[key]).toBe(value);
  612. });
  613. });
  614. describe('#registry', () => {
  615. it('should expose the setting registry', () => {
  616. const id = 'alpha';
  617. const data = { composite: {}, user: {} };
  618. const schema: ISettingRegistry.ISchema = { type: 'object' };
  619. const raw = '{ }';
  620. const version = 'test';
  621. const plugin = { id, data, raw, schema, version };
  622. settings = new Settings({ plugin, registry });
  623. expect(settings.registry).toBe(registry);
  624. });
  625. });
  626. describe('#dispose()', () => {
  627. it('should dispose the settings object', () => {
  628. const id = 'alpha';
  629. const data = { composite: {}, user: {} };
  630. const schema: ISettingRegistry.ISchema = { type: 'object' };
  631. const raw = '{ }';
  632. const version = 'test';
  633. const plugin = { id, data, raw, schema, version };
  634. settings = new Settings({ plugin, registry });
  635. expect(settings.isDisposed).toBe(false);
  636. settings.dispose();
  637. expect(settings.isDisposed).toBe(true);
  638. });
  639. });
  640. describe('#default()', () => {
  641. it('should return a fully extrapolated schema default', async () => {
  642. const id = 'omicron';
  643. const defaults = {
  644. foo: 'one',
  645. bar: 100,
  646. baz: {
  647. qux: 'two',
  648. quux: 'three',
  649. quuz: {
  650. corge: { grault: 200 }
  651. }
  652. }
  653. };
  654. connector.schemas[id] = {
  655. type: 'object',
  656. properties: {
  657. foo: { type: 'string', default: defaults.foo },
  658. bar: { type: 'number', default: defaults.bar },
  659. baz: {
  660. type: 'object',
  661. default: {},
  662. properties: {
  663. qux: { type: 'string', default: defaults.baz.qux },
  664. quux: { type: 'string', default: defaults.baz.quux },
  665. quuz: {
  666. type: 'object',
  667. default: {},
  668. properties: {
  669. corge: {
  670. type: 'object',
  671. default: {},
  672. properties: {
  673. grault: {
  674. type: 'number',
  675. default: defaults.baz.quuz.corge.grault
  676. }
  677. }
  678. }
  679. }
  680. }
  681. }
  682. },
  683. 'nonexistent-default': { type: 'string' }
  684. }
  685. };
  686. settings = (await registry.load(id)) as Settings;
  687. expect(settings.default('nonexistent-key')).toBeUndefined();
  688. expect(settings.default('foo')).toBe(defaults.foo);
  689. expect(settings.default('bar')).toBe(defaults.bar);
  690. expect(settings.default('baz')).toEqual(defaults.baz);
  691. expect(settings.default('nonexistent-default')).toBeUndefined();
  692. });
  693. });
  694. describe('#get()', () => {
  695. it('should get a setting item', async () => {
  696. const id = 'foo';
  697. const key = 'bar';
  698. const value = 'baz';
  699. connector.schemas[id] = { type: 'object' };
  700. await connector.save(id, JSON.stringify({ [key]: value }));
  701. settings = (await registry.load(id)) as Settings;
  702. const saved = settings.get(key);
  703. expect(saved.user).toBe(value);
  704. });
  705. it('should use schema default if user data not available', async () => {
  706. const id = 'alpha';
  707. const key = 'beta';
  708. const value = 'gamma';
  709. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  710. type: 'object',
  711. properties: {
  712. [key]: {
  713. type: typeof value as ISettingRegistry.Primitive,
  714. default: value
  715. }
  716. }
  717. });
  718. settings = (await registry.load(id)) as Settings;
  719. const saved = settings.get(key);
  720. expect(saved.composite).toBe(schema.properties![key].default);
  721. expect(saved.composite).not.toBe(saved.user);
  722. });
  723. it('should let user value override schema default', async () => {
  724. const id = 'alpha';
  725. const key = 'beta';
  726. const value = 'gamma';
  727. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  728. type: 'object',
  729. properties: {
  730. [key]: {
  731. type: typeof value as ISettingRegistry.Primitive,
  732. default: 'delta'
  733. }
  734. }
  735. });
  736. await connector.save(id, JSON.stringify({ [key]: value }));
  737. settings = (await registry.load(id)) as Settings;
  738. const saved = settings.get(key);
  739. expect(saved.composite).toBe(value);
  740. expect(saved.user).toBe(value);
  741. expect(saved.composite).not.toBe(schema.properties![key].default);
  742. expect(saved.user).not.toBe(schema.properties![key].default);
  743. });
  744. it('should be `undefined` if a key does not exist', async () => {
  745. const id = 'foo';
  746. const key = 'bar';
  747. connector.schemas[id] = { type: 'object' };
  748. settings = (await registry.load(id)) as Settings;
  749. const saved = settings.get(key);
  750. expect(saved.composite).toBeUndefined();
  751. expect(saved.user).toBeUndefined();
  752. });
  753. });
  754. describe('#remove()', () => {
  755. it('should remove a setting item', async () => {
  756. const id = 'foo';
  757. const key = 'bar';
  758. const value = 'baz';
  759. connector.schemas[id] = { type: 'object' };
  760. await connector.save(id, JSON.stringify({ [key]: value }));
  761. settings = (await registry.load(id)) as Settings;
  762. let saved = settings.get(key);
  763. expect(saved.user).toBe(value);
  764. await settings.remove(key);
  765. saved = settings.get(key);
  766. expect(saved.composite).toBeUndefined();
  767. expect(saved.user).toBeUndefined();
  768. });
  769. });
  770. describe('#save()', () => {
  771. it('should save user setting data', async () => {
  772. const id = 'foo';
  773. const one = 'one';
  774. const two = 'two';
  775. connector.schemas[id] = { type: 'object' };
  776. settings = (await registry.load(id)) as Settings;
  777. await settings.save(JSON.stringify({ one, two }));
  778. let saved = settings.get('one');
  779. expect(saved.composite).toBe(one);
  780. expect(saved.user).toBe(one);
  781. saved = settings.get('two');
  782. expect(saved.composite).toBe(two);
  783. expect(saved.user).toBe(two);
  784. });
  785. });
  786. describe('#set()', () => {
  787. it('should set a user setting item', async () => {
  788. const id = 'foo';
  789. const one = 'one';
  790. connector.schemas[id] = { type: 'object' };
  791. settings = (await registry.load(id)) as Settings;
  792. await settings.set('one', one);
  793. const saved = settings.get('one');
  794. expect(saved.composite).toBe(one);
  795. expect(saved.user).toBe(one);
  796. });
  797. });
  798. });
  799. });