Skip to content

Commit

Permalink
Merge d320e78 into ea3d7bc
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Jan 16, 2023
2 parents ea3d7bc + d320e78 commit 0be22b5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 73 deletions.
1 change: 1 addition & 0 deletions cSpell.json
Expand Up @@ -6,6 +6,7 @@
"adamiecki",
"alois",
"antiscript",
"appli",
"applitools",
"asciidoctor",
"ashish",
Expand Down
82 changes: 17 additions & 65 deletions cypress/helpers/util.js
Expand Up @@ -2,7 +2,7 @@ const utf8ToB64 = (str) => {
return window.btoa(unescape(encodeURIComponent(str)));
};

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

export const mermaidUrl = (graphStr, options, api) => {
const obj = {
Expand Down Expand Up @@ -46,69 +46,26 @@ export const imgSnapshotTest = (graphStr, _options, api = false, validation) =>
if (!options.fontSize) {
options.fontSize = '16px';
}
const useAppli = Cypress.env('useAppli');
cy.log('Hello ' + useAppli ? 'Appli' : 'image-snapshot');
const name = (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,
});
}

const url = mermaidUrl(graphStr, options, api);

cy.visit(url);
if (validation) {
cy.get('svg').should(validation);
}
cy.get('svg');
// Default name to test title

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);
}
openURLAndVerifyRendering(url, options, validation);
};

export const urlSnapshotTest = (url, _options, api = false, validation) => {
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';
}
openURLAndVerifyRendering(url, options, validation);
};

export const renderGraph = (graphStr, options, api) => {
const url = mermaidUrl(graphStr, options, api);
openURLAndVerifyRendering(url, options);
};

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

if (useAppli) {
cy.log('Opening eyes 2' + Cypress.spec.name);
cy.log('Opening eyes ' + Cypress.spec.name + ' --- ' + name);
cy.eyesOpen({
appName: 'Mermaid',
testName: name,
Expand All @@ -118,24 +75,19 @@ export const urlSnapshotTest = (url, _options, api = false, validation) => {
}

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

if (validation) {
cy.get('svg').should(validation);
}
cy.get('body');
// Default name to test title

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

export const renderGraph = (graphStr, options, api) => {
const url = mermaidUrl(graphStr, options, api);

cy.visit(url);
};
8 changes: 1 addition & 7 deletions cypress/integration/rendering/mindmap.spec.ts
@@ -1,4 +1,4 @@
import { imgSnapshotTest, renderGraph } from '../../helpers/util.js';
import { imgSnapshotTest } from '../../helpers/util.js';

/**
* Check whether the SVG Element has a Mindmap root
Expand Down Expand Up @@ -158,7 +158,6 @@ mindmap
undefined,
shouldHaveRoot
);
cy.get('svg');
});
it('rounded rect shape', () => {
imgSnapshotTest(
Expand All @@ -172,7 +171,6 @@ mindmap
undefined,
shouldHaveRoot
);
cy.get('svg');
});
it('circle shape', () => {
imgSnapshotTest(
Expand All @@ -186,7 +184,6 @@ mindmap
undefined,
shouldHaveRoot
);
cy.get('svg');
});
it('default shape', () => {
imgSnapshotTest(
Expand All @@ -198,7 +195,6 @@ mindmap
undefined,
shouldHaveRoot
);
cy.get('svg');
});
it('adding children', () => {
imgSnapshotTest(
Expand All @@ -212,7 +208,6 @@ mindmap
undefined,
shouldHaveRoot
);
cy.get('svg');
});
it('adding grand children', () => {
imgSnapshotTest(
Expand All @@ -227,7 +222,6 @@ mindmap
undefined,
shouldHaveRoot
);
cy.get('svg');
});
/* The end */
});
3 changes: 3 additions & 0 deletions cypress/platform/external-diagrams-mindmap.html
Expand Up @@ -44,6 +44,9 @@ <h1>Should correctly load a third-party diagram using registerDiagram</h1>
await mermaid.registerExternalDiagrams([mindmap]);
await mermaid.initialize({ logLevel: 0 });
await mermaid.initThrowsErrorsAsync();
if (window.Cypress) {
window.rendered = true;
}
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions cypress/platform/ghsa1.html
Expand Up @@ -21,6 +21,9 @@ <h1>Background should be yellow!!!</h1>
const diagram = document.getElementById('diagram');
const svg = mermaid.render('diagram-svg', graph);
diagram.innerHTML = svg;
if (window.Cypress) {
window.rendered = true;
}
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions cypress/platform/ghsa2.html
Expand Up @@ -21,6 +21,9 @@ <h1>This element does not belong to the SVG but we can style it</h1>
const diagram = document.getElementById('diagram');
const svg = mermaid.render('diagram-svg', graph);
diagram.innerHTML = svg;
if (window.Cypress) {
window.rendered = true;
}
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions cypress/platform/ghsa3.html
Expand Up @@ -94,6 +94,9 @@ <h1>PAGE SHOULD NOT BE RED</h1>
// document.querySelector('#diagram').innerHTML = diagram;
mermaid.render('diagram', diagram, (res) => {
document.querySelector('#res').innerHTML = res;
if (window.Cypress) {
window.rendered = true;
}
});
</script>
</body>
Expand Down
11 changes: 10 additions & 1 deletion cypress/platform/viewer.js
Expand Up @@ -5,6 +5,13 @@ function b64ToUtf8(str) {
return decodeURIComponent(escape(window.atob(str)));
}

// Adds a rendered flag to window when rendering is done, so cypress can wait for it.
function markRendered() {
if (window.Cypress) {
window.rendered = true;
}
}

/**
* ##contentLoaded Callback function that is called when page is loaded. This functions fetches
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the
Expand Down Expand Up @@ -39,7 +46,8 @@ const contentLoaded = async function () {

await mermaid2.registerExternalDiagrams([mindmap]);
mermaid2.initialize(graphObj.mermaid);
mermaid2.init();
await mermaid2.init();
markRendered();
}
};

Expand Down Expand Up @@ -128,6 +136,7 @@ const contentLoadedApi = function () {
);
}
}
markRendered();
};

if (typeof document !== 'undefined') {
Expand Down

0 comments on commit 0be22b5

Please sign in to comment.