convert cypress/helpers/util.js into ts

- add types for parameter and returned value and variables
- change the deperated `unescape` into `decodeURIComponent`
- create `CypressConfig` and `CypressMermaidConfig` and `CodeObject` types
- add default value for some parameter
This commit is contained in:
Yokozuna59
2023-06-27 20:53:01 +03:00
parent 6cbd24f704
commit a2cf41c9c0

View File

@@ -1,18 +1,36 @@
const utf8ToB64 = (str) => { import type { MermaidConfig } from '../../packages/mermaid/src/config.type.js';
return window.btoa(unescape(encodeURIComponent(str)));
type CypressConfig = {
listUrl?: boolean;
listId?: string;
name?: string;
};
type CypressMermaidConfig = MermaidConfig & CypressConfig;
interface CodeObject {
code: string;
mermaid: CypressMermaidConfig;
}
const utf8ToB64 = (str: string): string => {
return window.btoa(decodeURIComponent(encodeURIComponent(str)));
}; };
const batchId = 'mermaid-batch' + new Date().getTime(); const batchId: string = 'mermaid-batch' + new Date().getTime();
export const mermaidUrl = (graphStr, options, api) => { export const mermaidUrl = (
const obj = { graphStr: string,
options: CypressMermaidConfig,
api: boolean
): string => {
const codeObject: CodeObject = {
code: graphStr, code: graphStr,
mermaid: options, mermaid: options,
}; };
const objStr = JSON.stringify(obj); const objStr: string = JSON.stringify(codeObject);
let url = 'http://localhost:9000/e2e.html?graph=' + utf8ToB64(objStr); let url: string = `http://localhost:9000/e2e.html?graph=${utf8ToB64(objStr)}`;
if (api) { if (api) {
url = 'http://localhost:9000/xss.html?graph=' + graphStr; url = `http://localhost:9000/xss.html?graph=${graphStr}`;
} }
if (options.listUrl) { if (options.listUrl) {
@@ -22,9 +40,14 @@ export const mermaidUrl = (graphStr, options, api) => {
return url; return url;
}; };
export const imgSnapshotTest = (graphStr, _options = {}, api = false, validation = undefined) => { export const imgSnapshotTest = (
cy.log(_options); graphStr: string,
const options = Object.assign(_options); _options: CypressMermaidConfig = {},
api: boolean = false,
validation?: any
): void => {
cy.log(JSON.stringify(_options));
const options: CypressMermaidConfig = Object.assign(_options);
if (!options.fontFamily) { if (!options.fontFamily) {
options.fontFamily = 'courier'; options.fontFamily = 'courier';
} }
@@ -44,28 +67,42 @@ export const imgSnapshotTest = (graphStr, _options = {}, api = false, validation
options.sequence.actorFontFamily = 'courier'; options.sequence.actorFontFamily = 'courier';
} }
if (!options.fontSize) { if (!options.fontSize) {
options.fontSize = '16px'; options.fontSize = 16;
} }
const url = mermaidUrl(graphStr, options, api);
const url: string = mermaidUrl(graphStr, options, api);
openURLAndVerifyRendering(url, options, validation); openURLAndVerifyRendering(url, options, validation);
}; };
export const urlSnapshotTest = (url, _options, api = false, validation) => { export const urlSnapshotTest = (
const options = Object.assign(_options); url: string,
_options: CypressMermaidConfig,
_api: boolean = false,
validation?: any
): void => {
const options: CypressMermaidConfig = Object.assign(_options);
openURLAndVerifyRendering(url, options, validation); openURLAndVerifyRendering(url, options, validation);
}; };
export const renderGraph = (graphStr, options, api) => { export const renderGraph = (
const url = mermaidUrl(graphStr, options, api); graphStr: string,
options: CypressMermaidConfig = {},
api: boolean = false
): void => {
const url: string = mermaidUrl(graphStr, options, api);
openURLAndVerifyRendering(url, options); openURLAndVerifyRendering(url, options);
}; };
export const openURLAndVerifyRendering = (url, options, validation = undefined) => { export const openURLAndVerifyRendering = (
const useAppli = Cypress.env('useAppli'); url: string,
const name = (options.name || cy.state('runnable').fullTitle()).replace(/\s+/g, '-'); 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) { if (useAppli) {
cy.log('Opening eyes ' + Cypress.spec.name + ' --- ' + name); cy.log(`Opening eyes ${Cypress.spec.name} --- ${name}`);
cy.eyesOpen({ cy.eyesOpen({
appName: 'Mermaid', appName: 'Mermaid',
testName: name, testName: name,
@@ -83,9 +120,9 @@ export const openURLAndVerifyRendering = (url, options, validation = undefined)
} }
if (useAppli) { if (useAppli) {
cy.log('Check eyes' + Cypress.spec.name); cy.log(`Check eyes ${Cypress.spec.name}`);
cy.eyesCheckWindow('Click!'); cy.eyesCheckWindow('Click!');
cy.log('Closing eyes' + Cypress.spec.name); cy.log(`Closing eyes ${Cypress.spec.name}`);
cy.eyesClose(); cy.eyesClose();
} else { } else {
cy.matchImageSnapshot(name); cy.matchImageSnapshot(name);