Remove ApiWrapper from sql-database-projects (#11345)

* Remove ApiWrapper

* fix compile error

* Use .resolves

* Check error messages

* Check for not called

* FIx global beforeEach/afterEach
This commit is contained in:
Charles Gagnon
2020-07-15 14:02:33 -07:00
committed by GitHub
parent c7799168b0
commit f4a4127471
11 changed files with 753 additions and 867 deletions

View File

@@ -6,15 +6,14 @@
import * as should from 'should';
import * as path from 'path';
import * as os from 'os';
import * as TypeMoq from 'typemoq';
import * as vscode from 'vscode';
import * as sinon from 'sinon';
import * as baselines from './baselines/baselines';
import * as templates from '../templates/templates';
import * as constants from '../common/constants';
import { createContext, TestContext } from './testContext';
import MainController from '../controllers/mainController';
import { shouldThrowSpecificError, generateTestFolderPath, createTestProject } from './testUtils';
import { generateTestFolderPath, createTestProject } from './testUtils';
let testContext: TestContext;
@@ -25,22 +24,18 @@ describe('MainController: main controller operations', function (): void {
await baselines.loadBaselines();
});
beforeEach(function (): void {
testContext.apiWrapper.reset();
afterEach(function (): void {
sinon.restore();
});
it('Should create new project through MainController', async function (): Promise<void> {
const projFileDir = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
testContext.apiWrapper.setup(x => x.showInputBox(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('MyProjectName'));
testContext.apiWrapper.setup(x => x.showOpenDialog(TypeMoq.It.isAny())).returns(() => Promise.resolve([vscode.Uri.file(projFileDir)]));
testContext.apiWrapper.setup(x => x.workspaceFolders()).returns(() => undefined);
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => {
console.log(s);
return Promise.resolve(s);
});
sinon.stub(vscode.window, 'showInputBox').resolves('MyProjectName');
sinon.stub(vscode.window, 'showOpenDialog').resolves([vscode.Uri.file(projFileDir)]);
sinon.replaceGetter(vscode.workspace, 'workspaceFolders', () => undefined);
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
const controller = new MainController(testContext.context);
const proj = await controller.createNewProject();
should(proj).not.equal(undefined);
@@ -48,30 +43,33 @@ describe('MainController: main controller operations', function (): void {
it('Should show error when no project name', async function (): Promise<void> {
for (const name of ['', ' ', undefined]) {
testContext.apiWrapper.reset();
testContext.apiWrapper.setup(x => x.showInputBox(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(name));
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => { throw new Error(s); });
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
await shouldThrowSpecificError(async () => await controller.createNewProject(), constants.projectNameRequired, `case: '${name}'`);
const stub = sinon.stub(vscode.window, 'showInputBox').resolves(name);
const spy = sinon.spy(vscode.window, 'showErrorMessage');
const controller = new MainController(testContext.context);
await controller.createNewProject();
should(spy.calledOnce).be.true('showErrorMessage should have been called exactly once');
should(spy.calledWith(constants.projectNameRequired)).be.true(`showErrorMessage not called with expected message '${constants.projectNameRequired}' Actual '${spy.getCall(0).args[0]}'`);
stub.restore();
spy.restore();
}
});
it('Should show error when no location name', async function (): Promise<void> {
testContext.apiWrapper.setup(x => x.showInputBox(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('MyProjectName'));
testContext.apiWrapper.setup(x => x.showOpenDialog(TypeMoq.It.isAny())).returns(() => Promise.resolve(undefined));
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => { throw new Error(s); });
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
await shouldThrowSpecificError(async () => await controller.createNewProject(), constants.projectLocationRequired);
sinon.stub(vscode.window, 'showInputBox').resolves('MyProjectName');
sinon.stub(vscode.window, 'showOpenDialog').resolves(undefined);
const spy = sinon.spy(vscode.window, 'showErrorMessage');
const controller = new MainController(testContext.context);
await controller.createNewProject();
should(spy.calledOnce).be.true('showErrorMessage should be called exactly once');
should(spy.calledWith(constants.projectLocationRequired)).be.true(`showErrorMessage not called with expected message '${constants.projectLocationRequired}' Actual '${spy.getCall(0).args[0]}'`);
});
it('Should create new instance without error', async function (): Promise<void> {
should.doesNotThrow(() => new MainController(testContext.context, testContext.apiWrapper.object), 'Creating controller should not throw an error');
should.doesNotThrow(() => new MainController(testContext.context), 'Creating controller should not throw an error');
});
it('Should activate and deactivate without error', async function (): Promise<void> {
let controller = new MainController(testContext.context, testContext.apiWrapper.object);
let controller = new MainController(testContext.context);
should.notEqual(controller.extensionContext, undefined);
should.doesNotThrow(() => controller.activate(), 'activate() should not throw an error');
@@ -84,14 +82,14 @@ describe('MainController: main controller operations', function (): void {
const nestedFolder = path.join(rootFolderPath, 'nestedProject');
const nestedProject = await createTestProject(baselines.openProjectFileBaseline, nestedFolder);
testContext.apiWrapper.setup(x => x.workspaceFolders()).returns(() => [workspaceFolder]);
const workspaceFolder: vscode.WorkspaceFolder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
sinon.replaceGetter(vscode.workspace, 'workspaceFolders', () => [workspaceFolder]);
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
const controller = new MainController(testContext.context);
should(controller.projController.projects.length).equal(0);
await controller.loadProjectsInWorkspace();