2023-07-17 15:58:58 +03:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import { Buffer } from 'buffer';
|
2023-06-27 20:53:01 +03:00
|
|
|
import type { MermaidConfig } from '../../packages/mermaid/src/config.type.js';
|
|
|
|
|
2023-07-19 22:36:55 +05:30
|
|
|
interface CypressConfig {
|
2023-06-27 20:53:01 +03:00
|
|
|
listUrl?: boolean;
|
|
|
|
listId?: string;
|
|
|
|
name?: string;
|
2023-07-19 23:45:02 +03:00
|
|
|
}
|
2023-06-27 20:53:01 +03:00
|
|
|
type CypressMermaidConfig = MermaidConfig & CypressConfig;
|
|
|
|
|
|
|
|
interface CodeObject {
|
|
|
|
code: string;
|
|
|
|
mermaid: CypressMermaidConfig;
|
|
|
|
}
|
2019-09-11 18:53:05 +02:00
|
|
|
|
2023-06-27 20:53:01 +03:00
|
|
|
const utf8ToB64 = (str: string): string => {
|
2023-07-17 15:58:58 +03:00
|
|
|
return Buffer.from(decodeURIComponent(encodeURIComponent(str))).toString('base64');
|
2023-06-27 20:53:01 +03:00
|
|
|
};
|
2022-11-04 13:05:46 +01:00
|
|
|
|
2023-08-25 14:13:11 +02:00
|
|
|
const batchId: string =
|
|
|
|
'mermaid-batch-' +
|
|
|
|
(Cypress.env('useAppli')
|
|
|
|
? Date.now().toString()
|
|
|
|
: Cypress.env('CYPRESS_COMMIT') || Date.now().toString());
|
2023-06-27 20:53:01 +03:00
|
|
|
|
|
|
|
export const mermaidUrl = (
|
|
|
|
graphStr: string,
|
|
|
|
options: CypressMermaidConfig,
|
|
|
|
api: boolean
|
|
|
|
): string => {
|
|
|
|
const codeObject: CodeObject = {
|
2019-09-11 18:53:05 +02:00
|
|
|
code: graphStr,
|
2021-11-18 19:17:00 +01:00
|
|
|
mermaid: options,
|
2019-09-18 18:25:06 +02:00
|
|
|
};
|
2023-06-27 20:53:01 +03:00
|
|
|
const objStr: string = JSON.stringify(codeObject);
|
2023-06-27 21:05:44 +03:00
|
|
|
let url = `http://localhost:9000/e2e.html?graph=${utf8ToB64(objStr)}`;
|
2019-09-11 18:53:05 +02:00
|
|
|
if (api) {
|
2023-06-27 20:53:01 +03:00
|
|
|
url = `http://localhost:9000/xss.html?graph=${graphStr}`;
|
2019-09-11 18:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.listUrl) {
|
2019-09-18 18:25:06 +02:00
|
|
|
cy.log(options.listId, ' ', url);
|
2019-09-11 18:53:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 18:25:06 +02:00
|
|
|
return url;
|
|
|
|
};
|
2019-09-11 18:53:05 +02:00
|
|
|
|
2023-06-27 20:53:01 +03:00
|
|
|
export const imgSnapshotTest = (
|
|
|
|
graphStr: string,
|
|
|
|
_options: CypressMermaidConfig = {},
|
2023-06-27 21:05:44 +03:00
|
|
|
api = false,
|
2023-06-27 20:53:01 +03:00
|
|
|
validation?: any
|
|
|
|
): void => {
|
2023-09-06 22:50:32 +05:30
|
|
|
const options: CypressMermaidConfig = {
|
|
|
|
..._options,
|
2023-09-06 23:21:51 +05:30
|
|
|
fontFamily: _options.fontFamily || 'courier',
|
2023-09-06 22:50:32 +05:30
|
|
|
// @ts-ignore TODO: Fix type of fontSize
|
2023-09-06 23:21:51 +05:30
|
|
|
fontSize: _options.fontSize || '16px',
|
2023-09-06 22:50:32 +05:30
|
|
|
sequence: {
|
2023-09-06 23:21:51 +05:30
|
|
|
...(_options.sequence || {}),
|
2023-09-06 22:50:32 +05:30
|
|
|
actorFontFamily: 'courier',
|
2023-09-07 08:31:05 +05:30
|
|
|
noteFontFamily:
|
|
|
|
_options.sequence && _options.sequence.noteFontFamily
|
|
|
|
? _options.sequence.noteFontFamily
|
|
|
|
: 'courier',
|
2023-09-06 22:50:32 +05:30
|
|
|
messageFontFamily: 'courier',
|
|
|
|
},
|
|
|
|
};
|
2023-06-27 20:53:01 +03:00
|
|
|
|
|
|
|
const url: string = mermaidUrl(graphStr, options, api);
|
2023-01-16 02:07:37 +05:30
|
|
|
openURLAndVerifyRendering(url, options, validation);
|
2019-09-18 18:25:06 +02:00
|
|
|
};
|
2019-11-20 19:06:46 +01:00
|
|
|
|
2023-06-27 20:53:01 +03:00
|
|
|
export const urlSnapshotTest = (
|
|
|
|
url: string,
|
2023-09-06 22:50:32 +05:30
|
|
|
options: CypressMermaidConfig,
|
2023-06-27 21:05:44 +03:00
|
|
|
_api = false,
|
2023-06-27 20:53:01 +03:00
|
|
|
validation?: any
|
|
|
|
): void => {
|
2023-01-16 02:07:37 +05:30
|
|
|
openURLAndVerifyRendering(url, options, validation);
|
|
|
|
};
|
|
|
|
|
2023-06-27 20:53:01 +03:00
|
|
|
export const renderGraph = (
|
|
|
|
graphStr: string,
|
|
|
|
options: CypressMermaidConfig = {},
|
2023-06-27 21:05:44 +03:00
|
|
|
api = false
|
2023-06-27 20:53:01 +03:00
|
|
|
): void => {
|
|
|
|
const url: string = mermaidUrl(graphStr, options, api);
|
2023-01-16 02:07:37 +05:30
|
|
|
openURLAndVerifyRendering(url, options);
|
|
|
|
};
|
|
|
|
|
2023-06-27 20:53:01 +03:00
|
|
|
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, '-');
|
2022-06-21 21:17:53 +02:00
|
|
|
|
|
|
|
if (useAppli) {
|
2023-06-27 20:53:01 +03:00
|
|
|
cy.log(`Opening eyes ${Cypress.spec.name} --- ${name}`);
|
2022-06-21 21:17:53 +02:00
|
|
|
cy.eyesOpen({
|
2022-09-14 01:43:07 +01:00
|
|
|
appName: 'Mermaid',
|
2022-06-21 21:17:53 +02:00
|
|
|
testName: name,
|
2022-11-04 13:05:46 +01:00
|
|
|
batchName: Cypress.spec.name,
|
|
|
|
batchId: batchId + Cypress.spec.name,
|
2022-06-21 21:17:53 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cy.visit(url);
|
2023-01-16 02:07:37 +05:30
|
|
|
cy.window().should('have.property', 'rendered', true);
|
|
|
|
cy.get('svg').should('be.visible');
|
|
|
|
|
2022-10-19 20:06:54 +02:00
|
|
|
if (validation) {
|
|
|
|
cy.get('svg').should(validation);
|
|
|
|
}
|
2022-06-21 21:17:53 +02:00
|
|
|
|
|
|
|
if (useAppli) {
|
2023-06-27 20:53:01 +03:00
|
|
|
cy.log(`Check eyes ${Cypress.spec.name}`);
|
2022-06-21 21:17:53 +02:00
|
|
|
cy.eyesCheckWindow('Click!');
|
2023-06-27 20:53:01 +03:00
|
|
|
cy.log(`Closing eyes ${Cypress.spec.name}`);
|
2022-06-21 21:17:53 +02:00
|
|
|
cy.eyesClose();
|
|
|
|
} else {
|
|
|
|
cy.matchImageSnapshot(name);
|
|
|
|
}
|
|
|
|
};
|