|
1 | 1 | // Copyright (c) Jupyter Development Team. |
2 | 2 | // Distributed under the terms of the Modified BSD License. |
3 | 3 |
|
4 | | -import type { INotebookModel, NotebookPanel } from '@jupyterlab/notebook'; |
5 | | -import { NotebookToCModel } from '@jupyterlab/notebook'; |
| 4 | +import { Sanitizer } from '@jupyterlab/apputils'; |
| 5 | +import type { Cell } from '@jupyterlab/cells'; |
6 | 6 | import type { Context } from '@jupyterlab/docregistry'; |
| 7 | +import type { |
| 8 | + INotebookModel, |
| 9 | + KernelError, |
| 10 | + NotebookPanel |
| 11 | +} from '@jupyterlab/notebook'; |
| 12 | +import { NotebookToCModel, RunningStatus } from '@jupyterlab/notebook'; |
7 | 13 | import { NBTestUtils } from '@jupyterlab/notebook/lib/testutils'; |
8 | | -import { Sanitizer } from '@jupyterlab/apputils'; |
9 | 14 | import { signalToPromise } from '@jupyterlab/testing'; |
10 | 15 | import * as utils from './utils'; |
11 | 16 |
|
| 17 | +class TestKernelError extends Error { |
| 18 | + readonly errorName = 'ZeroDivisionError'; |
| 19 | + readonly errorValue = 'division by zero'; |
| 20 | + readonly traceback: string[] = []; |
| 21 | +} |
| 22 | + |
| 23 | +class TestNotebookToCModel extends NotebookToCModel { |
| 24 | + scheduleExecution(cell: Cell): void { |
| 25 | + this.onExecutionScheduled(null, { notebook: this.widget.content, cell }); |
| 26 | + } |
| 27 | + |
| 28 | + finishExecution( |
| 29 | + cell: Cell, |
| 30 | + success: boolean, |
| 31 | + error: KernelError | null |
| 32 | + ): void { |
| 33 | + this.onExecuted(null, { |
| 34 | + notebook: this.widget.content, |
| 35 | + cell, |
| 36 | + success, |
| 37 | + error |
| 38 | + }); |
| 39 | + } |
| 40 | +} |
| 41 | + |
12 | 42 | describe('@jupyterlab/notebook', () => { |
13 | 43 | describe('NotebookToCModel', () => { |
14 | 44 | let context: Context<INotebookModel>; |
15 | 45 | let panel: NotebookPanel; |
16 | | - let model: NotebookToCModel; |
| 46 | + let model: TestNotebookToCModel; |
17 | 47 | const sanitizer = new Sanitizer(); |
18 | 48 | const initialCells = [{ cell_type: 'markdown', source: '# heading' }]; |
19 | 49 |
|
20 | 50 | beforeEach(async () => { |
21 | 51 | context = await NBTestUtils.createMockContext(false); |
22 | 52 | panel = utils.createNotebookPanel(context); |
23 | | - model = new NotebookToCModel(panel, null, sanitizer); |
| 53 | + model = new TestNotebookToCModel(panel, null, sanitizer); |
24 | 54 | panel.model!.sharedModel.insertCells(0, initialCells); |
25 | 55 | }); |
26 | 56 |
|
@@ -72,5 +102,69 @@ describe('@jupyterlab/notebook', () => { |
72 | 102 | await promise; |
73 | 103 | }); |
74 | 104 | }); |
| 105 | + |
| 106 | + describe('#refresh', () => { |
| 107 | + it('should clear error status when an errored cell is deleted', async () => { |
| 108 | + panel.model!.sharedModel.insertCells(1, [ |
| 109 | + { cell_type: 'code', source: '1 / 2' }, |
| 110 | + { cell_type: 'code', source: '1 / 0' }, |
| 111 | + { cell_type: 'code', source: '1 / 2' } |
| 112 | + ]); |
| 113 | + |
| 114 | + await model.refresh(); |
| 115 | + |
| 116 | + const erroredCell = panel.content.widgets[2]; |
| 117 | + model.scheduleExecution(erroredCell); |
| 118 | + model.finishExecution(erroredCell, false, new TestKernelError()); |
| 119 | + |
| 120 | + expect(model.headings[0].isRunning).toBe(RunningStatus.Error); |
| 121 | + |
| 122 | + panel.model!.sharedModel.deleteCell(2); |
| 123 | + expect(panel.content.widgets).not.toContain(erroredCell); |
| 124 | + await model.refresh(); |
| 125 | + |
| 126 | + expect(model.headings[0].isRunning).toBe(RunningStatus.Idle); |
| 127 | + expect(model.headings[0].dataset!['data-running']).toBe( |
| 128 | + RunningStatus.Idle.toString() |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should preserve execution status updates during heading refresh', async () => { |
| 133 | + panel.model!.sharedModel.insertCells(1, [ |
| 134 | + { cell_type: 'code', source: '1 / 0' } |
| 135 | + ]); |
| 136 | + |
| 137 | + await model.refresh(); |
| 138 | + |
| 139 | + const headingCell = panel.content.widgets[0]; |
| 140 | + const erroredCell = panel.content.widgets[1]; |
| 141 | + const getHeadings = headingCell.getHeadings.bind(headingCell); |
| 142 | + let resumeRefresh: (() => void) | undefined; |
| 143 | + const refreshGate = new Promise<void>(resolve => { |
| 144 | + resumeRefresh = resolve; |
| 145 | + }); |
| 146 | + const getHeadingsSpy = jest |
| 147 | + .spyOn(headingCell, 'getHeadings') |
| 148 | + .mockImplementation(async () => { |
| 149 | + await refreshGate; |
| 150 | + return getHeadings(); |
| 151 | + }); |
| 152 | + |
| 153 | + const refresh = model.refresh(); |
| 154 | + await Promise.resolve(); |
| 155 | + |
| 156 | + model.scheduleExecution(erroredCell); |
| 157 | + model.finishExecution(erroredCell, false, new TestKernelError()); |
| 158 | + |
| 159 | + resumeRefresh!(); |
| 160 | + await refresh; |
| 161 | + getHeadingsSpy.mockRestore(); |
| 162 | + |
| 163 | + expect(model.headings[0].isRunning).toBe(RunningStatus.Error); |
| 164 | + expect(model.headings[0].dataset!['data-running']).toBe( |
| 165 | + RunningStatus.Error.toString() |
| 166 | + ); |
| 167 | + }); |
| 168 | + }); |
75 | 169 | }); |
76 | 170 | }); |
0 commit comments