Skip to content

Commit

Permalink
Merge pull request #4552 from Yokozuna59/convert-cypress-util-to-ts
Browse files Browse the repository at this point in the history
convert `cypress/helpers/util.js` to ts
  • Loading branch information
sidharthv96 committed Jul 19, 2023
2 parents e66135b + 8cd8714 commit 477bb43
Show file tree
Hide file tree
Showing 34 changed files with 190 additions and 149 deletions.
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Expand Up @@ -45,6 +45,7 @@ jobs:
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
VITEST_COVERAGE: true
CYPRESS_COMMIT: ${{ github.sha }}
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v3
# Run step only pushes to develop and pull_requests
Expand Down
93 changes: 0 additions & 93 deletions cypress/helpers/util.js

This file was deleted.

132 changes: 132 additions & 0 deletions cypress/helpers/util.ts
@@ -0,0 +1,132 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Buffer } from 'buffer';
import type { MermaidConfig } from '../../packages/mermaid/src/config.type.js';

interface CypressConfig {
listUrl?: boolean;
listId?: string;
name?: string;
}
type CypressMermaidConfig = MermaidConfig & CypressConfig;

interface CodeObject {
code: string;
mermaid: CypressMermaidConfig;
}

const utf8ToB64 = (str: string): string => {
return Buffer.from(decodeURIComponent(encodeURIComponent(str))).toString('base64');
};

const batchId: string = 'mermaid-batch-' + Cypress.env('CYPRESS_COMMIT') || Date.now().toString();

export const mermaidUrl = (
graphStr: string,
options: CypressMermaidConfig,
api: boolean
): string => {
const codeObject: CodeObject = {
code: graphStr,
mermaid: options,
};
const objStr: string = JSON.stringify(codeObject);
let url = `http://localhost:9000/e2e.html?graph=${utf8ToB64(objStr)}`;
if (api) {
url = `http://localhost:9000/xss.html?graph=${graphStr}`;
}

if (options.listUrl) {
cy.log(options.listId, ' ', url);
}

return url;
};

export const imgSnapshotTest = (
graphStr: string,
_options: CypressMermaidConfig = {},
api = false,
validation?: any
): void => {
cy.log(JSON.stringify(_options));
const options: CypressMermaidConfig = Object.assign(_options);
if (!options.fontFamily) {
options.fontFamily = 'courier';
}
if (!options.sequence) {
options.sequence = {};
}
if (!options.sequence || (options.sequence && !options.sequence.actorFontFamily)) {
options.sequence.actorFontFamily = 'courier';
}
if (options.sequence && !options.sequence.noteFontFamily) {
options.sequence.noteFontFamily = 'courier';
}
options.sequence.actorFontFamily = 'courier';
options.sequence.noteFontFamily = 'courier';
options.sequence.messageFontFamily = 'courier';
if (options.sequence && !options.sequence.actorFontFamily) {
options.sequence.actorFontFamily = 'courier';
}
if (!options.fontSize) {
options.fontSize = 16;
}

const url: string = mermaidUrl(graphStr, options, api);
openURLAndVerifyRendering(url, options, validation);
};

export const urlSnapshotTest = (
url: string,
_options: CypressMermaidConfig,
_api = false,
validation?: any
): void => {
const options: CypressMermaidConfig = Object.assign(_options);
openURLAndVerifyRendering(url, options, validation);
};

export const renderGraph = (
graphStr: string,
options: CypressMermaidConfig = {},
api = false
): void => {
const url: string = mermaidUrl(graphStr, options, api);
openURLAndVerifyRendering(url, options);
};

export const openURLAndVerifyRendering = (
url: string,
options: CypressMermaidConfig,
validation?: any
): void => {
const useAppli: boolean = Cypress.env('useAppli');
const name: string = (options.name || cy.state('runnable').fullTitle()).replace(/\s+/g, '-');

if (useAppli) {
cy.log(`Opening eyes ${Cypress.spec.name} --- ${name}`);
cy.eyesOpen({
appName: 'Mermaid',
testName: name,
batchName: Cypress.spec.name,
batchId: batchId + Cypress.spec.name,
});
}

cy.visit(url);
cy.window().should('have.property', 'rendered', true);
cy.get('svg').should('be.visible');

if (validation) {
cy.get('svg').should(validation);
}

if (useAppli) {
cy.log(`Check eyes ${Cypress.spec.name}`);
cy.eyesCheckWindow('Click!');
cy.log(`Closing eyes ${Cypress.spec.name}`);
cy.eyesClose();
} else {
cy.matchImageSnapshot(name);
}
};
2 changes: 1 addition & 1 deletion cypress/integration/other/configuration.spec.js
@@ -1,4 +1,4 @@
import { renderGraph } from '../../helpers/util.js';
import { renderGraph } from '../../helpers/util.ts';
describe('Configuration', () => {
describe('arrowMarkerAbsolute', () => {
it('should handle default value false of arrowMarkerAbsolute', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/other/external-diagrams.spec.js
@@ -1,4 +1,4 @@
import { urlSnapshotTest } from '../../helpers/util.js';
import { urlSnapshotTest } from '../../helpers/util.ts';

describe('mermaid', () => {
describe('registerDiagram', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/other/ghsa.spec.js
@@ -1,4 +1,4 @@
import { urlSnapshotTest, openURLAndVerifyRendering } from '../../helpers/util.js';
import { urlSnapshotTest, openURLAndVerifyRendering } from '../../helpers/util.ts';

describe('CSS injections', () => {
it('should not allow CSS injections outside of the diagram', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/other/xss.spec.js
@@ -1,4 +1,4 @@
import { mermaidUrl } from '../../helpers/util.js';
import { mermaidUrl } from '../../helpers/util.ts';
describe('XSS', () => {
it('should handle xss in tags', () => {
const str =
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/appli.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest } from '../../helpers/util.js';
import { imgSnapshotTest } from '../../helpers/util.ts';

describe('Git Graph diagram', () => {
it('1: should render a simple gitgraph with commit on main branch', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/c4.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest, renderGraph } from '../../helpers/util.js';
import { imgSnapshotTest, renderGraph } from '../../helpers/util.ts';

describe('C4 diagram', () => {
it('should render a simple C4Context diagram', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/classDiagram-v2.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest } from '../../helpers/util.js';
import { imgSnapshotTest } from '../../helpers/util.ts';
describe('Class diagram V2', () => {
it('0: should render a simple class diagram', () => {
imgSnapshotTest(
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/classDiagram.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest, renderGraph } from '../../helpers/util.js';
import { imgSnapshotTest, renderGraph } from '../../helpers/util.ts';

describe('Class diagram', () => {
it('1: should render a simple class diagram', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/conf-and-directives.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest } from '../../helpers/util.js';
import { imgSnapshotTest } from '../../helpers/util.ts';

describe('Configuration and directives - nodes should be light blue', () => {
it('No config - use default', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/current.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest } from '../../helpers/util.js';
import { imgSnapshotTest } from '../../helpers/util.ts';

describe('Current diagram', () => {
it('should render a state with states in it', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/debug.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest } from '../../helpers/util.js';
import { imgSnapshotTest } from '../../helpers/util.ts';

describe('Flowchart', () => {
it('34: testing the label width in percy', () => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/rendering/erDiagram.spec.js
@@ -1,4 +1,4 @@
import { imgSnapshotTest, renderGraph } from '../../helpers/util.js';
import { imgSnapshotTest, renderGraph } from '../../helpers/util.ts';

describe('Entity Relationship Diagram', () => {
it('should render a simple ER diagram', () => {
Expand Down

0 comments on commit 477bb43

Please sign in to comment.