observablejson.spec.ts 2.0 KB

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