Add test for last edit mode (#18734)

* add test for persisting last edit mode

* remove empty line

* address comments

* update comment
This commit is contained in:
Maddy
2022-03-18 16:13:58 -07:00
committed by GitHub
parent 6263cc2f87
commit 23a69f9b69
2 changed files with 65 additions and 0 deletions

View File

@@ -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');
});
});

View File

@@ -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