config.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) Jupyter Development Team.
  2. import 'jest';
  3. import { UUID } from '@lumino/coreutils';
  4. import { JSONObject } from '@lumino/coreutils';
  5. import { ConfigSection, ConfigWithDefaults } from '../../src';
  6. import { expectFailure, JupyterServer } from '@jupyterlab/testutils';
  7. import { handleRequest, makeSettings, getRequestHandler } from '../utils';
  8. /**
  9. * Generate a random config section name.
  10. *
  11. * #### Notes
  12. * Config sections cannot have dashes (see
  13. * https://github.com/jupyter/notebook/blob/b2edf8963cc017733f264cca35fd6584f328c8b6/notebook/services/config/handlers.py#L36),
  14. * so we remove the dashes.
  15. */
  16. function randomName() {
  17. return UUID.uuid4().replace(/-/g, '');
  18. }
  19. const server = new JupyterServer();
  20. beforeAll(async () => {
  21. await server.start();
  22. });
  23. afterAll(async () => {
  24. await server.shutdown();
  25. });
  26. describe('config', () => {
  27. describe('ConfigSection.create()', () => {
  28. it('should load a config', async () => {
  29. const config = await ConfigSection.create({ name: randomName() });
  30. expect(Object.keys(config.data).length).toBe(0);
  31. });
  32. it('should accept server settings', async () => {
  33. const serverSettings = makeSettings();
  34. const config = await ConfigSection.create({
  35. name: randomName(),
  36. serverSettings
  37. });
  38. expect(Object.keys(config.data).length).toBe(0);
  39. });
  40. it('should fail for an incorrect response', async () => {
  41. const serverSettings = getRequestHandler(201, {});
  42. const configPromise = ConfigSection.create({
  43. name: randomName(),
  44. serverSettings
  45. });
  46. await expectFailure(configPromise, 'Invalid response: 201 Created');
  47. });
  48. });
  49. describe('#update()', () => {
  50. it('should update a config', async () => {
  51. const config = await ConfigSection.create({ name: randomName() });
  52. const data: any = await config.update({ foo: 'baz', spam: 'eggs' });
  53. expect(data.foo).toBe('baz');
  54. expect(config.data['foo']).toBe('baz');
  55. expect(data['spam']).toBe('eggs');
  56. expect(config.data['spam']).toBe('eggs');
  57. });
  58. it('should accept server settings', async () => {
  59. const serverSettings = makeSettings();
  60. const config = await ConfigSection.create({
  61. name: randomName(),
  62. serverSettings
  63. });
  64. const data: any = await config.update({ foo: 'baz', spam: 'eggs' });
  65. expect(data.foo).toBe('baz');
  66. expect(config.data['foo']).toBe('baz');
  67. expect(data['spam']).toBe('eggs');
  68. expect(config.data['spam']).toBe('eggs');
  69. });
  70. it('should fail for an incorrect response', async () => {
  71. const config = await ConfigSection.create({ name: randomName() });
  72. handleRequest(config, 201, {});
  73. const update = config.update({ foo: 'baz' });
  74. await expectFailure(update, 'Invalid response: 201 Created');
  75. });
  76. });
  77. });
  78. describe('jupyter.services - ConfigWithDefaults', () => {
  79. describe('#constructor()', () => {
  80. it('should complete properly', async () => {
  81. const defaults: JSONObject = { spam: 'eggs' };
  82. const className = 'testclass';
  83. const section = await ConfigSection.create({
  84. name: randomName()
  85. });
  86. const config = new ConfigWithDefaults({
  87. section,
  88. defaults,
  89. className
  90. });
  91. expect(config).toBeInstanceOf(ConfigWithDefaults);
  92. });
  93. });
  94. describe('#get()', () => {
  95. it('should get a new config value', async () => {
  96. const defaults: JSONObject = { foo: 'bar' };
  97. const className = 'testclass';
  98. const section = await ConfigSection.create({
  99. name: randomName()
  100. });
  101. const config = new ConfigWithDefaults({
  102. section,
  103. defaults,
  104. className
  105. });
  106. const data = config.get('foo');
  107. expect(data).toBe('bar');
  108. });
  109. it('should get a default config value', async () => {
  110. const defaults: JSONObject = { spam: 'eggs' };
  111. const className = 'testclass';
  112. const section = await ConfigSection.create({
  113. name: randomName()
  114. });
  115. const config = new ConfigWithDefaults({
  116. section,
  117. defaults,
  118. className
  119. });
  120. const data = config.get('spam');
  121. expect(data).toBe('eggs');
  122. });
  123. it('should get a default config value with no class', async () => {
  124. const defaults: JSONObject = { spam: 'eggs' };
  125. const className = 'testclass';
  126. const section = await ConfigSection.create({
  127. name: randomName()
  128. });
  129. const config = new ConfigWithDefaults({
  130. section,
  131. defaults,
  132. className
  133. });
  134. const data = config.get('spam');
  135. expect(data).toBe('eggs');
  136. });
  137. it('should get a falsey value', async () => {
  138. const defaults: JSONObject = { foo: true };
  139. const className = 'testclass';
  140. const serverSettings = getRequestHandler(200, { foo: false });
  141. const section = await ConfigSection.create({
  142. name: randomName(),
  143. serverSettings
  144. });
  145. const config = new ConfigWithDefaults({
  146. section,
  147. defaults,
  148. className
  149. });
  150. const data = config.get('foo');
  151. expect(data).toBeFalsy();
  152. });
  153. });
  154. describe('#set()', () => {
  155. it('should set a value in a class immediately', async () => {
  156. const className = 'testclass';
  157. const section = await ConfigSection.create({ name: randomName() });
  158. const config = new ConfigWithDefaults({ section, className });
  159. let data: any = await config.set('foo', 'bar');
  160. data = section.data['testclass'] as JSONObject;
  161. expect(data['foo']).toBe('bar');
  162. });
  163. it('should set a top level value', async () => {
  164. const section = await ConfigSection.create({ name: randomName() });
  165. const config = new ConfigWithDefaults({ section });
  166. const set = config.set('foo', 'bar');
  167. expect(section.data['foo']).toBe('bar');
  168. await set;
  169. expect(section.data['foo']).toBe('bar');
  170. });
  171. it('should fail for an invalid response', async () => {
  172. const serverSettings = getRequestHandler(200, {});
  173. const section = await ConfigSection.create({
  174. name: randomName(),
  175. serverSettings
  176. });
  177. handleRequest(section, 201, { foo: 'bar' });
  178. const config = new ConfigWithDefaults({ section });
  179. const set = config.set('foo', 'bar');
  180. expect(section.data['foo']).toBe('bar');
  181. await expectFailure(set, 'Invalid response: 201 Created');
  182. });
  183. });
  184. });