diff --git a/extensions/notebook/src/test/common/notebookUtils.test.ts b/extensions/notebook/src/test/common/notebookUtils.test.ts index a22389ad86..2434225e56 100644 --- a/extensions/notebook/src/test/common/notebookUtils.test.ts +++ b/extensions/notebook/src/test/common/notebookUtils.test.ts @@ -15,6 +15,7 @@ import * as uuid from 'uuid'; import { promises as fs } from 'fs'; import { tryDeleteFile } from './testUtils'; import { CellTypes } from '../../contracts/content'; +import { NBFORMAT, NBFORMAT_MINOR } from '../../common/constants'; describe('notebookUtils Tests', function (): void { let notebookUtils: NotebookUtils = new NotebookUtils(); @@ -91,6 +92,34 @@ describe('notebookUtils Tests', function (): void { await notebookUtils.openNotebook(); should(showErrorMessageSpy.calledOnce).be.true('showErrorMessage should have been called'); }); + + it('closing and opening an untitled notebook shows correct contents', async function (): Promise { + await notebookUtils.newNotebook(); + await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); + await notebookUtils.newNotebook({ + initialContent: { + cells: [{ + source: 'test content', + cell_type: 'markdown' + }], + metadata: { + kernelspec: { + name: 'SQL', + language: 'sql', + display_name: 'SQL' + } + }, + nbformat: NBFORMAT, + nbformat_minor: NBFORMAT_MINOR + } + }); + let activeEditor = azdata.nb.activeNotebookEditor; + let cells = activeEditor.document.cells; + // We currently can't retrieve the cell source from the extension API, but all we care + // about is that the notebook doesn't open as empty again, so just check the number of + // cells here. + should(cells.length).be.greaterThan(0); + }); }); describe('runActiveCell', function () { diff --git a/src/sql/workbench/contrib/notebook/browser/models/notebookInput.ts b/src/sql/workbench/contrib/notebook/browser/models/notebookInput.ts index dec4733d14..db47aa24d9 100644 --- a/src/sql/workbench/contrib/notebook/browser/models/notebookInput.ts +++ b/src/sql/workbench/contrib/notebook/browser/models/notebookInput.ts @@ -77,6 +77,7 @@ export class NotebookEditorModel extends EditorModel { }, err => undefined); } })); + this._register(this.textEditorModel); if (this.textEditorModel instanceof UntitledTextEditorModel) { this._register(this.textEditorModel.onDidChangeDirty(e => { let dirty = this.textEditorModel instanceof TextResourceEditorModel ? false : this.textEditorModel.isDirty(); @@ -420,6 +421,7 @@ export abstract class NotebookInput extends EditorInput implements INotebookInpu } } else { const textEditorModelReference = await this.textModelService.createModelReference(this.resource); + this._register(textEditorModelReference); textEditorModelReference.object.textEditorModel.onBeforeAttached(); await textEditorModelReference.object.resolve(); textOrUntitledEditorModel = textEditorModelReference.object as TextFileEditorModel | TextResourceEditorModel;