tracker.spec.ts 3.2 KB

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