Skip to content

Commit 90b0e0e

Browse files
author
Pierre-Luc Gagné
committed
fix: use mei.clearSelection() in clearHistory and fix tests
1 parent 1e50bf0 commit 90b0e0e

2 files changed

Lines changed: 23 additions & 25 deletions

File tree

src/plugin/operationHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default function (mei: MindElixirInstance) {
9292
history = []
9393
currentIndex = -1
9494
current = mei.getData()
95-
currentSelectedNodes = []
95+
mei.clearSelection()
9696
}
9797
const handleOperation = function (operation: Operation) {
9898
if (operation.name === 'beginEdit') return

tests/clear-history.spec.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,40 @@ const diagramA = {
44
nodeData: {
55
id: 'root-a',
66
topic: 'Diagram A',
7-
children: [
8-
{ id: 'child-a', topic: 'Child A' },
9-
],
7+
children: [{ id: 'child-a', topic: 'Child A' }],
108
},
119
}
1210

1311
const diagramB = {
1412
nodeData: {
1513
id: 'root-b',
1614
topic: 'Diagram B',
17-
children: [
18-
{ id: 'child-b', topic: 'Child B' },
19-
],
15+
children: [{ id: 'child-b', topic: 'Child B' }],
2016
},
2117
}
2218

2319
test.beforeEach(async ({ me }) => {
2420
await me.init(diagramA)
2521
})
2622

27-
test('clearHistory - undo cannot revert into the pre-refresh diagram', async ({ page, me }) => {
23+
test('clearHistory - undo cannot revert into pre-refresh diagram', async ({ page, me }) => {
2824
// Perform an operation in Diagram A
2925
await me.click('Child A')
3026
await page.keyboard.press('Tab')
3127
await page.keyboard.press('Enter')
3228
await expect(me.getByText('New Node')).toBeVisible()
3329

34-
// Load a new diagram and clear the history
35-
await page.evaluate(data => {
30+
// Load Diagram B and clear the history stack
31+
await page.evaluate((data: typeof diagramB) => {
3632
const mind = (window as any)['#map']
3733
mind.refresh(data)
3834
mind.clearHistory()
3935
}, diagramB)
4036

41-
// Diagram B should now be shown
4237
await expect(me.getByText('Diagram B')).toBeVisible()
4338
await expect(me.getByText('Diagram A')).toBeHidden()
4439

45-
// Undo should NOT travel back into Diagram A
40+
// Undo should be a no-op — must not travel back into Diagram A
4641
await page.keyboard.press('Control+z')
4742
await expect(me.getByText('Diagram B')).toBeVisible()
4843
await expect(me.getByText('Diagram A')).toBeHidden()
@@ -54,18 +49,20 @@ test('clearHistory - undo cannot revert into the pre-refresh diagram', async ({
5449
})
5550

5651
test('clearHistory - operations after clearHistory are undoable normally', async ({ page, me }) => {
57-
// Perform an operation in Diagram A then load Diagram B and clear history
52+
// Perform an operation in Diagram A, then switch to Diagram B and clear history
5853
await me.click('Child A')
5954
await page.keyboard.press('Delete')
6055
await expect(me.getByText('Child A')).toBeHidden()
6156

62-
await page.evaluate(data => {
57+
await page.evaluate((data: typeof diagramB) => {
6358
const mind = (window as any)['#map']
6459
mind.refresh(data)
6560
mind.clearHistory()
6661
}, diagramB)
6762

68-
// Now add a node to Diagram B
63+
await expect(me.getByText('Diagram B')).toBeVisible()
64+
65+
// Add a node to Diagram B
6966
await me.click('Child B')
7067
await page.keyboard.press('Tab')
7168
await page.keyboard.press('Enter')
@@ -74,32 +71,33 @@ test('clearHistory - operations after clearHistory are undoable normally', async
7471
// Undo the add — should work
7572
await page.keyboard.press('Control+z')
7673
await expect(me.getByText('New Node')).toBeHidden()
74+
await expect(me.getByText('Diagram B')).toBeVisible()
7775

78-
// Redo the add — should also work
79-
await page.keyboard.press('Control+y')
80-
await expect(me.getByText('New Node')).toBeVisible()
81-
82-
// One more undo should be a no-op (no history before the cleared point)
83-
await page.keyboard.press('Control+z') // undo add
84-
await page.keyboard.press('Control+z') // should be no-op, not reach Diagram A
76+
// Another undo should be a no-op — must not reach Diagram A
77+
await page.keyboard.press('Control+z')
8578
await expect(me.getByText('Diagram B')).toBeVisible()
8679
await expect(me.getByText('Diagram A')).toBeHidden()
80+
81+
// Redo restores the added node
82+
await page.keyboard.press('Control+y')
83+
await expect(me.getByText('New Node')).toBeVisible()
8784
})
8885

8986
test('clearHistory - first undo baseline is the refreshed diagram state', async ({ page, me }) => {
90-
// Load Diagram B, clear history, then add a node and undo
91-
await page.evaluate(data => {
87+
// Switch to Diagram B and clear history
88+
await page.evaluate((data: typeof diagramB) => {
9289
const mind = (window as any)['#map']
9390
mind.refresh(data)
9491
mind.clearHistory()
9592
}, diagramB)
9693

94+
// Add a node to Diagram B
9795
await me.click('Child B')
9896
await page.keyboard.press('Tab')
9997
await page.keyboard.press('Enter')
10098
await expect(me.getByText('New Node')).toBeVisible()
10199

102-
// Undo restores exactly to the state right after refresh (Diagram B with just Child B)
100+
// Undo should restore exactly to the post-refresh state of Diagram B
103101
await page.keyboard.press('Control+z')
104102
await expect(me.getByText('New Node')).toBeHidden()
105103
await expect(me.getByText('Child B')).toBeVisible()

0 commit comments

Comments
 (0)