Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cfb0d40

Browse files
authored
Tests for undo using keyboard shortcut 'z' (microsoft#7824)
1 parent 3bdbd7d commit cfb0d40

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/datascience-ui/interactive-common/mainStateController.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,13 @@ export class MainStateController implements IMessageHandler {
517517
// Skip if already has focus
518518
if (cellId !== this.pendingState.focusedCellId) {
519519
const newVMs = [...this.pendingState.cellVMs];
520-
// Switch the old vm
521-
const oldSelect = this.findCellIndex(this.pendingState.selectedCellId);
522-
if (oldSelect >= 0) {
523-
newVMs[oldSelect] = { ...newVMs[oldSelect], selected: false, focused: false };
524-
}
520+
// Reset the old vms (nothing should be selected/focused)
521+
// Change state only for cells that were selected/focused
522+
newVMs.forEach((cellVM, index) => {
523+
if (cellVM.selected || cellVM.focused){
524+
newVMs[index] = { ...cellVM, selected: false, focused: false };
525+
}
526+
});
525527
const newSelect = this.findCellIndex(cellId);
526528
if (newSelect >= 0) {
527529
newVMs[newSelect] = { ...newVMs[newSelect], selected: true, focused: true };
@@ -536,11 +538,13 @@ export class MainStateController implements IMessageHandler {
536538
// Skip if already the same cell
537539
if (this.pendingState.selectedCellId !== cellId || this.pendingState.focusedCellId !== focusedCellId) {
538540
const newVMs = [...this.pendingState.cellVMs];
539-
// Switch the old vm
540-
const oldSelect = this.findCellIndex(this.pendingState.selectedCellId);
541-
if (oldSelect >= 0) {
542-
newVMs[oldSelect] = { ...newVMs[oldSelect], selected: false, focused: false };
543-
}
541+
// Reset the old vms (nothing should be selected/focused)
542+
// Change state only for cells that were selected/focused
543+
newVMs.forEach((cellVM, index) => {
544+
if (cellVM.selected || cellVM.focused){
545+
newVMs[index] = { ...cellVM, selected: false, focused: false };
546+
}
547+
});
544548
const newSelect = this.findCellIndex(cellId);
545549
if (newSelect >= 0) {
546550
newVMs[newSelect] = { ...newVMs[newSelect], selected: true, focused: focusedCellId === newVMs[newSelect].cell.id };

src/test/datascience/nativeEditor.functional.test.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -659,5 +659,34 @@ suite('DataScience Native Editor', () => {
659659
assert.equal(wrapper.find(NativeCell).at(1).find(CellOutput).length, 0);
660660
assert.equal(wrapper.find(NativeCell).at(1).find(MonacoEditor).length, 1);
661661
});
662+
663+
test('Test undo using the key \'z\'', async () => {
664+
clickCell(0);
665+
666+
// Add, then undo, keep doing at least 3 times and confirm it works as expected.
667+
for (let i = 0; i < 3; i += 1){
668+
// Add a new cell
669+
let update = waitForUpdate(wrapper, NativeEditor, 1);
670+
simulateKeyPressOnCell(0, { code: 'a' });
671+
await update;
672+
673+
// There should be 4 cells and first cell is selected & nothing focused.
674+
assert.equal(isCellSelected(wrapper, 'NativeCell', 0), true);
675+
assert.equal(isCellSelected(wrapper, 'NativeCell', 1), false);
676+
assert.equal(isCellFocused(wrapper, 'NativeCell', 0), false);
677+
assert.equal(isCellFocused(wrapper, 'NativeCell', 1), false);
678+
assert.equal(wrapper.find('NativeCell').length, 4);
679+
680+
// Press 'z' to undo.
681+
update = waitForUpdate(wrapper, NativeEditor, 1);
682+
simulateKeyPressOnCell(0, { code: 'z' });
683+
await update;
684+
685+
// There should be 3 cells and first cell is selected & nothing focused.
686+
assert.equal(isCellSelected(wrapper, 'NativeCell', 0), true);
687+
assert.equal(isCellSelected(wrapper, 'NativeCell', 1), false);
688+
assert.equal(wrapper.find('NativeCell').length, 3);
689+
}
690+
});
662691
});
663692
});

0 commit comments

Comments
 (0)