Skip to content

Commit 93643b7

Browse files
committed
chore: add mock of vscode, draftly migrate test case of creating interface
1 parent 7c8e15a commit 93643b7

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

__mocks__/vscode.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
const path = require("path");
5+
6+
// Class
7+
const Uri = {
8+
fsPath: "defaultPath",
9+
file: jest.fn()
10+
};
11+
12+
const Diagnostic = jest.fn((range, message, severity) => {
13+
return {
14+
range,
15+
message,
16+
severity
17+
};
18+
});
19+
20+
const Range = jest.fn();
21+
22+
// Interface
23+
const OutputChannel = {
24+
appendLine: jest.fn(),
25+
show: jest.fn(),
26+
dispose: jest.fn()
27+
};
28+
29+
const ExtensionContext = {
30+
asAbsolutePath: jest.fn(p => {
31+
const rootPath = path.resolve(__dirname, "..");
32+
return path.join(rootPath, p);
33+
})
34+
};
35+
36+
const WorkspaceFolder = {
37+
uri: Uri
38+
};
39+
40+
const TextDocument = {
41+
uri: Uri,
42+
getText: jest.fn(),
43+
positionAt: jest.fn()
44+
};
45+
46+
const DiagnosticCollection = {
47+
set: jest.fn(),
48+
delete: jest.fn()
49+
};
50+
51+
const QuickPickOptions = {};
52+
53+
const QuickPickItem = {};
54+
55+
const OpenDialogOptions = {};
56+
57+
const InputBoxOptions = {};
58+
59+
// Enum
60+
const ConfigurationTarget = {
61+
Global: 1,
62+
workspace: 2,
63+
WorkspaceFolder: 3
64+
};
65+
66+
const DiagnosticSeverity = {
67+
Error: 0,
68+
Warning: 1,
69+
Information: 2,
70+
Hint: 3
71+
};
72+
73+
// Namespace
74+
const window = {
75+
createOutputChannel: jest.fn(() => OutputChannel),
76+
showQuickPick: jest.fn(items => items[0]),
77+
showOpenDialog: jest.fn(),
78+
showInputBox: jest.fn(),
79+
showTextDocument: jest.fn(),
80+
showInformationMessage: jest.fn(),
81+
showWarningMessage: jest.fn(),
82+
showErrorMessage: jest.fn()
83+
};
84+
85+
const commands = {
86+
executeCommand: jest.fn()
87+
};
88+
89+
const workspace = {
90+
workspaceFolders: undefined
91+
};
92+
93+
const vscode = {
94+
// Class
95+
Uri,
96+
Diagnostic,
97+
Range,
98+
99+
// Interface
100+
OutputChannel,
101+
ExtensionContext,
102+
WorkspaceFolder,
103+
QuickPickOptions,
104+
QuickPickItem,
105+
OpenDialogOptions,
106+
InputBoxOptions,
107+
TextDocument,
108+
DiagnosticCollection,
109+
110+
// Enum
111+
ConfigurationTarget,
112+
DiagnosticSeverity,
113+
114+
// Namespace
115+
window,
116+
commands,
117+
workspace
118+
};
119+
120+
module.exports = vscode;

src/test/deviceModelManager.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import * as path from "path";
5+
import { ColorizedChannel } from "../common/colorizedChannel";
6+
import { Constants } from "../common/constants";
7+
import { ProcessError } from "../common/processError";
8+
import { Utility } from "../common/utility";
9+
import { DeviceModelManager, ModelType } from "../deviceModel/deviceModelManager";
10+
import { UI } from "../view/ui";
11+
12+
const vscode = require("../../__mocks__/vscode");
13+
14+
jest.mock("../common/colorizedChannel");
15+
jest.mock("../common/utility");
16+
jest.mock("../view/ui");
17+
18+
describe("Device model manager", () => {
19+
const folder = "root";
20+
const context = vscode.ExtensionContext;
21+
const channel = new ColorizedChannel(Constants.CHANNEL_NAME);
22+
const manager = new DeviceModelManager(context, channel);
23+
24+
UI.selectRootFolder = jest.fn().mockResolvedValue(folder);
25+
UI.inputModelName = jest.fn().mockResolvedValue("test");
26+
27+
test("create interface successfully", async () => {
28+
await manager.createModel(ModelType.Interface);
29+
expect(UI.openAndShowTextDocument).toHaveBeenCalledWith(path.join(folder, "test.interface.json"));
30+
});
31+
32+
test("create interface with error", async () => {
33+
Utility.createFileFromTemplate = jest.fn().mockRejectedValueOnce(new Error());
34+
await expect(manager.createModel(ModelType.Interface)).rejects.toThrow(ProcessError);
35+
});
36+
});

0 commit comments

Comments
 (0)