@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
191 lines • 8.43 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"));
class CreatingAViewPluginTest extends AbstractSkillTest_1.default {
static skillCacheKey = 'views';
static action;
static async beforeEach() {
await super.beforeEach();
this.action = this.Action('view', 'createPlugin');
}
static async hasAction() {
test_utils_1.assert.isFunction(this.action.execute);
}
static async createsExpectedFile(readable, expected, combinedViewAction) {
const results = await this.execute({ nameReadable: readable });
this.assertExpectedFileCreated(results, expected, combinedViewAction);
test_utils_1.assert.isFalsy(results.errors, 'Errors were returned and none were expected!');
}
static async usesCamelNameIfProvided() {
const results = await this.execute({
nameReadable: 'test',
nameCamel: 'testCamel',
});
this.assertExpectedFileCreated(results, 'testCamel', 'updated');
}
static async actuallyCreatesFile() {
this.assertViewPluginWritten('test');
this.assertViewPluginWritten('another');
this.assertViewPluginWritten('aThird');
}
static async throwsIfPluginAlreadyExists() {
await this.assertThrowsAlreadyExists('test');
await this.assertThrowsAlreadyExists('another');
}
static async contentsAreEqualToExpected() {
this.assertPluginContentsEqualExpected('test', 'Test');
this.assertPluginContentsEqualExpected('another', 'Another');
this.assertPluginContentsEqualExpected('aThird', 'AThird');
}
static async combineViewsFileIsValid() {
const combined = this.getPathToCombinedViewsFile();
await this.Service('typeChecker').check(combined);
}
static async updatesViewCombinedFileWithTypesAsExpected() {
const expected = ` interface ViewControllerPlugins {
aThird: AThirdViewPlugin
another: AnotherViewPlugin
test: TestViewPlugin
testCamel: TestCamelViewPlugin
}`;
this.assertCombinedFileIncludes(expected);
}
static async updatesViewCombinedWithPluginsAsExpected() {
const expected = `export const pluginsByName = {
aThird: AThirdViewPlugin,
another: AnotherViewPlugin,
test: TestViewPlugin,
testCamel: TestCamelViewPlugin,
}
`;
this.assertCombinedFileIncludes(expected);
this.assertCombinedFileIncludes('heartwood({ vcs, pluginsByName })');
}
static async viewPluginCanImportAnotherViewPlugin() {
this.writeFile('export class ExternalViewPlugin {}', 'external.ts');
this.writeFile(`export { ExternalViewPlugin as default } from './external'`, 'actual.view.plugin.ts');
await this.syncViews();
const expected = `export const pluginsByName = {
actual: ExternalViewPlugin,
aThird: AThirdViewPlugin,
another: AnotherViewPlugin,
test: TestViewPlugin,
testCamel: TestCamelViewPlugin,
}
`;
this.assertCombinedFileIncludes(expected);
}
static async canImportPluginFromExternalLibrary() {
this.writeFile("export { CardViewController as default } from '@sprucelabs/heartwood-view-controllers'", 'card.view.plugin.ts');
await this.syncViews();
const expected = `export const pluginsByName = {
actual: ExternalViewPlugin,
card: CardViewController,
aThird: AThirdViewPlugin,
another: AnotherViewPlugin,
test: TestViewPlugin,
testCamel: TestCamelViewPlugin,
}
`;
this.assertCombinedFileIncludes(expected);
}
static async syncViews() {
await this.Action('view', 'sync').execute({});
}
static writeFile(content, fileName) {
const actualPlugiContent = content;
const actualDestination = this.resolvePath('src', fileName);
spruce_skill_utils_1.diskUtil.writeFile(actualDestination, actualPlugiContent);
}
static assertCombinedFileIncludes(expected) {
const combined = this.getPathToCombinedViewsFile();
const contents = spruce_skill_utils_1.diskUtil.readFile(combined);
test_utils_1.assert.doesInclude(contents, expected);
}
static assertPluginContentsEqualExpected(nameCamel, namePascal) {
const contents = spruce_skill_utils_1.diskUtil.readFile(this.buildPathToViewPlugin(nameCamel));
const expected = `import { ViewControllerPlugin } from '@sprucelabs/heartwood-view-controllers'\n\nexport default class ${namePascal}ViewPlugin implements ViewControllerPlugin {}`;
test_utils_1.assert.isEqual(contents.trim(), expected.trim());
}
static async assertThrowsAlreadyExists(name) {
const results = await this.execute({ nameReadable: name });
test_utils_1.assert.isTruthy(results.errors);
test_utils_1.errorAssert.assertError(results.errors[0], 'VIEW_PLUGIN_ALREADY_EXISTS', {
name,
});
}
static assertViewPluginWritten(name) {
const path = this.buildPathToViewPlugin(name);
test_utils_1.assert.isTrue(spruce_skill_utils_1.diskUtil.doesFileExist(path), `File ${path} was not written!`);
}
static buildPathToViewPlugin(name) {
return this.resolvePath('src', 'viewPlugins', `${name}.view.plugin.ts`);
}
static async execute(options) {
return await this.action.execute(options);
}
static assertExpectedFileCreated(results, expected, combinedViewAction) {
const expectedName = `${expected}.view.plugin.ts`;
test_utils_1.assert.doesInclude(results.files?.[0], {
action: 'generated',
name: expectedName,
path: this.resolvePath('src', 'viewPlugins', expectedName),
});
test_utils_1.assert.doesInclude(results.files?.[1], {
action: combinedViewAction,
name: 'views.ts',
path: this.getPathToCombinedViewsFile(),
});
}
static getPathToCombinedViewsFile() {
return this.resolveHashSprucePath('views', 'views.ts');
}
}
exports.default = CreatingAViewPluginTest;
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "hasAction", null);
__decorate([
(0, test_utils_1.test)('creates expected plugin file 1', 'test', 'test', 'updated'),
(0, test_utils_1.test)('creates expected plugin file 2', 'another', 'another', 'updated'),
(0, test_utils_1.test)('creates expected plugin file 3', 'a third', 'aThird', 'generated')
], CreatingAViewPluginTest, "createsExpectedFile", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "usesCamelNameIfProvided", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "actuallyCreatesFile", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "throwsIfPluginAlreadyExists", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "contentsAreEqualToExpected", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "combineViewsFileIsValid", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "updatesViewCombinedFileWithTypesAsExpected", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "updatesViewCombinedWithPluginsAsExpected", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "viewPluginCanImportAnotherViewPlugin", null);
__decorate([
(0, test_utils_1.test)()
], CreatingAViewPluginTest, "canImportPluginFromExternalLibrary", null);
//# sourceMappingURL=CreatingAViewPlugin.test.js.map