observablejson.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { IObservableJSON, ObservableJSON } from '@jupyterlab/observables';
  5. describe('@jupyterlab/observables', () => {
  6. describe('ObservableJSON', () => {
  7. describe('#constructor()', () => {
  8. it('should create an observable JSON object', () => {
  9. const item = new ObservableJSON();
  10. expect(item).toBeInstanceOf(ObservableJSON);
  11. });
  12. it('should accept initial values', () => {
  13. const item = new ObservableJSON({
  14. values: { foo: 1, bar: 'baz' }
  15. });
  16. expect(item).toBeInstanceOf(ObservableJSON);
  17. });
  18. });
  19. describe('#toJSON()', () => {
  20. it('should serialize the model to JSON', () => {
  21. const item = new ObservableJSON();
  22. item.set('foo', 1);
  23. expect(item.toJSON()['foo']).toBe(1);
  24. });
  25. it('should return a copy of the data', () => {
  26. const item = new ObservableJSON();
  27. item.set('foo', { bar: 1 });
  28. const value = item.toJSON();
  29. value['bar'] = 2;
  30. expect((item.get('foo') as any)['bar']).toBe(1);
  31. });
  32. });
  33. });
  34. describe('ObservableJSON.ChangeMessage', () => {
  35. describe('#constructor()', () => {
  36. it('should create a new message', () => {
  37. const message = new ObservableJSON.ChangeMessage('jsonvalue-changed', {
  38. key: 'foo',
  39. type: 'add',
  40. oldValue: 1,
  41. newValue: 2
  42. });
  43. expect(message).toBeInstanceOf(ObservableJSON.ChangeMessage);
  44. });
  45. });
  46. describe('#args', () => {
  47. it('should be the args of the message', () => {
  48. const args: IObservableJSON.IChangedArgs = {
  49. key: 'foo',
  50. type: 'add',
  51. oldValue: 'ho',
  52. newValue: 'hi'
  53. };
  54. const message = new ObservableJSON.ChangeMessage(
  55. 'jsonvalue-changed',
  56. args
  57. );
  58. expect(message.args).toBe(args);
  59. });
  60. });
  61. });
  62. });