Files
azuredatastudio/extensions/sql-database-projects/src/test/mainController.test.ts
Benjin Dubishar 80901c9a7b Swapping vscode calls for ApiWrapper for testability (#10267)
* swapping vscode calls for apiwrapper for testability

* Adding mainController tests

* Adding unit tests for input validation

* Adding project controller tests, reorganizing error handling

* Removing commented-out code
2020-05-06 14:16:27 -07:00

69 lines
3.4 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
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 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 } from './testUtils';
let testContext: TestContext;
describe('MainController: main controller operations', function (): void {
before(async function (): Promise<void> {
testContext = createContext();
await templates.loadTemplates(path.join(__dirname, '..', '..', 'resources', 'templates'));
await baselines.loadBaselines();
});
beforeEach(async function (): Promise<void> {
testContext.apiWrapper.reset();
});
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);
});
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
const proj = await controller.createNewProject();
should(proj).not.equal(undefined);
});
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}'`);
}
});
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);
});
});