@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
165 lines • 7.69 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const test_utils_1 = require("@sprucelabs/test-utils");
const AbstractSkillTest_1 = __importDefault(require("../../../tests/AbstractSkillTest"));
const test_utility_1 = __importDefault(require("../../../tests/utilities/test.utility"));
class CreatingAnAppControllerTest extends AbstractSkillTest_1.default {
static skillCacheKey = 'views';
static acPath;
static skillNamespace = (0, test_utils_1.generateId)().replace(/\d/g, '');
static action;
static lastExecution = [];
static async beforeAll() {
await super.beforeAll();
this.acPath = this.resolvePath('src/App.ac.ts');
const pgk = this.Service('pkg');
pgk.set({ path: 'skill.namespace', value: this.skillNamespace });
}
static async beforeEach() {
await super.beforeEach();
this.action = this.Action('view', 'createApp');
await this.cli.on('feature.will-execute', (payload) => {
this.lastExecution.push(payload);
});
}
static async generatesExpectedFile() {
const results = await this.execute();
test_utils_1.assert.isFalsy(results.errors);
test_utility_1.default.assertFileByPathInGeneratedFiles(this.acPath, results.files);
test_utils_1.assert.doesInclude(results.files, {
action: 'generated',
name: 'App.ac.ts',
path: this.acPath,
});
test_utils_1.assert.doesInclude(results.files, {
action: 'generated',
name: 'views.ts',
path: this.combinedViewsPath,
});
}
static async syncsViewsOnCreation() {
test_utils_1.assert.doesInclude(this.lastExecution, {
actionCode: 'sync',
featureCode: 'view',
});
test_utils_1.assert.isEqual(this.lastExecution[1].actionCode, 'sync');
test_utils_1.assert.isTrue(spruce_skill_utils_1.diskUtil.doesFileExist(this.combinedViewsPath), 'views.ts does not exist');
}
static async throwsIfAppAlreadyExists() {
await this.executeAndAssertThrowsAvcExists();
}
static async acIncludesExpectedContents() {
const contents = spruce_skill_utils_1.diskUtil.readFile(this.acPath);
test_utils_1.assert.doesInclude(contents, `export default class ${this.className} extends AbstractAppController`);
test_utils_1.assert.doesInclude(contents, 'public async load(_options: AppControllerLoadOptions)');
}
static async generatedFilesAreValid() {
await this.Service('typeChecker').check(this.acPath);
await this.assertCombinedViewsFileIsValid();
}
static async doesNotDropAppIntoSkillViewTypes() {
const expected = `interface ViewControllerMap {}`;
this.assertCombinedViewsContains(expected);
}
static async callsHeartwoodImportFunctionAsExpected() {
this.assertCombinedViewsContains(`heartwood({ vcs, pluginsByName, App: ${this.className} })`);
}
static async combineViewsTypesAsExpected() {
const expected = `interface AppControllerMap {
'${this.skillNamespace}' : ${this.className}
}`;
this.assertCombinedViewsContains(expected);
}
static async throwsEvenIfAppMoves() {
const destination = this.resolvePath(`src/${(0, test_utils_1.generateId)()}/App.ac.ts`);
spruce_skill_utils_1.diskUtil.moveFile(this.acPath, destination);
await this.executeAndAssertThrowsAvcExists();
this.acPath = destination;
}
static async canRenameAppAndItUpdatesCombinedViews() {
const contents = spruce_skill_utils_1.diskUtil.readFile(this.acPath);
const updatedContents = contents.replace(this.className, 'NewAppControllerImpl');
spruce_skill_utils_1.diskUtil.writeFile(this.acPath, updatedContents);
await this.Action('view', 'sync').execute({});
this.assertCombinedViewsContains('heartwood({ vcs, pluginsByName, App: NewAppControllerImpl })');
await this.assertCombinedViewsFileIsValid();
}
static async importsAppFromCombinedViews() {
const testScript = `import { App } from '#` +
`spruce/views/views'
const app = new App({} as any)
console.log(app)
if (App.id !== '${this.skillNamespace}') {
throw new Error('App.id is not correct')
}`;
const testScriptPath = this.resolvePath('src/test.ts');
spruce_skill_utils_1.diskUtil.writeFile(testScriptPath, testScript);
await this.Service('typeChecker').check(testScriptPath);
}
static async assertCombinedViewsFileIsValid() {
await this.Service('typeChecker').check(this.combinedViewsPath);
}
static assertCombinedViewsContains(expected) {
const contents = spruce_skill_utils_1.diskUtil.readFile(this.combinedViewsPath);
test_utils_1.assert.doesInclude(contents.replace(/\s/g, ''), expected.replace(/\s/g, ''), `The combined views file does not include the expected contents.`);
}
static get combinedViewsPath() {
return this.resolveHashSprucePath('views', 'views.ts');
}
static get className() {
return `${spruce_skill_utils_1.namesUtil.toPascal(this.skillNamespace)}AppController`;
}
static async executeAndAssertThrowsAvcExists() {
const results = await this.execute();
test_utils_1.assert.isTruthy(results.errors);
test_utils_1.errorAssert.assertError(results.errors[0], 'APP_CONTROLLER_ALREADY_EXISTS');
}
static async execute() {
return await this.action.execute();
}
}
exports.default = CreatingAnAppControllerTest;
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "generatesExpectedFile", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "syncsViewsOnCreation", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "throwsIfAppAlreadyExists", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "acIncludesExpectedContents", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "generatedFilesAreValid", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "doesNotDropAppIntoSkillViewTypes", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "callsHeartwoodImportFunctionAsExpected", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "combineViewsTypesAsExpected", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "throwsEvenIfAppMoves", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "canRenameAppAndItUpdatesCombinedViews", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAnAppControllerTest, "importsAppFromCombinedViews", null);
//# sourceMappingURL=CreatingAnAppController.test.js.map