UNPKG

@11joselu/cypress-to-playwright

Version:
86 lines 4.68 kB
import ts from 'typescript'; import { isCy } from '../is/is-cy.js'; import { fixString } from './fix-string.js'; export function handle(call, factory) { const propertyExpression = call.expression; const callArgs = call.arguments.map((arg) => fixString(arg.getText())); const { propertyTypeAccessArguments, propertyAccessArguments } = getArgumentsOfPropertyAccessExpression(propertyExpression); const cyCommandName = getExpressionName(propertyExpression.expression); let newExpression = propertyExpression.expression; if (isCy.get(cyCommandName) || isCy.isFirst(cyCommandName) || isCy.isLast(cyCommandName)) { if (isCy.isFirst(cyCommandName) || isCy.isLast(cyCommandName)) { const foundExpression = findGetPropertyExpression(propertyExpression); newExpression = factory.playwrightLocatorProperty(isCy.isFirst(cyCommandName) ? "first" /* LOCATOR_PROPERTIES.FIRST */ : "last" /* LOCATOR_PROPERTIES.LAST */, foundExpression.typeArguments, foundExpression.arguments); } else { newExpression = factory.callExpression(factory.playwrightCommand("locator" /* COMMANDS.LOCATOR */), propertyTypeAccessArguments, propertyAccessArguments); } } let shouldCyValidation = callArgs[0]; const isNegativeValidation = isCy.validation.isNegativeValidation(shouldCyValidation); if (isNegativeValidation) shouldCyValidation = shouldCyValidation.replace('not.', ''); if (isCy.validation.haveLength(shouldCyValidation)) { return factory.expect(newExpression, "toHaveCount" /* VALIDATION.TO_HAVE_COUNT */, [factory.numeric(callArgs[1])], isNegativeValidation); } if (isCy.validation.toHaveText(shouldCyValidation)) { return factory.expect(newExpression, "toHaveText" /* VALIDATION.TO_HAVE_TEXT */, [call.arguments[1]], isNegativeValidation); } if (isCy.validation.toHaveClass(shouldCyValidation)) { return factory.expect(newExpression, "toHaveClass" /* VALIDATION.TO_HAVE_CLASS */, [call.arguments[1]], isNegativeValidation); } if (isCy.validation.beVisible(shouldCyValidation)) { return factory.expect(newExpression, "toBeVisible" /* VALIDATION.TO_BE_VISIBLE */, [], isNegativeValidation); } if (isCy.validation.toHaveValue(shouldCyValidation)) { return factory.expect(newExpression, "toHaveValue" /* VALIDATION.TO_HAVE_VALUE */, [call.arguments[1]], isNegativeValidation); } if (isCy.validation.toContain(shouldCyValidation)) { return factory.expect(newExpression, "toContainText" /* VALIDATION.TO_CONTAIN_TEXT */, [call.arguments[1]], isNegativeValidation); } if (isCy.validation.beChecked(shouldCyValidation)) { return factory.expect(newExpression, "toBeChecked" /* VALIDATION.BE_CHECKED */, [], isNegativeValidation); } if (isCy.validation.beDisabled(shouldCyValidation)) { return factory.expect(newExpression, "toBeDisabled" /* VALIDATION.BE_DISABLED */, [], isNegativeValidation); } if (isCy.validation.haveAttr(shouldCyValidation)) { return factory.expect(newExpression, "toHaveAttribute" /* VALIDATION.TO_HAVE_ATTR */, callArgs.filter((_, index) => index).map((arg) => factory.string(arg)), isNegativeValidation); } return undefined; } function getArgumentsOfPropertyAccessExpression(propertyAccessExpression) { const callExpression = propertyAccessExpression.expression; const propertyTypeAccessArguments = ts.isCallExpression(callExpression) ? callExpression.typeArguments : undefined; const propertyAccessArguments = ts.isCallExpression(callExpression) ? callExpression.arguments : []; return { propertyTypeAccessArguments, propertyAccessArguments }; } function getExpressionName(expressions) { return getListOfExpressionName(expressions).reverse().join('.'); } function findGetPropertyExpression(propertyExpression) { const expressionName = getExpressionName(propertyExpression); if (isCy.get(expressionName)) { return propertyExpression.parent; } if (ts.isCallExpression(propertyExpression.expression)) { return findGetPropertyExpression(propertyExpression.expression.expression); } return propertyExpression.parent; } function getListOfExpressionName(expression) { const result = []; if ('name' in expression) { result.push(expression.name.escapedText.toString()); } if ('escapedText' in expression) { result.push(expression.escapedText); } if ('expression' in expression) { result.push(...getListOfExpressionName(expression.expression)); } return result; } //# sourceMappingURL=validations.js.map