settingregistry.spec.ts 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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', disabled: true },
  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: [
  384. { command: 'sub-1', disabled: true },
  385. { command: 'sub-2' }
  386. ]
  387. }
  388. }
  389. ]
  390. }
  391. ];
  392. const merged = SettingRegistry.reconcileMenus(a, b);
  393. expect(merged).toHaveLength(4);
  394. expect(merged[0].id).toEqual('1');
  395. expect(merged[0].items).toHaveLength(1);
  396. expect(merged[1].id).toEqual('2');
  397. expect(merged[1].items).toHaveLength(4);
  398. expect(merged[1].items![0].command).toEqual('b');
  399. expect(merged[1].items![0].args).toBeUndefined();
  400. expect(merged[1].items![0].disabled).toEqual(true);
  401. expect(merged[1].items![1].command).toEqual('b');
  402. expect(merged[1].items![1].args?.input).toEqual('hello');
  403. expect(merged[1].items![2].command).toEqual('b');
  404. expect(merged[1].items![2].args?.input).toEqual('world');
  405. expect(merged[2].id).toEqual('4');
  406. expect(merged[2].items).toHaveLength(1);
  407. expect(merged[2].items![0].submenu?.items).toHaveLength(2);
  408. expect(merged[2].items![0].submenu?.items![0].command).toEqual('sub-1');
  409. expect(merged[2].items![0].submenu?.items![0].disabled).toEqual(true);
  410. expect(merged[3].id).toEqual('3');
  411. expect(merged[3].items).toHaveLength(1);
  412. });
  413. it('should merge menu tree without adding new items', () => {
  414. const a: ISettingRegistry.IMenu[] = [
  415. {
  416. id: '1',
  417. items: [{ command: 'a' }]
  418. },
  419. {
  420. id: '2',
  421. items: [{ command: 'b' }]
  422. },
  423. {
  424. id: '4',
  425. items: [
  426. {
  427. type: 'submenu',
  428. submenu: {
  429. id: 'sub',
  430. items: [{ command: 'sub-1' }]
  431. }
  432. }
  433. ]
  434. }
  435. ];
  436. const b: ISettingRegistry.IMenu[] = [
  437. {
  438. id: '2',
  439. items: [
  440. { command: 'b', disabled: true },
  441. { command: 'b', args: { input: 'hello' } },
  442. { command: 'b', args: { input: 'world' } },
  443. { command: 'c' }
  444. ]
  445. },
  446. {
  447. id: '3',
  448. items: [{ command: 'd' }]
  449. },
  450. {
  451. id: '4',
  452. items: [
  453. {
  454. type: 'submenu',
  455. submenu: {
  456. id: 'sub',
  457. items: [
  458. { command: 'sub-1', disabled: true },
  459. { command: 'sub-2' }
  460. ]
  461. }
  462. }
  463. ],
  464. disabled: true
  465. }
  466. ];
  467. const merged = SettingRegistry.reconcileMenus(a, b, false, false);
  468. expect(merged).toHaveLength(3);
  469. expect(merged![0].id).toEqual('1');
  470. expect(merged![0].items).toHaveLength(1);
  471. expect(merged![1].id).toEqual('2');
  472. expect(merged![1].items).toHaveLength(1);
  473. expect(merged[1].items![0].command).toEqual('b');
  474. expect(merged[1].items![0].args).toBeUndefined();
  475. expect(merged[1].items![0].disabled).toEqual(true);
  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(merged[2].items![0].submenu?.items![0].command).toEqual('sub-1');
  480. expect(merged[2].items![0].submenu?.items![0].disabled).toEqual(true);
  481. expect(merged[2].disabled).toEqual(true);
  482. });
  483. });
  484. describe('reconcileItems', () => {
  485. it('should merge items list', () => {
  486. const a: ISettingRegistry.IContextMenuItem[] = [
  487. { command: 'a', selector: '.a' },
  488. { command: 'b', selector: '.b' },
  489. {
  490. type: 'submenu',
  491. submenu: {
  492. id: 'sub',
  493. items: [{ command: 'sub-1' }]
  494. },
  495. selector: '.sub'
  496. }
  497. ];
  498. const b: ISettingRegistry.IContextMenuItem[] = [
  499. { command: 'b', selector: '.b', disabled: true },
  500. { command: 'b', selector: '.bb' },
  501. { command: 'b', selector: '.b1', args: { input: 'hello' } },
  502. { command: 'b', selector: '.b2', args: { input: 'world' } },
  503. { command: 'c', selector: '.c' },
  504. { command: 'd', selector: '.d' },
  505. {
  506. type: 'submenu',
  507. submenu: {
  508. id: 'sub',
  509. items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
  510. },
  511. selector: '.s'
  512. }
  513. ];
  514. const merged = SettingRegistry.reconcileItems(a, b);
  515. expect(merged).toHaveLength(8);
  516. expect(merged![1].command).toEqual('b');
  517. expect(merged![1].selector).toEqual('.b');
  518. expect(merged![1].disabled).toEqual(true);
  519. expect(merged![2].submenu?.items).toHaveLength(2);
  520. expect(merged![3].command).toEqual('b');
  521. expect(merged![3].selector).toEqual('.bb');
  522. expect(merged![4].command).toEqual('b');
  523. expect(merged![4].args?.input).toEqual('hello');
  524. expect(merged![5].command).toEqual('b');
  525. expect(merged![5].args?.input).toEqual('world');
  526. });
  527. it('should merge items list without adding new ones', () => {
  528. const a: ISettingRegistry.IContextMenuItem[] = [
  529. { command: 'a', selector: '.a' },
  530. { command: 'b', selector: '.b' },
  531. {
  532. type: 'submenu',
  533. submenu: {
  534. id: 'sub',
  535. items: [{ command: 'sub-1' }]
  536. },
  537. selector: '.sub'
  538. }
  539. ];
  540. const b: ISettingRegistry.IContextMenuItem[] = [
  541. { command: 'b', selector: '.b', disabled: true },
  542. { command: 'b', selector: '.b1', args: { input: 'hello' } },
  543. { command: 'b', selector: '.b2', args: { input: 'world' } },
  544. { command: 'c', selector: '.c' },
  545. { command: 'd', selector: '.d' },
  546. {
  547. type: 'submenu',
  548. submenu: {
  549. id: 'sub',
  550. items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
  551. },
  552. selector: '.s'
  553. }
  554. ];
  555. const merged = SettingRegistry.reconcileItems(a, b, false, false);
  556. expect(merged).toHaveLength(3);
  557. expect(merged![1].command).toEqual('b');
  558. expect(merged![1].selector).toEqual('.b');
  559. expect(merged![1].disabled).toEqual(true);
  560. expect(merged![2].submenu?.items).toHaveLength(1);
  561. expect(merged![2].submenu?.items![0].command).toEqual('sub-1');
  562. expect(merged![2].submenu?.items![0].disabled).toEqual(true);
  563. });
  564. });
  565. describe('reconcileToolbarItems', () => {
  566. it('should merge toolbar items list', () => {
  567. const a: ISettingRegistry.IToolbarItem[] = [
  568. { name: 'a' },
  569. { name: 'b', command: 'command-b' }
  570. ];
  571. const b: ISettingRegistry.IToolbarItem[] = [
  572. { name: 'b', disabled: true },
  573. { name: 'c', type: 'spacer' },
  574. { name: 'd', command: 'command-d' }
  575. ];
  576. const merged = SettingRegistry.reconcileToolbarItems(a, b);
  577. expect(merged).toHaveLength(4);
  578. expect(merged![0].name).toEqual('a');
  579. expect(merged![1].name).toEqual('b');
  580. expect(merged![1].disabled).toEqual(true);
  581. expect(merged![2].name).toEqual('c');
  582. expect(merged![2].type).toEqual('spacer');
  583. expect(merged![3].name).toEqual('d');
  584. });
  585. });
  586. describe('filterDisabledItems', () => {
  587. it('should remove disabled menu item', () => {
  588. const a: ISettingRegistry.IContextMenuItem[] = [
  589. { command: 'a', selector: '.a' },
  590. { type: 'separator', selector: '.a' },
  591. { command: 'b', disabled: true, selector: '.a' },
  592. {
  593. type: 'submenu',
  594. submenu: {
  595. id: 'sub',
  596. items: [{ command: 'sub-1', disabled: true }, { command: 'sub-2' }]
  597. },
  598. selector: '.s'
  599. }
  600. ];
  601. const filtered = SettingRegistry.filterDisabledItems(a);
  602. expect(filtered).toHaveLength(3);
  603. expect(filtered[0]?.command).toEqual('a');
  604. expect(filtered[1]?.type).toEqual('separator');
  605. expect(filtered[2]?.type).toEqual('submenu');
  606. expect(filtered[2]?.submenu?.items).toHaveLength(1);
  607. expect(filtered[2]?.submenu?.items![0].command).toEqual('sub-2');
  608. });
  609. });
  610. describe('Settings', () => {
  611. const connector = new TestConnector();
  612. let registry: SettingRegistry;
  613. let settings: Settings | null;
  614. afterEach(() => {
  615. if (settings) {
  616. settings.dispose();
  617. settings = null;
  618. }
  619. connector.schemas = {};
  620. return connector.clear();
  621. });
  622. beforeEach(() => {
  623. registry = new SettingRegistry({ connector });
  624. });
  625. describe('#constructor()', () => {
  626. it('should create a new settings object for a plugin', () => {
  627. const id = 'alpha';
  628. const data = { composite: {}, user: {} };
  629. const schema: ISettingRegistry.ISchema = { type: 'object' };
  630. const raw = '{ }';
  631. const version = 'test';
  632. const plugin = { id, data, raw, schema, version };
  633. settings = new Settings({ plugin, registry });
  634. expect(settings).toBeInstanceOf(Settings);
  635. });
  636. });
  637. describe('#changed', () => {
  638. it('should emit when a plugin changes', async () => {
  639. const id = 'alpha';
  640. const schema: ISettingRegistry.ISchema = { type: 'object' };
  641. connector.schemas[id] = schema;
  642. settings = (await registry.load(id)) as Settings;
  643. const promise = signalToPromise(settings.changed);
  644. await settings.set('foo', 'bar');
  645. await expect(promise).resolves.toContain(settings);
  646. });
  647. });
  648. describe('#composite', () => {
  649. it('should contain the merged user and default data', async () => {
  650. const id = 'alpha';
  651. const key = 'beta';
  652. const value = 'gamma';
  653. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  654. type: 'object',
  655. properties: {
  656. [key]: {
  657. type: typeof value as ISettingRegistry.Primitive,
  658. default: value
  659. }
  660. }
  661. });
  662. connector.schemas[id] = schema;
  663. settings = (await registry.load(id)) as Settings;
  664. expect(settings.composite[key]).toBe(value);
  665. });
  666. it('should privilege user data', async () => {
  667. const id = 'alpha';
  668. const key = 'beta';
  669. const value = 'gamma';
  670. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  671. type: 'object',
  672. properties: {
  673. [key]: {
  674. type: typeof value as ISettingRegistry.Primitive,
  675. default: 'delta'
  676. }
  677. }
  678. });
  679. connector.schemas[id] = schema;
  680. settings = (await registry.load(id)) as Settings;
  681. await settings.set(key, value);
  682. expect(settings.composite[key]).toBe(value);
  683. });
  684. });
  685. describe('#id', () => {
  686. it('should expose the plugin ID', () => {
  687. const id = 'alpha';
  688. const data = { composite: {}, user: {} };
  689. const schema: ISettingRegistry.ISchema = { type: 'object' };
  690. const raw = '{ }';
  691. const version = 'test';
  692. const plugin = { id, data, raw, schema, version };
  693. settings = new Settings({ plugin, registry });
  694. expect(settings.id).toBe(id);
  695. });
  696. });
  697. describe('#isDisposed', () => {
  698. it('should test whether the settings object is disposed', () => {
  699. const id = 'alpha';
  700. const data = { composite: {}, user: {} };
  701. const schema: ISettingRegistry.ISchema = { type: 'object' };
  702. const raw = '{ }';
  703. const version = 'test';
  704. const plugin = { id, data, raw, schema, version };
  705. settings = new Settings({ plugin, registry });
  706. expect(settings.isDisposed).toBe(false);
  707. settings.dispose();
  708. expect(settings.isDisposed).toBe(true);
  709. });
  710. });
  711. describe('#schema', () => {
  712. it('should expose the plugin schema', async () => {
  713. const id = 'alpha';
  714. const schema: ISettingRegistry.ISchema = { type: 'object' };
  715. connector.schemas[id] = schema;
  716. settings = (await registry.load(id)) as Settings;
  717. expect(settings.schema).toEqual(schema);
  718. });
  719. });
  720. describe('#user', () => {
  721. it('should privilege user data', async () => {
  722. const id = 'alpha';
  723. const key = 'beta';
  724. const value = 'gamma';
  725. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  726. type: 'object',
  727. properties: {
  728. [key]: {
  729. type: typeof value as ISettingRegistry.Primitive,
  730. default: 'delta'
  731. }
  732. }
  733. });
  734. connector.schemas[id] = schema;
  735. settings = (await registry.load(id)) as Settings;
  736. await settings.set(key, value);
  737. expect(settings.user[key]).toBe(value);
  738. });
  739. });
  740. describe('#registry', () => {
  741. it('should expose the setting registry', () => {
  742. const id = 'alpha';
  743. const data = { composite: {}, user: {} };
  744. const schema: ISettingRegistry.ISchema = { type: 'object' };
  745. const raw = '{ }';
  746. const version = 'test';
  747. const plugin = { id, data, raw, schema, version };
  748. settings = new Settings({ plugin, registry });
  749. expect(settings.registry).toBe(registry);
  750. });
  751. });
  752. describe('#dispose()', () => {
  753. it('should dispose the settings object', () => {
  754. const id = 'alpha';
  755. const data = { composite: {}, user: {} };
  756. const schema: ISettingRegistry.ISchema = { type: 'object' };
  757. const raw = '{ }';
  758. const version = 'test';
  759. const plugin = { id, data, raw, schema, version };
  760. settings = new Settings({ plugin, registry });
  761. expect(settings.isDisposed).toBe(false);
  762. settings.dispose();
  763. expect(settings.isDisposed).toBe(true);
  764. });
  765. });
  766. describe('#default()', () => {
  767. it('should return a fully extrapolated schema default', async () => {
  768. const id = 'omicron';
  769. const defaults = {
  770. foo: 'one',
  771. bar: 100,
  772. baz: {
  773. qux: 'two',
  774. quux: 'three',
  775. quuz: {
  776. corge: { grault: 200 }
  777. }
  778. }
  779. };
  780. connector.schemas[id] = {
  781. type: 'object',
  782. properties: {
  783. foo: { type: 'string', default: defaults.foo },
  784. bar: { type: 'number', default: defaults.bar },
  785. baz: {
  786. type: 'object',
  787. default: {},
  788. properties: {
  789. qux: { type: 'string', default: defaults.baz.qux },
  790. quux: { type: 'string', default: defaults.baz.quux },
  791. quuz: {
  792. type: 'object',
  793. default: {},
  794. properties: {
  795. corge: {
  796. type: 'object',
  797. default: {},
  798. properties: {
  799. grault: {
  800. type: 'number',
  801. default: defaults.baz.quuz.corge.grault
  802. }
  803. }
  804. }
  805. }
  806. }
  807. }
  808. },
  809. 'nonexistent-default': { type: 'string' }
  810. }
  811. };
  812. settings = (await registry.load(id)) as Settings;
  813. expect(settings.default('nonexistent-key')).toBeUndefined();
  814. expect(settings.default('foo')).toBe(defaults.foo);
  815. expect(settings.default('bar')).toBe(defaults.bar);
  816. expect(settings.default('baz')).toEqual(defaults.baz);
  817. expect(settings.default('nonexistent-default')).toBeUndefined();
  818. });
  819. });
  820. describe('#get()', () => {
  821. it('should get a setting item', async () => {
  822. const id = 'foo';
  823. const key = 'bar';
  824. const value = 'baz';
  825. connector.schemas[id] = { type: 'object' };
  826. await connector.save(id, JSON.stringify({ [key]: value }));
  827. settings = (await registry.load(id)) as Settings;
  828. const saved = settings.get(key);
  829. expect(saved.user).toBe(value);
  830. });
  831. it('should use schema default if user data not available', async () => {
  832. const id = 'alpha';
  833. const key = 'beta';
  834. const value = 'gamma';
  835. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  836. type: 'object',
  837. properties: {
  838. [key]: {
  839. type: typeof value as ISettingRegistry.Primitive,
  840. default: value
  841. }
  842. }
  843. });
  844. settings = (await registry.load(id)) as Settings;
  845. const saved = settings.get(key);
  846. expect(saved.composite).toBe(schema.properties![key].default);
  847. expect(saved.composite).not.toBe(saved.user);
  848. });
  849. it('should let user value override schema default', async () => {
  850. const id = 'alpha';
  851. const key = 'beta';
  852. const value = 'gamma';
  853. const schema: ISettingRegistry.ISchema = (connector.schemas[id] = {
  854. type: 'object',
  855. properties: {
  856. [key]: {
  857. type: typeof value as ISettingRegistry.Primitive,
  858. default: 'delta'
  859. }
  860. }
  861. });
  862. await connector.save(id, JSON.stringify({ [key]: value }));
  863. settings = (await registry.load(id)) as Settings;
  864. const saved = settings.get(key);
  865. expect(saved.composite).toBe(value);
  866. expect(saved.user).toBe(value);
  867. expect(saved.composite).not.toBe(schema.properties![key].default);
  868. expect(saved.user).not.toBe(schema.properties![key].default);
  869. });
  870. it('should be `undefined` if a key does not exist', async () => {
  871. const id = 'foo';
  872. const key = 'bar';
  873. connector.schemas[id] = { type: 'object' };
  874. settings = (await registry.load(id)) as Settings;
  875. const saved = settings.get(key);
  876. expect(saved.composite).toBeUndefined();
  877. expect(saved.user).toBeUndefined();
  878. });
  879. });
  880. describe('#remove()', () => {
  881. it('should remove a setting item', async () => {
  882. const id = 'foo';
  883. const key = 'bar';
  884. const value = 'baz';
  885. connector.schemas[id] = { type: 'object' };
  886. await connector.save(id, JSON.stringify({ [key]: value }));
  887. settings = (await registry.load(id)) as Settings;
  888. let saved = settings.get(key);
  889. expect(saved.user).toBe(value);
  890. await settings.remove(key);
  891. saved = settings.get(key);
  892. expect(saved.composite).toBeUndefined();
  893. expect(saved.user).toBeUndefined();
  894. });
  895. });
  896. describe('#save()', () => {
  897. it('should save user setting data', async () => {
  898. const id = 'foo';
  899. const one = 'one';
  900. const two = 'two';
  901. connector.schemas[id] = { type: 'object' };
  902. settings = (await registry.load(id)) as Settings;
  903. await settings.save(JSON.stringify({ one, two }));
  904. let saved = settings.get('one');
  905. expect(saved.composite).toBe(one);
  906. expect(saved.user).toBe(one);
  907. saved = settings.get('two');
  908. expect(saved.composite).toBe(two);
  909. expect(saved.user).toBe(two);
  910. });
  911. });
  912. describe('#set()', () => {
  913. it('should set a user setting item', async () => {
  914. const id = 'foo';
  915. const one = 'one';
  916. connector.schemas[id] = { type: 'object' };
  917. settings = (await registry.load(id)) as Settings;
  918. await settings.set('one', one);
  919. const saved = settings.get('one');
  920. expect(saved.composite).toBe(one);
  921. expect(saved.user).toBe(one);
  922. });
  923. });
  924. });
  925. });