inspector.spec.ts 3.0 KB

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