From 23a69f9b690eedbd50dd7d61f51cc64b50df5282 Mon Sep 17 00:00:00 2001 From: Maddy <12754347+MaddyDev@users.noreply.github.com> Date: Fri, 18 Mar 2022 16:13:58 -0700 Subject: [PATCH] Add test for last edit mode (#18734) * add test for persisting last edit mode * remove empty line * address comments * update comment --- .../test/electron-browser/cell.test.ts | 60 +++++++++++++++++++ .../services/notebook/browser/models/cell.ts | 5 ++ 2 files changed, 65 insertions(+) diff --git a/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts b/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts index e96b37973e..f8941d3f6f 100644 --- a/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts +++ b/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts @@ -1358,4 +1358,64 @@ suite('Cell Model', function (): void { assert(editMode); assert.strictEqual(editMode, CellEditModes.WYSIWYG, 'Default edit mode should be WYSIWYG.'); }); + + test('cell should have lastEditMode set to whatever the user edited out of last', async function () { + let notebookModel = new NotebookModelStub({ + name: '', + version: '', + mimetype: '' + }); + let contents: nb.ICellContents = { + cell_type: CellTypes.Markdown, + source: '', + metadata: {} + }; + let cellModel = factory.createCell(contents, { notebook: notebookModel, isTrusted: false }); + + // Non-Editing Preview mode -> showPreview should be true and showMarkdown should be false. + assert(cellModel.showPreview, 'showPreview should default to true when not in editMode'); + assert(!cellModel.showMarkdown, 'showMarkdown should be false when not in editMode'); + + let getCurrentCellEditModePromise = () => { + return new Promise((resolve, reject) => { + cellModel.onCurrentEditModeChanged(cellEditMode => { + resolve(cellEditMode); + }); + }); + }; + + let cellModePromise = getCurrentCellEditModePromise(); + // Initially mode is defaulted be WYSIWYG -> showPreview is true and showMarkdown is false + assert.strictEqual(cellModel.currentMode, CellEditModes.WYSIWYG, 'Current mode should be WYSIWYG when not in edit mode'); + assert.strictEqual(cellModel.isEditMode, false, 'cell should not default to edit mode'); + + cellModel.isEditMode = true; + let lastEditMode = await cellModePromise; + assert.strictEqual(lastEditMode, CellEditModes.WYSIWYG, 'Default edit mode should be WYSIWYG'); + // update mode to SPLITVIEW -> showMarkdown and showPreview both are true + cellModePromise = getCurrentCellEditModePromise(); + cellModel.showMarkdown = true; + lastEditMode = await cellModePromise; + assert.strictEqual(lastEditMode, CellEditModes.SPLIT, 'LastEditMode should be set to split view'); + + // come out of edit mode and enter edit mode again to check edit mode. + cellModel.isEditMode = false; + assert.strictEqual(cellModel.currentMode, CellEditModes.WYSIWYG, 'Should default to WYSIWYG when not editing'); + cellModel.isEditMode = true; + assert.strictEqual(cellModel.currentMode, CellEditModes.SPLIT, 'Should persist lastEditMode and be in Split View'); + + // update mode to markdown mode only -> showPreview is false and showMarkdown is true + cellModePromise = getCurrentCellEditModePromise(); + cellModel.showPreview = false; + lastEditMode = await cellModePromise; + assert.strictEqual(lastEditMode, CellEditModes.MARKDOWN, 'LastEditMode should be set to markdown'); + + // come out of edit mode and enter edit mode again to check edit mode. + cellModel.isEditMode = false; + assert.strictEqual(cellModel.currentMode, CellEditModes.WYSIWYG, 'Should default to WYSIWYG when not editing'); + cellModel.isEditMode = true; + assert.strictEqual(cellModel.currentMode, CellEditModes.MARKDOWN, 'Should persist lastEditMode and be in markdown only'); + + }); + }); diff --git a/src/sql/workbench/services/notebook/browser/models/cell.ts b/src/sql/workbench/services/notebook/browser/models/cell.ts index 192cd31d4a..48140bbc6f 100644 --- a/src/sql/workbench/services/notebook/browser/models/cell.ts +++ b/src/sql/workbench/services/notebook/browser/models/cell.ts @@ -241,6 +241,11 @@ export class CellModel extends Disposable implements ICellModel { const newEditMode = this._lastEditMode ?? this._defaultTextEditMode; this.showPreview = newEditMode !== TextCellEditModes.Markdown; this.showMarkdown = newEditMode !== TextCellEditModes.RichText; + } else { + // when not in edit mode, default the values since they are only valid when editing. + // And to return the correct currentMode value. + this._showMarkdown = false; + this._showPreview = true; } this._onCellModeChanged.fire(this._isEditMode); // Note: this does not require a notebook update as it does not change overall state