inspector.spec.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { Signal } from '@lumino/signaling';
  4. import { Widget } from '@lumino/widgets';
  5. import { IInspector, InspectorPanel } from '@jupyterlab/inspector';
  6. class TestInspectorPanel extends InspectorPanel {
  7. methods: string[] = [];
  8. protected onInspectorUpdate(
  9. sender: any,
  10. args: IInspector.IInspectorUpdate
  11. ): void {
  12. super.onInspectorUpdate(sender, args);
  13. this.methods.push('onInspectorUpdate');
  14. }
  15. }
  16. class TestInspectable implements IInspector.IInspectable {
  17. disposed = new Signal<this, void>(this);
  18. cleared = new Signal<this, void>(this);
  19. inspected = new Signal<this, IInspector.IInspectorUpdate>(this);
  20. isDisposed = false;
  21. standby = false;
  22. }
  23. describe('inspector/index', () => {
  24. describe('Inspector', () => {
  25. describe('#constructor()', () => {
  26. it('should construct a new inspector widget', () => {
  27. const widget = new InspectorPanel();
  28. expect(widget).toBeInstanceOf(InspectorPanel);
  29. });
  30. it('should add the `jp-Inspector` class', () => {
  31. const widget = new InspectorPanel();
  32. expect(widget.hasClass('jp-Inspector')).toBe(true);
  33. });
  34. });
  35. describe('#source', () => {
  36. it('should default to `null`', () => {
  37. const widget = new InspectorPanel();
  38. expect(widget.source).toBeNull();
  39. });
  40. it('should be settable multiple times', () => {
  41. const widget = new InspectorPanel();
  42. const source = new TestInspectable();
  43. expect(widget.source).toBeNull();
  44. widget.source = source;
  45. expect(widget.source).toBe(source);
  46. widget.source = null;
  47. expect(widget.source).toBeNull();
  48. widget.source = new TestInspectable();
  49. expect(widget.source).toBeInstanceOf(TestInspectable);
  50. });
  51. });
  52. describe('#dispose()', () => {
  53. it('should dispose of the resources used by the inspector', () => {
  54. const widget = new InspectorPanel();
  55. expect(widget.isDisposed).toBe(false);
  56. widget.dispose();
  57. expect(widget.isDisposed).toBe(true);
  58. });
  59. it('should be a no-op if called more than once', () => {
  60. const widget = new InspectorPanel();
  61. expect(widget.isDisposed).toBe(false);
  62. widget.dispose();
  63. widget.dispose();
  64. expect(widget.isDisposed).toBe(true);
  65. });
  66. });
  67. describe('#onInspectorUpdate()', () => {
  68. it('should fire when a source updates', () => {
  69. const widget = new TestInspectorPanel();
  70. widget.source = new TestInspectable();
  71. expect(widget.methods).toEqual(
  72. expect.not.arrayContaining(['onInspectorUpdate'])
  73. );
  74. (widget.source.inspected as any).emit({ content: new Widget() });
  75. expect(widget.methods).toEqual(
  76. expect.arrayContaining(['onInspectorUpdate'])
  77. );
  78. });
  79. });
  80. });
  81. });