Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cypress/helpers/util.ts #4340

Merged
merged 7 commits into from
Sep 6, 2023
Merged
57 changes: 21 additions & 36 deletions cypress/helpers/util.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
const utf8ToB64 = (str) => {
return window.btoa(unescape(encodeURIComponent(str)));
return btoa(unescape(encodeURIComponent(str)));
};

const batchId = 'mermaid-batch' + new Date().getTime();
const batchId = 'mermaid-batch' + Date.now();

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

if (options.listUrl) {
cy.log(options.listId, ' ', url);
Expand All @@ -22,36 +19,24 @@ export const mermaidUrl = (graphStr, options, api) => {
return url;
};

export const imgSnapshotTest = (graphStr, _options = {}, api = false, validation = undefined) => {
cy.log(_options);
const options = 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 = '16px';
}
export const imgSnapshotTest = (graphStr, _options = {}, api = false, validation) => {
const options = {
..._options,
fontFamily: _options.fontFamily || 'courier',
fontSize: _options.fontSize || '16px',
sequence: {
...(options.sequence || {}),
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved
actorFontFamily: 'courier',
noteFontFamily: _options.sequence?.noteFontFamily || 'courier',
messageFontFamily: 'courier',
},
};

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

export const urlSnapshotTest = (url, _options, api = false, validation) => {
const options = Object.assign(_options);
export const urlSnapshotTest = (url, options = {}, api = false, validation) => {
openURLAndVerifyRendering(url, options, validation);
};

Expand All @@ -60,12 +45,12 @@ export const renderGraph = (graphStr, options, api) => {
openURLAndVerifyRendering(url, options);
};

const openURLAndVerifyRendering = (url, options, validation = undefined) => {
const openURLAndVerifyRendering = (url, options, validation) => {
const useAppli = Cypress.env('useAppli');
const name = (options.name || cy.state('runnable').fullTitle()).replace(/\s+/g, '-');

if (useAppli) {
cy.log('Opening eyes ' + Cypress.spec.name + ' --- ' + name);
cy.log(`Opening eyes ${Cypress.spec.name} --- ${name}`);
cy.eyesOpen({
appName: 'Mermaid',
testName: name,
Expand All @@ -83,9 +68,9 @@ const openURLAndVerifyRendering = (url, options, validation = undefined) => {
}

if (useAppli) {
cy.log('Check eyes' + Cypress.spec.name);
cy.log(`Check eyes ${Cypress.spec.name}`);
cy.eyesCheckWindow('Click!');
cy.log('Closing eyes' + Cypress.spec.name);
cy.log(`Closing eyes ${Cypress.spec.name}`);
cy.eyesClose();
} else {
cy.matchImageSnapshot(name);
Expand Down