inspector.spec.ts 2.9 KB

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