UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

61 lines 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngAddFunctionsToFixture = void 0; const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); const ts = require("typescript"); const helpers_1 = require("./helpers"); const models_1 = require("./models"); /** * Generate fixture * @param options options to generate a fixture */ function ngAddFunctionsToFixtureFn(options) { return (0, schematics_1.chain)([ (tree) => { const { path, methods, selector } = options; if (!tree.exists(path)) { throw new schematics_2.O3rCliError(`File does not exist: ${path}`); } methods.forEach((methodType, index) => { const signature = (0, helpers_1.getSignature)(methodType, selector); const codeForInterface = `${models_1.description[methodType]}\n ${signature};\n`; const classPropSelector = `SELECTOR_${selector.toUpperCase().replace(/\./g, '').replace(/-/g, '_')}`; const codeForSelectorProp = ` protected readonly ${classPropSelector} = '${selector}';\n`; const codeForImplem = ` /** @inheritDoc */ public async ${signature} ${(0, helpers_1.getImplementation)(methodType, classPropSelector)}\n`; const sourceFile = ts.createSourceFile(path, tree.read(path).toString(), ts.ScriptTarget.ES2015, true); const recorder = tree.beginUpdate(path); sourceFile.forEachChild((node) => { if (ts.isInterfaceDeclaration(node)) { const methodSignatures = node.getChildren().filter((child) => ts.isMethodSignature(child)); const lastMethodSignature = methodSignatures.at(-1); const posForSignature = lastMethodSignature ? lastMethodSignature.end + 1 : node.end - 1; recorder.insertLeft(posForSignature, codeForInterface); } else if (ts.isClassDeclaration(node)) { if (index === 0) { const propDeclarations = node.members.filter((child) => ts.isPropertyDeclaration(child)); const lastPropDecl = propDeclarations.at(-1); const posForSelectorProp = lastPropDecl ? lastPropDecl.end + 1 : node.end - 1; recorder.insertLeft(posForSelectorProp, codeForSelectorProp); } const methodDeclarations = node.getChildren().filter((child) => ts.isMethodDeclaration(child)); const lastMethodDecl = methodDeclarations.at(-1); const posForImplem = lastMethodDecl ? lastMethodDecl.end + 1 : node.end - 1; recorder.insertLeft(posForImplem, codeForImplem); } }); tree.commitUpdate(recorder); }); }, options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)() ]); } /** * Generate fixture * @param options options to generate a fixture */ exports.ngAddFunctionsToFixture = (0, schematics_2.createOtterSchematic)(ngAddFunctionsToFixtureFn); //# sourceMappingURL=index.js.map