Skip to content

Commit

Permalink
Refactoring: improving tests quality and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kbysiec committed Jul 16, 2021
1 parent ee56268 commit 7d344d3
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/test/test/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe("Workspace", () => {
it(`1: should registerAction method be invoked which register update
action if text document has changed and exists in workspace`, async () => {
const [registerActionStub] = setups.handleDidChangeTextDocument1();
const textDocumentChangeEvent = await getTextDocumentChangeEvent(true);
const textDocumentChangeEvent = getTextDocumentChangeEvent(true);
await workspaceAny.handleDidChangeTextDocument(textDocumentChangeEvent);

assert.equal(registerActionStub.calledOnce, true);
Expand All @@ -159,7 +159,7 @@ describe("Workspace", () => {

it(`2: should do nothing if text document does not exist in workspace`, async () => {
const [registerActionStub] = setups.handleDidChangeTextDocument2();
const textDocumentChangeEvent = await getTextDocumentChangeEvent(true);
const textDocumentChangeEvent = getTextDocumentChangeEvent(true);
await workspaceAny.handleDidChangeTextDocument(textDocumentChangeEvent);

assert.equal(registerActionStub.calledOnce, false);
Expand Down
5 changes: 2 additions & 3 deletions src/test/testSetup/actionProcessor.testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as vscode from "vscode";
import ActionProcessor from "../../actionProcessor";
import ActionType from "../../enum/actionType";
import Action from "../../interface/action";
import { getAction, getEventEmitter, getActions } from "../util/mockFactory";
import { getAction, getActions, getEventEmitter } from "../util/mockFactory";
import { restoreStubbedMultiple, stubMultiple } from "../util/stubHelpers";

export const getTestSetups = (actionProcessor: ActionProcessor) => {
Expand Down Expand Up @@ -125,7 +124,7 @@ export const getTestSetups = (actionProcessor: ActionProcessor) => {
"test action 3",
3,
true,
vscode.Uri.file("./#")
vscode.Uri.file("./test3/#")
),
];

Expand Down
44 changes: 3 additions & 41 deletions src/test/testSetup/dataService.testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from "vscode";
import DataService from "../../dataService";
import ExcludeMode from "../../enum/excludeMode";
import ItemsFilter from "../../interface/itemsFilter";
import {
getDocumentSymbolItemSingleLineArray,
Expand All @@ -12,8 +11,6 @@ import { restoreStubbedMultiple, stubMultiple } from "../util/stubHelpers";
export const getTestSetups = (dataService: DataService) => {
const dataServiceAny = dataService as any;

// dataServiceAny.patternProvider = getPatternProviderStub();

const stubConfig = (
itemsFilter: ItemsFilter = {},
isCancelled: boolean = false
Expand All @@ -33,15 +30,8 @@ export const getTestSetups = (dataService: DataService) => {
},
{
object: dataServiceAny.patternProvider,
method: "excludeMode",
returns: ExcludeMode.SearchEverywhere,
isNotMethod: true,
},
{
object: dataServiceAny.patternProvider,
method: "extensionExcludePatterns",
returns: ["**/.history/**", "**/.vscode/**"],
isNotMethod: true,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
};
Expand Down Expand Up @@ -119,40 +109,12 @@ export const getTestSetups = (dataService: DataService) => {
{
object: vscode.workspace,
method: "findFiles",
customReturns: true,
returns: [
{
onCall: 0,
returns: Promise.resolve([]),
},
{
onCall: 1,
throws: "wwwww",
},
],
throws: "test error",
},
{
object: vscode.commands,
method: "executeCommand",
},
{
object: dataServiceAny.patternProvider,
method: "extensionExcludePatterns",
returns: [],
isNotMethod: true,
},
{
object: dataServiceAny.patternProvider,
method: "fallbackExcludePatterns",
returns: [".vscode", ".history"],
isNotMethod: true,
},
{
object: dataServiceAny.patternProvider,
method: "excludeMode",
returns: ExcludeMode.SearchEverywhere,
isNotMethod: true,
},
]);
},
fetchData5: () => {
Expand Down
12 changes: 2 additions & 10 deletions src/test/testSetup/extensionController.testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ExcludeMode from "../../enum/excludeMode";
import ExtensionController from "../../extensionController";
import { getQpItems } from "../util/qpItemMockFactory";
import { getQuickPickStub } from "../util/stubFactory";
Expand Down Expand Up @@ -104,15 +103,8 @@ export const getTestSetups = (extensionController: ExtensionController) => {
},
{
object: extensionControllerAny.workspace.dataService.patternProvider,
method: "excludeMode",
returns: ExcludeMode.SearchEverywhere,
isNotMethod: true,
},
{
object: extensionControllerAny.workspace.dataService.patternProvider,
method: "extensionExcludePatterns",
returns: ["**/.history/**", "**/.vscode/**"],
isNotMethod: true,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
},
Expand Down
7 changes: 0 additions & 7 deletions src/test/testSetup/workspace.testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,6 @@ export const getTestSetups = (workspace: Workspace) => {
]);
},
handleDidChangeTextDocument1: () => {
restoreStubbedMultiple([
{
object: vscode.workspace,
method: "openTextDocument",
},
]);

return stubMultiple([
{
object: workspaceAny.common,
Expand Down
55 changes: 54 additions & 1 deletion src/test/testSetup/workspaceCommon.testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as vscode from "vscode";
import WorkspaceCommon from "../../workspaceCommon";
import { getDirectory, getItem, getItems } from "../util/itemMockFactory";
import { getWorkspaceData } from "../util/mockFactory";
import { getQpItems } from "../util/qpItemMockFactory";
import { restoreStubbedMultiple, stubMultiple } from "../util/stubHelpers";

Expand Down Expand Up @@ -31,6 +30,10 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
object: workspaceCommonAny.utils,
method: "getNameFromUri",
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
},
]);

return stubMultiple([
Expand All @@ -46,6 +49,11 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
returns: getItem(),
isNotMethod: true,
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
},
wasDirectoryRenamed2: () => {
Expand All @@ -54,6 +62,10 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
object: workspaceCommonAny.utils,
method: "getNameFromUri",
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
},
]);

return stubMultiple([
Expand All @@ -69,6 +81,11 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
returns: getItem(),
isNotMethod: true,
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
},
wasDirectoryRenamed3: () => {
Expand All @@ -77,6 +94,10 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
object: workspaceCommonAny.utils,
method: "getNameFromUri",
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
},
]);

return stubMultiple([
Expand All @@ -92,6 +113,11 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
returns: null,
isNotMethod: true,
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
},
wasDirectoryRenamed4: () => {
Expand All @@ -100,6 +126,10 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
object: workspaceCommonAny.utils,
method: "getNameFromUri",
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
},
]);

return stubMultiple([
Expand All @@ -115,6 +145,11 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
returns: undefined,
isNotMethod: true,
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
},
index1: () => {
Expand All @@ -123,13 +158,22 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
object: workspaceCommonAny.actionProcessor,
method: "register",
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
},
]);

return stubMultiple([
{
object: workspaceCommonAny.actionProcessor,
method: "register",
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
},
indexWithProgress1: () => {
Expand All @@ -138,6 +182,10 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
object: workspaceCommonAny.utils,
method: "hasWorkspaceAnyFolder",
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
},
]);

return stubMultiple([
Expand All @@ -150,6 +198,11 @@ export const getTestSetups = (workspaceCommon: WorkspaceCommon) => {
method: "hasWorkspaceAnyFolder",
returns: true,
},
{
object: workspaceCommonAny.dataService.patternProvider,
method: "getExcludePatterns",
returns: Promise.resolve(["**/.history/**", "**/.vscode/**"]),
},
]);
},
indexWithProgress2: () => {
Expand Down
9 changes: 4 additions & 5 deletions src/test/util/eventMockFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as sinon from "sinon";
import * as vscode from "vscode";
import ExcludeMode from "../../enum/excludeMode";
import { getUntitledItem } from "./itemMockFactory";
import { getTextDocumentStub } from "./stubFactory";

export const getWorkspaceFoldersChangeEvent = (flag: boolean) => {
return flag
Expand Down Expand Up @@ -47,12 +47,11 @@ export const getConfigurationChangeEvent = (
};
};

export const getTextDocumentChangeEvent = async (
export const getTextDocumentChangeEvent = (
shouldContentBeChanged: boolean = false
): Promise<vscode.TextDocumentChangeEvent> => {
const itemUntitled = getUntitledItem();
): vscode.TextDocumentChangeEvent => {
const textDocumentChangeEvent = {
document: await vscode.workspace.openTextDocument(itemUntitled),
document: getTextDocumentStub(),
contentChanges: [],
};
shouldContentBeChanged &&
Expand Down
1 change: 1 addition & 0 deletions src/test/util/stubFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function getDataServiceStub(): DataService {
const dataServiceStub: any = createStubInstance(DataService);
dataServiceStub.utils = getUtilsStub();
dataServiceStub.config = getConfigStub();
dataServiceStub.patternProvider = getPatternProviderStub();

return dataServiceStub as DataService;
}
Expand Down

0 comments on commit 7d344d3

Please sign in to comment.