UNPKG

@o3r/testing

Version:

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

97 lines 4.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngAddFixture = void 0; exports.ngAddFixtureFn = ngAddFixtureFn; const node_path_1 = require("node:path"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); const change_1 = require("@schematics/angular/utility/change"); const ts = require("typescript"); const checkFixture = (componentPath, tree, baseFileName) => { const files = [ node_path_1.posix.join((0, node_path_1.dirname)(componentPath), `${baseFileName}.fixture.ts`) ]; if (files.some((file) => tree.exists(file))) { throw new schematics_2.O3rCliError(`Unable to add fixture to this component because it already has at least one of these files: ${files.join(', ')}.`); } }; /** * Add fixture to an existing component * @param options */ function ngAddFixtureFn(options) { return async (tree, context) => { try { const baseFileName = (0, node_path_1.basename)(options.path, '.component.ts'); const { name } = (0, schematics_2.getO3rComponentInfoOrThrowIfNotFound)(tree, options.path); checkFixture(options.path, tree, baseFileName); const properties = { ...options, componentFixture: name.concat('Fixture'), name: baseFileName }; const createFixtureFilesRule = (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./templates'), [ (0, schematics_1.template)(properties), (0, schematics_1.renameTemplateFiles)(), (0, schematics_1.move)((0, node_path_1.dirname)(options.path)) ]), schematics_1.MergeStrategy.Overwrite); const specFilePath = options.specFilePath || node_path_1.posix.join((0, node_path_1.dirname)(options.path), `${baseFileName}.spec.ts`); const updateSpecRule = (0, schematics_1.chain)([ (0, schematics_2.addImportsRule)(specFilePath, [ { from: '@o3r/testing/core', importNames: ['O3rElement'] }, { from: `./${baseFileName}.fixture`, importNames: [`${properties.componentFixture}Component`] } ]), () => { if (!tree.exists(specFilePath)) { context.logger.warn(`No update applied on spec file because ${specFilePath} does not exist.`); return; } const specSourceFile = ts.createSourceFile(specFilePath, tree.readText(specFilePath), ts.ScriptTarget.ES2020, true); const recorder = tree.beginUpdate(specFilePath); const lastImport = [...specSourceFile.statements].reverse().find((statement) => ts.isImportDeclaration(statement)); const changes = [ new change_1.InsertChange(specFilePath, lastImport?.getEnd() || 0, `\nlet componentFixture: ${properties.componentFixture}Component;`) ]; (0, change_1.applyToUpdateRecorder)(recorder, changes); tree.commitUpdate(recorder); const newContent = tree.readText(specFilePath) .replaceAll(/((\s+)component = fixture.componentInstance;)/g, `$1\n$2componentFixture = new ${properties.componentFixture}Component(new O3rElement(fixture.debugElement));`) .replaceAll(/((\s+)expect\(component\).toBeTruthy\(\);)/g, '$1\n$2expect(componentFixture).toBeTruthy();'); tree.overwrite(specFilePath, newContent); return tree; } ]); return (0, schematics_1.chain)([ createFixtureFilesRule, updateSpecRule, options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)() ]); } catch (e) { if (e instanceof schematics_2.NoOtterComponent && context.interactive) { const shouldConvertComponent = await (0, schematics_2.askConfirmationToConvertComponent)(); if (shouldConvertComponent) { return (0, schematics_1.chain)([ (0, schematics_1.externalSchematic)('@o3r/core', 'convert-component', { path: options.path }), ngAddFixtureFn(options) ]); } } throw e; } }; } /** * Add fixture to an existing component * @param options */ exports.ngAddFixture = (0, schematics_2.createOtterSchematic)(ngAddFixtureFn); //# sourceMappingURL=index.js.map