From dcc6ea6538ea88debd87c5091e0e175b8eebb51c Mon Sep 17 00:00:00 2001 From: Oskar Zaremba Date: Fri, 25 Feb 2022 09:51:50 +0100 Subject: [PATCH 1/2] feature: trial of conditional templates for possible usage (#109) --- .../support/index.d.ts.template | 21 ------------------- .../cypress/base/support/index.d.ts.template | 18 +++++++++++++++- 2 files changed, 17 insertions(+), 22 deletions(-) delete mode 100644 schematics/web/e2e/framework/files/cypress/authentication/support/index.d.ts.template diff --git a/schematics/web/e2e/framework/files/cypress/authentication/support/index.d.ts.template b/schematics/web/e2e/framework/files/cypress/authentication/support/index.d.ts.template deleted file mode 100644 index e6941e34..00000000 --- a/schematics/web/e2e/framework/files/cypress/authentication/support/index.d.ts.template +++ /dev/null @@ -1,21 +0,0 @@ -// Add all your custom global commands to the "Chainable" interface -namespace Cypress { - interface Chainable { - checkThatURLContains(specificPage: string); - fillSignUpForm(email: string, password: string); - fillSignInForm(email: string, password: string); - signOut(); - clickOn(selector: string); - typeText(selector: string, text: string); - clearElement(selector: string); - checkValidation(validationText: string); - getByDataTestId(selector: string, ...args: any[]); - checkIfTextIsShown(selector: string, text: string); - checkTheStateOfElement(selector: string, state: string); - checkIfPasswordIsVisible(selector: string, isVisible: boolean); - confirmUserSignUp(email: string); - createUserWithPassword(email: string); - addUserToSpecificGroup(email: string, group: string); - createAdminWithPassword(email: string); - } -} diff --git a/schematics/web/e2e/framework/files/cypress/base/support/index.d.ts.template b/schematics/web/e2e/framework/files/cypress/base/support/index.d.ts.template index e1f7a4b1..f7155312 100644 --- a/schematics/web/e2e/framework/files/cypress/base/support/index.d.ts.template +++ b/schematics/web/e2e/framework/files/cypress/base/support/index.d.ts.template @@ -1,6 +1,22 @@ // Add all your custom global commands to the "Chainable" interface namespace Cypress { interface Chainable { - checkThatURLContains(specificPage: string); + checkThatURLContains(specificPage: string);<%if(modules.includes('authentication')) {%> + fillSignUpForm(email: string, password: string); + fillSignInForm(email: string, password: string); + signOut(); + clickOn(selector: string); + typeText(selector: string, text: string); + clearElement(selector: string); + checkValidation(validationText: string); + getByDataTestId(selector: string, ...args: any[]); + checkIfTextIsShown(selector: string, text: string); + checkTheStateOfElement(selector: string, state: string); + checkIfPasswordIsVisible(selector: string, isVisible: boolean); + confirmUserSignUp(email: string); + createUserWithPassword(email: string); + addUserToSpecificGroup(email: string, group: string); + createAdminWithPassword(email: string); + <%}%> } } From fefd1423edad92f1c94a3348ace726aeb63c966e Mon Sep 17 00:00:00 2001 From: Oskar Zaremba Date: Fri, 25 Feb 2022 14:21:44 +0100 Subject: [PATCH 2/2] feature: trial of abstract syntax tree for possible usage (#109) --- schematics/utility/addAuthentication/index.ts | 24 +++++++++++ .../updateCommandsTs/change.ts | 40 +++++++++++++++++++ .../support/commands.ts.template | 5 --- schematics/web/e2e/framework/index.ts | 4 ++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 schematics/utility/addAuthentication/index.ts create mode 100644 schematics/utility/addAuthentication/updateCommandsTs/change.ts delete mode 100644 schematics/web/e2e/framework/files/cypress/authentication/support/commands.ts.template diff --git a/schematics/utility/addAuthentication/index.ts b/schematics/utility/addAuthentication/index.ts new file mode 100644 index 00000000..7876fc04 --- /dev/null +++ b/schematics/utility/addAuthentication/index.ts @@ -0,0 +1,24 @@ +import { Rule, Tree } from "@angular-devkit/schematics"; +import { InsertChange } from "@schematics/angular/utility/change"; +import { Schema } from "../../application/schema"; +import modifyCommandTs from "./updateCommandsTs/change"; + +function addAuthenticationRule(_options: Schema): Rule { + return (tree: Tree) => { + const changesInCommansTs = modifyCommandTs(tree); + const updateRecorder = tree.beginUpdate("web/cypress/support/commands.ts"); + + if (changesInCommansTs instanceof InsertChange) { + updateRecorder.insertLeft( + changesInCommansTs.pos, + changesInCommansTs.toAdd + ); + } + + tree.commitUpdate(updateRecorder); + + return tree; + }; +} + +export default addAuthenticationRule; diff --git a/schematics/utility/addAuthentication/updateCommandsTs/change.ts b/schematics/utility/addAuthentication/updateCommandsTs/change.ts new file mode 100644 index 00000000..576ceebf --- /dev/null +++ b/schematics/utility/addAuthentication/updateCommandsTs/change.ts @@ -0,0 +1,40 @@ +import { SchematicsException, Tree } from "@angular-devkit/schematics"; +import { getSourceNodes } from "@schematics/angular/utility/ast-utils"; +import { Change, InsertChange } from "@schematics/angular/utility/change"; +import ts from "typescript"; + +function modifyCommandTs(tree: Tree): Change { + const fileRawContent = tree.read("web/cypress/support/commands.ts"); + + if (!fileRawContent) throw new SchematicsException(`File does not exist.`); + + const fileFormattedContent = fileRawContent.toString("utf-8"); + const sourceFile = ts.createSourceFile( + "commands.ts", + fileFormattedContent, + ts.ScriptTarget.Latest, + true + ); + const nodes: ts.Node[] = getSourceNodes(sourceFile); + const insertionNode = nodes.find( + (node) => + node.kind === ts.SyntaxKind.ImportDeclaration && + node.getText().includes("./methods/base-commands") + ); + + if (!insertionNode) + throw new SchematicsException(`insertion node not retrieved!`); + + const contentToInsert = ` + import "./methods/auth-commands"; + import "./methods/aws-calls"; + `; + + return new InsertChange( + "web/cypress/support/commands.ts", + insertionNode.getEnd(), + contentToInsert + ); +} + +export default modifyCommandTs; diff --git a/schematics/web/e2e/framework/files/cypress/authentication/support/commands.ts.template b/schematics/web/e2e/framework/files/cypress/authentication/support/commands.ts.template deleted file mode 100644 index 6b16a352..00000000 --- a/schematics/web/e2e/framework/files/cypress/authentication/support/commands.ts.template +++ /dev/null @@ -1,5 +0,0 @@ -import "@cypress/code-coverage/support"; - -import "./methods/auth-commands"; -import "./methods/aws-calls"; -import "./methods/base-commands"; diff --git a/schematics/web/e2e/framework/index.ts b/schematics/web/e2e/framework/index.ts index 9688d870..f23ef39d 100644 --- a/schematics/web/e2e/framework/index.ts +++ b/schematics/web/e2e/framework/index.ts @@ -16,6 +16,7 @@ import { import { WEB_PACKAGE_JSON_PATH } from "@consts/index"; import { E2ERunner, Module } from "@enums/Module"; import { Schematic } from "@enums/Schematic"; +import addAuthenticationRule from "@janush-schematics/utility/addAuthentication"; import { stubArg } from "@janush-schematics/utility/stubArg/stubArg"; import { addPackageJsonDependency } from "@schematics/angular/utility/dependencies"; import { @@ -69,6 +70,9 @@ export const e2eFrameworkGenerator = (options: Schema): Rule => { MergeStrategy.Overwrite ) : stubArg, + options.modules.includes(Module.AUTHENTICATION) + ? addAuthenticationRule + : stubArg, packageJsonCypressExtender, schematic("apply-prettier", {}), ]);