Skip to content

Commit 7ab49a1

Browse files
committed
check that Undo for latest backup restoration works
1 parent 10fd3e8 commit 7ab49a1

File tree

1 file changed

+66
-54
lines changed

1 file changed

+66
-54
lines changed

src/test/suite/restoreBackups.test.ts

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as assert from "assert";
2-
import { randomUUID } from "crypto";
32
import { afterEach } from "mocha";
43
import * as sinon from "sinon";
54
import * as vscode from "vscode";
@@ -15,9 +14,9 @@ suite("snippet.restoreBackups", () => {
1514
});
1615

1716
test("No backups initially", async () => {
18-
await getBackups(async (backups: BackupItem[]) => {
19-
assert.strictEqual(backups.length, 0);
20-
});
17+
const backups = await getBackups();
18+
19+
assert.strictEqual(backups.length, 0);
2120
});
2221

2322
test("Creates a backup after rename", async () => {
@@ -30,14 +29,13 @@ suite("snippet.restoreBackups", () => {
3029
await vscode.commands.executeCommand("snippet.renameSnippet", {
3130
id: snippet.data.id,
3231
});
32+
const backups = await getBackups();
3333

34-
await getBackups(async (backups: BackupItem[]) => {
35-
assert.strictEqual(backups.length, 1);
36-
assert.strictEqual(
37-
JSON.stringify(backups[0].item.elements),
38-
originalElementsJson
39-
);
40-
});
34+
assert.strictEqual(backups.length, 1);
35+
assert.strictEqual(
36+
JSON.stringify(backups[0].item.elements),
37+
originalElementsJson
38+
);
4139
});
4240

4341
test("Creates a backup after delete", async () => {
@@ -50,14 +48,13 @@ suite("snippet.restoreBackups", () => {
5048
await vscode.commands.executeCommand("snippet.deleteSnippet", {
5149
id: snippet.data.id,
5250
});
51+
const backups = await getBackups();
5352

54-
await getBackups(async (backups: BackupItem[]) => {
55-
assert.strictEqual(backups.length, 2);
56-
assert.strictEqual(
57-
JSON.stringify(backups[0].item.elements),
58-
originalElementsJson
59-
);
60-
});
53+
assert.strictEqual(backups.length, 2);
54+
assert.strictEqual(
55+
JSON.stringify(backups[0].item.elements),
56+
originalElementsJson
57+
);
6158
});
6259

6360
test("Creates a backup after save", async () => {
@@ -76,16 +73,14 @@ suite("snippet.restoreBackups", () => {
7673

7774
await vscode.commands.executeCommand("snippet.saveSnippet");
7875
sinon.restore();
76+
const backups = await getBackups();
77+
await closeAllEditors();
7978

80-
await getBackups(async (backups: BackupItem[]) => {
81-
await closeAllEditors();
82-
83-
assert.strictEqual(backups.length, 3);
84-
assert.strictEqual(
85-
JSON.stringify(backups[0].item.elements),
86-
originalElementsJson
87-
);
88-
});
79+
assert.strictEqual(backups.length, 3);
80+
assert.strictEqual(
81+
JSON.stringify(backups[0].item.elements),
82+
originalElementsJson
83+
);
8984
});
9085

9186
test("Creates a backup after move", async () => {
@@ -97,19 +92,18 @@ suite("snippet.restoreBackups", () => {
9792
elements[2].data.id,
9893
elements[1].data.id
9994
);
95+
const backups = await getBackups();
10096

101-
await getBackups(async (backups: BackupItem[]) => {
102-
assert.strictEqual(backups.length, 4);
103-
assert.strictEqual(
104-
JSON.stringify(backups[0].item.elements),
105-
originalElementsJson
106-
);
107-
});
97+
assert.strictEqual(backups.length, 4);
98+
assert.strictEqual(
99+
JSON.stringify(backups[0].item.elements),
100+
originalElementsJson
101+
);
108102
});
109103

110104
test("Restores backup", async () => {
111105
sinon.stub(vscode.window, "showInputBox").callsFake(() => {
112-
return Promise.resolve(randomUUID());
106+
return Promise.resolve("new name");
113107
});
114108
sinon.stub(vscode.window, "showInformationMessage").callsFake(() => {
115109
return Promise.resolve("Ok" as unknown as MessageItem);
@@ -120,19 +114,36 @@ suite("snippet.restoreBackups", () => {
120114
await vscode.commands.executeCommand("snippet.renameSnippet", {
121115
id: snippet.data.id,
122116
});
123-
124-
let backups: BackupItem[] = [];
125-
await getBackups(async (b: BackupItem[]) => {
126-
backups = b;
127-
}, true);
117+
const backups = await getBackups(true);
128118

129119
assert.strictEqual(backups.length, 5);
130120
assert.strictEqual(getElementsJson(), originalElementsJson);
131121
});
132122

123+
test("Undoes backup restoration", async () => {
124+
sinon.stub(vscode.window, "showInformationMessage").callsFake(() => {
125+
return Promise.resolve("Undo" as unknown as MessageItem);
126+
});
127+
sinon.stub(vscode.window, "showInputBox").callsFake(() => {
128+
return Promise.resolve("new name");
129+
});
130+
const originalElementsJson = getElementsJson();
131+
const snippet = getAnySnippet(originalElementsJson);
132+
133+
await vscode.commands.executeCommand("snippet.renameSnippet", {
134+
id: snippet.data.id,
135+
});
136+
const elementsAfterChange = getElementsJson();
137+
const backups = await getBackups(true);
138+
139+
assert.strictEqual(backups.length, 6);
140+
assert.notStrictEqual(getElementsJson(), originalElementsJson);
141+
assert.strictEqual(getElementsJson(), elementsAfterChange);
142+
});
143+
133144
test("Saves up to 10 backups", async () => {
134145
sinon.stub(vscode.window, "showInputBox").callsFake(() => {
135-
return Promise.resolve(randomUUID());
146+
return Promise.resolve("new name");
136147
});
137148
const originalElementsJson = getElementsJson();
138149
const snippet = getAnySnippet(originalElementsJson);
@@ -142,25 +153,26 @@ suite("snippet.restoreBackups", () => {
142153
id: snippet.data.id,
143154
});
144155
}
156+
const backups = await getBackups();
145157

146-
await getBackups(async (backups: BackupItem[]) => {
147-
assert.strictEqual(backups.length, 10);
148-
});
158+
assert.strictEqual(backups.length, 10);
149159
});
150160
});
151161

152-
async function getBackups(
153-
callback: (backups: BackupItem[]) => Promise<void>,
154-
restoreLatest = false
155-
): Promise<void> {
156-
const showQuickPickStub = sinon.stub(vscode.window, "showQuickPick");
162+
async function getBackups(restoreLatest = false): Promise<BackupItem[]> {
163+
return new Promise((resolve) => {
164+
const showQuickPickStub = sinon.stub(vscode.window, "showQuickPick");
157165

158-
showQuickPickStub.callsFake(async (backups: BackupItem[]) => {
159-
await callback(backups);
160-
return restoreLatest && backups[0] ? backups[0] : null;
161-
});
166+
let result: BackupItem[] = [];
167+
showQuickPickStub.callsFake(async (backups: BackupItem[]) => {
168+
result = backups;
169+
return restoreLatest && backups[0] ? backups[0] : null;
170+
});
162171

163-
await vscode.commands.executeCommand("snippet.restoreBackups");
172+
vscode.commands.executeCommand("snippet.restoreBackups").then(() => {
173+
resolve(result);
174+
});
175+
});
164176
}
165177

166178
function getElementsJson(): string {

0 commit comments

Comments
 (0)