UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

186 lines • 8.82 kB
"use strict"; 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 LintService_1 = __importDefault(require("../../../services/LintService")); const AbstractTestTest_1 = __importDefault(require("../../../tests/AbstractTestTest")); const test_utility_1 = __importDefault(require("../../../tests/utilities/test.utility")); const expectedAbstractTests = [ 'AbstractSpruceTest (default)', 'AbstractStoreTest (requires install)', 'AbstractViewControllerTest (requires install)', 'AbstractSpruceFixtureTest', 'AbstractTest', 'AbstractTest2', 'AbstractBananaTestDifferentThanFileName', ]; const featuresWithRegisteredTests = [ { featureCode: 'store', className: 'AbstractStoreTest' }, { featureCode: 'view', className: 'AbstractViewControllerTest' }, ]; class SelectingAnAbstractTestClassTest extends AbstractTestTest_1.default { static async asksForYouToSelectABaseClass() { LintService_1.default.enableLinting(); const { promise } = await this.installCopyTestFilesSelectLocalAbstractTest(); const results = await promise; test_utility_1.default.assertFileByNameInGeneratedFiles('CanBookAppointment.test.ts', results.files); await this.buildAndAssertTestFailsAsExpected(); } static async canSelectAbstractClassWhileSelectingSubDir() { LintService_1.default.enableLinting(); const testDir = this.resolvePath('src', '__tests__', 'behavioral', 'taco'); spruce_skill_utils_1.diskUtil.createDir(testDir); await this.installAndCopyTestFiles(); const { promise } = await this.invokeCreateActionAndWaitForInput(); await this.ui.sendInput({ path: 'taco' }); await this.waitForInput(); this.selectOptionBasedOnLabel('AbstractBananaTestDifferentThanFileName'); await promise; await this.buildAndAssertTestFailsAsExpected(); } static async canSelectAbstractTestBasedOnEventEmitter() { const cli = await this.installTests(); void cli.on('test.register-abstract-test-classes', async () => { return { abstractClasses: [ { name: 'TestClass', label: 'TestClass', import: '@sprucelabs/another-lib', }, { name: 'TestClass2', label: 'TestClass2', import: '@sprucelabs/another-lib', }, ], }; }); const { choices, promise } = await this.executeCreateUntilAbstractClassSelection(); test_utils_1.assert.doesInclude(choices, { label: 'TestClass', }); test_utils_1.assert.doesInclude(choices, { label: 'TestClass2', }); this.selectOptionBasedOnLabel('TestClass2'); const results = await promise; test_utils_1.assert.isFalsy(results.errors); const match = test_utility_1.default.assertFileByNameInGeneratedFiles('CanBookAppointment.test.ts', results.files); const contents = spruce_skill_utils_1.diskUtil.readFile(match); test_utils_1.assert.doesInclude(contents, "import { TestClass2 } from '@sprucelabs/another-lib'"); test_utils_1.assert.doesInclude(contents, 'extends TestClass2'); } static async installingAFeatureRemovesLabelFromName() { this.commandFaker.fakeInstall(); await this.installTests(); const testFeature = this.featureInstaller.getFeature('test'); for (const feat of featuresWithRegisteredTests) { await this.featureInstaller.install({ features: [ //@ts-ignore { code: feat.featureCode, }, ], }); const candidates = await testFeature.buildParentClassCandidates(); test_utils_1.assert.isTruthy(candidates.find(({ name }) => { return name === feat.className; })); } } static async selectingUninstalledTestInstallsTheFeature() { this.commandFaker.fakeInstall(); for (const feat of featuresWithRegisteredTests) { this.cwd = spruce_skill_utils_1.diskUtil.createRandomTempDir(); await this.installTests(); const { choices, promise } = await this.executeCreateUntilAbstractClassSelection(); const match = choices.find((c) => c.label.includes(feat.className)); test_utils_1.assert.isTruthy(match); let isInstalled = await this.featureInstaller.isInstalled(feat.featureCode); test_utils_1.assert.isFalse(isInstalled); await this.ui.sendInput(`${match?.value}`); await promise; isInstalled = await this.featureInstaller.isInstalled(feat.featureCode); test_utils_1.assert.isTrue(isInstalled); } } static relativePathsAddDotSlash() { const action = this.Action('test', 'create'); //@ts-ignore const actual = action.buildParentClassFromCandidate({ label: 'test', name: 'AbstractTest', path: this.resolvePath('test/AbstractTest'), isDefaultExport: false, }, this.resolvePath('test')); test_utils_1.assert.isEqual(actual.importPath, './AbstractTest'); } static async buildAndAssertTestFailsAsExpected() { await this.Service('build').build(); await test_utils_1.assert.doesThrowAsync(() => this.Service('command').execute('yarn test'), /false.*?does not equal.*?true/gis); } static async installCopyTestFilesSelectLocalAbstractTest() { await this.installAndCopyTestFiles(); const { choices, promise } = await this.executeCreateUntilAbstractClassSelection(); for (const expected of expectedAbstractTests) { test_utils_1.assert.doesInclude(choices, { label: expected }); } this.selectOptionBasedOnLabel('AbstractBananaTestDifferentThanFileName'); return { promise }; } static async installAndCopyTestFiles() { await this.installTests(); await this.copyTestFiles(); } static async copyTestFiles() { const source = this.resolveTestPath('abstract_tests'); const destination = this.resolvePath('src'); await spruce_skill_utils_1.diskUtil.copyDir(source, destination); } static async executeCreateUntilAbstractClassSelection() { const { promise } = await this.invokeCreateActionAndWaitForInput(); const last = this.ui.getLastInvocation(); const { choices } = last.options.options ?? {}; return { promise, choices }; } static async invokeCreateActionAndWaitForInput() { const promise = this.Action('test', 'create').execute({ type: 'behavioral', nameReadable: 'Can book appointment', nameCamel: 'canBookAppointment', }); await this.waitForInput(); return { promise }; } } exports.default = SelectingAnAbstractTestClassTest; __decorate([ (0, test_utils_1.test)() ], SelectingAnAbstractTestClassTest, "asksForYouToSelectABaseClass", null); __decorate([ (0, test_utils_1.test)() ], SelectingAnAbstractTestClassTest, "canSelectAbstractClassWhileSelectingSubDir", null); __decorate([ (0, test_utils_1.test)() ], SelectingAnAbstractTestClassTest, "canSelectAbstractTestBasedOnEventEmitter", null); __decorate([ (0, test_utils_1.test)() ], SelectingAnAbstractTestClassTest, "installingAFeatureRemovesLabelFromName", null); __decorate([ (0, test_utils_1.test)() ], SelectingAnAbstractTestClassTest, "selectingUninstalledTestInstallsTheFeature", null); __decorate([ (0, test_utils_1.test)() ], SelectingAnAbstractTestClassTest, "relativePathsAddDotSlash", null); //# sourceMappingURL=SelectingAnAbstractTestClass.test.js.map