mirror of
				https://github.com/mermaid-js/mermaid.git
				synced 2025-10-30 18:34:09 +01:00 
			
		
		
		
	feat: Wait for rendering to finish before taking image snapshots
This commit is contained in:
		| @@ -6,6 +6,7 @@ | |||||||
|     "adamiecki", |     "adamiecki", | ||||||
|     "alois", |     "alois", | ||||||
|     "antiscript", |     "antiscript", | ||||||
|  |     "appli", | ||||||
|     "applitools", |     "applitools", | ||||||
|     "asciidoctor", |     "asciidoctor", | ||||||
|     "ashish", |     "ashish", | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ const utf8ToB64 = (str) => { | |||||||
|   return window.btoa(unescape(encodeURIComponent(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) => { | export const mermaidUrl = (graphStr, options, api) => { | ||||||
|   const obj = { |   const obj = { | ||||||
| @@ -46,8 +46,22 @@ export const imgSnapshotTest = (graphStr, _options, api = false, validation) => | |||||||
|   if (!options.fontSize) { |   if (!options.fontSize) { | ||||||
|     options.fontSize = '16px'; |     options.fontSize = '16px'; | ||||||
|   } |   } | ||||||
|  |   const url = mermaidUrl(graphStr, options, api); | ||||||
|  |   openURLAndVerifyRendering(url, options, validation); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | export const urlSnapshotTest = (url, _options, api = false, validation) => { | ||||||
|  |   const options = Object.assign(_options); | ||||||
|  |   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'); |   const useAppli = Cypress.env('useAppli'); | ||||||
|   cy.log('Hello ' + useAppli ? 'Appli' : 'image-snapshot'); |  | ||||||
|   const name = (options.name || cy.state('runnable').fullTitle()).replace(/\s+/g, '-'); |   const name = (options.name || cy.state('runnable').fullTitle()).replace(/\s+/g, '-'); | ||||||
|  |  | ||||||
|   if (useAppli) { |   if (useAppli) { | ||||||
| @@ -60,82 +74,20 @@ export const imgSnapshotTest = (graphStr, _options, api = false, validation) => | |||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   const url = mermaidUrl(graphStr, options, api); |  | ||||||
|  |  | ||||||
|   cy.visit(url); |   cy.visit(url); | ||||||
|  |   cy.window().should('have.property', 'rendered', true); | ||||||
|  |   cy.get('svg').should('be.visible'); | ||||||
|  |  | ||||||
|   if (validation) { |   if (validation) { | ||||||
|     cy.get('svg').should(validation); |     cy.get('svg').should(validation); | ||||||
|   } |   } | ||||||
|   cy.get('svg'); |  | ||||||
|   // Default name to test title |  | ||||||
|  |  | ||||||
|   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); | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| 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'; |  | ||||||
|   } |  | ||||||
|   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.eyesOpen({ |  | ||||||
|       appName: 'Mermaid', |  | ||||||
|       testName: name, |  | ||||||
|       batchName: Cypress.spec.name, |  | ||||||
|       batchId: batchId + Cypress.spec.name, |  | ||||||
|     }); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   cy.visit(url); |  | ||||||
|   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.eyesCheckWindow('Click!'); |  | ||||||
|     cy.log('Closing eyes 2' + Cypress.spec.name); |  | ||||||
|     cy.eyesClose(); |  | ||||||
|   } else { |  | ||||||
|     cy.matchImageSnapshot(name); |  | ||||||
|   } |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| export const renderGraph = (graphStr, options, api) => { |  | ||||||
|   const url = mermaidUrl(graphStr, options, api); |  | ||||||
|  |  | ||||||
|   cy.visit(url); |  | ||||||
| }; |  | ||||||
|   | |||||||
| @@ -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 |  * Check whether the SVG Element has a Mindmap root | ||||||
| @@ -158,7 +158,6 @@ mindmap | |||||||
|       undefined, |       undefined, | ||||||
|       shouldHaveRoot |       shouldHaveRoot | ||||||
|     ); |     ); | ||||||
|     cy.get('svg'); |  | ||||||
|   }); |   }); | ||||||
|   it('rounded rect shape', () => { |   it('rounded rect shape', () => { | ||||||
|     imgSnapshotTest( |     imgSnapshotTest( | ||||||
| @@ -172,7 +171,6 @@ mindmap | |||||||
|       undefined, |       undefined, | ||||||
|       shouldHaveRoot |       shouldHaveRoot | ||||||
|     ); |     ); | ||||||
|     cy.get('svg'); |  | ||||||
|   }); |   }); | ||||||
|   it('circle shape', () => { |   it('circle shape', () => { | ||||||
|     imgSnapshotTest( |     imgSnapshotTest( | ||||||
| @@ -186,7 +184,6 @@ mindmap | |||||||
|       undefined, |       undefined, | ||||||
|       shouldHaveRoot |       shouldHaveRoot | ||||||
|     ); |     ); | ||||||
|     cy.get('svg'); |  | ||||||
|   }); |   }); | ||||||
|   it('default shape', () => { |   it('default shape', () => { | ||||||
|     imgSnapshotTest( |     imgSnapshotTest( | ||||||
| @@ -198,7 +195,6 @@ mindmap | |||||||
|       undefined, |       undefined, | ||||||
|       shouldHaveRoot |       shouldHaveRoot | ||||||
|     ); |     ); | ||||||
|     cy.get('svg'); |  | ||||||
|   }); |   }); | ||||||
|   it('adding children', () => { |   it('adding children', () => { | ||||||
|     imgSnapshotTest( |     imgSnapshotTest( | ||||||
| @@ -212,7 +208,6 @@ mindmap | |||||||
|       undefined, |       undefined, | ||||||
|       shouldHaveRoot |       shouldHaveRoot | ||||||
|     ); |     ); | ||||||
|     cy.get('svg'); |  | ||||||
|   }); |   }); | ||||||
|   it('adding grand children', () => { |   it('adding grand children', () => { | ||||||
|     imgSnapshotTest( |     imgSnapshotTest( | ||||||
| @@ -227,7 +222,6 @@ mindmap | |||||||
|       undefined, |       undefined, | ||||||
|       shouldHaveRoot |       shouldHaveRoot | ||||||
|     ); |     ); | ||||||
|     cy.get('svg'); |  | ||||||
|   }); |   }); | ||||||
|   /* The end */ |   /* The end */ | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -5,6 +5,13 @@ function b64ToUtf8(str) { | |||||||
|   return decodeURIComponent(escape(window.atob(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 |  * ##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 |  * configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the | ||||||
| @@ -39,7 +46,8 @@ const contentLoaded = async function () { | |||||||
|  |  | ||||||
|     await mermaid2.registerExternalDiagrams([mindmap]); |     await mermaid2.registerExternalDiagrams([mindmap]); | ||||||
|     mermaid2.initialize(graphObj.mermaid); |     mermaid2.initialize(graphObj.mermaid); | ||||||
|     mermaid2.init(); |     await mermaid2.init(); | ||||||
|  |     markRendered(); | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -128,6 +136,7 @@ const contentLoadedApi = function () { | |||||||
|       ); |       ); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |   markRendered(); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| if (typeof document !== 'undefined') { | if (typeof document !== 'undefined') { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Sidharth Vinod
					Sidharth Vinod