tracker.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { Cell } from '@jupyterlab/cells';
  5. import { Context } from '@jupyterlab/docregistry';
  6. import { INotebookModel, NotebookPanel, NotebookTracker } from '../src';
  7. import { initNotebookContext } from '@jupyterlab/testutils';
  8. import { JupyterServer } from '@jupyterlab/testutils/lib/start_jupyter_server';
  9. import * as utils from './utils';
  10. const namespace = 'notebook-tracker-test';
  11. const server = new JupyterServer();
  12. beforeAll(async () => {
  13. jest.setTimeout(20000);
  14. await server.start();
  15. });
  16. afterAll(async () => {
  17. await server.shutdown();
  18. });
  19. class TestTracker extends NotebookTracker {
  20. methods: string[] = [];
  21. protected onCurrentChanged(widget: NotebookPanel): void {
  22. super.onCurrentChanged(widget);
  23. this.methods.push('onCurrentChanged');
  24. }
  25. }
  26. describe('@jupyterlab/notebook', () => {
  27. describe('NotebookTracker', () => {
  28. let context: Context<INotebookModel>;
  29. beforeEach(async () => {
  30. context = await initNotebookContext();
  31. });
  32. afterEach(() => {
  33. context.dispose();
  34. });
  35. describe('#constructor()', () => {
  36. it('should create a NotebookTracker', () => {
  37. const tracker = new NotebookTracker({ namespace });
  38. expect(tracker).toBeInstanceOf(NotebookTracker);
  39. });
  40. });
  41. describe('#activeCell', () => {
  42. it('should be `null` if there is no tracked notebook panel', () => {
  43. const tracker = new NotebookTracker({ namespace });
  44. expect(tracker.activeCell).toBeNull();
  45. });
  46. it('should be `null` if a tracked notebook has no active cell', () => {
  47. const tracker = new NotebookTracker({ namespace });
  48. const panel = utils.createNotebookPanel(context);
  49. panel.content.model!.cells.clear();
  50. void tracker.add(panel);
  51. expect(tracker.activeCell).toBeNull();
  52. });
  53. it('should be the active cell if a tracked notebook has one', async () => {
  54. const tracker = new NotebookTracker({ namespace });
  55. const panel = utils.createNotebookPanel(context);
  56. await tracker.add(panel);
  57. panel.content.model!.fromJSON(utils.DEFAULT_CONTENT);
  58. expect(tracker.activeCell).toBeInstanceOf(Cell);
  59. panel.dispose();
  60. });
  61. });
  62. describe('#activeCellChanged', () => {
  63. it('should emit a signal when the active cell changes', async () => {
  64. const tracker = new NotebookTracker({ namespace });
  65. const panel = utils.createNotebookPanel(context);
  66. let count = 0;
  67. tracker.activeCellChanged.connect(() => {
  68. count++;
  69. });
  70. panel.content.model!.fromJSON(utils.DEFAULT_CONTENT);
  71. await tracker.add(panel);
  72. expect(count).toBe(1);
  73. panel.content.activeCellIndex = 1;
  74. expect(count).toBe(2);
  75. panel.dispose();
  76. });
  77. });
  78. describe('#onCurrentChanged()', () => {
  79. it('should be called when the active cell changes', async () => {
  80. const tracker = new TestTracker({ namespace });
  81. const panel = utils.createNotebookPanel(context);
  82. await tracker.add(panel);
  83. expect(tracker.methods).toEqual(
  84. expect.arrayContaining(['onCurrentChanged'])
  85. );
  86. });
  87. });
  88. });
  89. });