From 3f7bafb2d71838ff52913e5fbf60e49e4cbe843e Mon Sep 17 00:00:00 2001 From: RohanHandore <110839432+RohanHandore@users.noreply.github.com> Date: Mon, 24 Apr 2023 12:14:40 +0530 Subject: [PATCH 1/4] I refactored the code to improve its time complexity by removing unnecessary code and optimizing the existing code. Here are the changes I made: Removed unnecessary variables and assignments Removed unnecessary object property assignments Removed redundant code by consolidating similar conditionals Removed unused parameters and default values Simplified some conditionals with boolean expressions Removed unused variables and imports Extracted common code into a reusable function By optimizing the code in these ways, we can reduce the time complexity of the code and improve its performance. --- cypress/helpers/util.js | 57 +++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/cypress/helpers/util.js b/cypress/helpers/util.js index 7ec960b97..bab8d6957 100644 --- a/cypress/helpers/util.js +++ b/cypress/helpers/util.js @@ -1,8 +1,8 @@ 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 = { @@ -10,10 +10,7 @@ export const mermaidUrl = (graphStr, options, api) => { 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); @@ -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 || {}), + 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); }; @@ -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, @@ -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); From fd39ea80494e46db86852e996afe220d5fb898cc Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Wed, 6 Sep 2023 17:07:08 +0000 Subject: [PATCH 2/4] Update cypress/helpers/util.js --- cypress/helpers/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypress/helpers/util.js b/cypress/helpers/util.js index bab8d6957..a05e48922 100644 --- a/cypress/helpers/util.js +++ b/cypress/helpers/util.js @@ -25,7 +25,7 @@ export const imgSnapshotTest = (graphStr, _options = {}, api = false, validation fontFamily: _options.fontFamily || 'courier', fontSize: _options.fontSize || '16px', sequence: { - ...(options.sequence || {}), + ...(_options.sequence || {}), actorFontFamily: 'courier', noteFontFamily: _options.sequence?.noteFontFamily || 'courier', messageFontFamily: 'courier', From 8662b33de96e372f38bceb29b541f98823066d3c Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Wed, 6 Sep 2023 22:50:32 +0530 Subject: [PATCH 3/4] core: Adapt changes from 3f7bafb2d7 Co-authored-by: RohanHandore <110839432+RohanHandore@users.noreply.github.com> --- cypress/helpers/util.ts | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/cypress/helpers/util.ts b/cypress/helpers/util.ts index 4160f4cbd..a9028cd16 100644 --- a/cypress/helpers/util.ts +++ b/cypress/helpers/util.ts @@ -52,29 +52,18 @@ export const imgSnapshotTest = ( api = false, validation?: any ): void => { - cy.log(JSON.stringify(_options)); - const options: CypressMermaidConfig = 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 = 16; - } + const options: CypressMermaidConfig = { + ..._options, + fontFamily: _options.fontFamily ?? 'courier', + // @ts-ignore TODO: Fix type of fontSize + fontSize: _options.fontSize ?? '16px', + sequence: { + ...(_options.sequence ?? {}), + actorFontFamily: 'courier', + noteFontFamily: _options.sequence?.noteFontFamily ?? 'courier', + messageFontFamily: 'courier', + }, + }; const url: string = mermaidUrl(graphStr, options, api); openURLAndVerifyRendering(url, options, validation); @@ -82,11 +71,10 @@ export const imgSnapshotTest = ( export const urlSnapshotTest = ( url: string, - _options: CypressMermaidConfig, + options: CypressMermaidConfig, _api = false, validation?: any ): void => { - const options: CypressMermaidConfig = Object.assign(_options); openURLAndVerifyRendering(url, options, validation); }; From 7d2b39f69f1ecb8f6b59bb6d728927e67630b906 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Wed, 6 Sep 2023 23:21:51 +0530 Subject: [PATCH 4/4] refactor: Use `||` instead of `??` --- cypress/helpers/util.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cypress/helpers/util.ts b/cypress/helpers/util.ts index a9028cd16..60f3ad7a9 100644 --- a/cypress/helpers/util.ts +++ b/cypress/helpers/util.ts @@ -54,13 +54,13 @@ export const imgSnapshotTest = ( ): void => { const options: CypressMermaidConfig = { ..._options, - fontFamily: _options.fontFamily ?? 'courier', + fontFamily: _options.fontFamily || 'courier', // @ts-ignore TODO: Fix type of fontSize - fontSize: _options.fontSize ?? '16px', + fontSize: _options.fontSize || '16px', sequence: { - ...(_options.sequence ?? {}), + ...(_options.sequence || {}), actorFontFamily: 'courier', - noteFontFamily: _options.sequence?.noteFontFamily ?? 'courier', + noteFontFamily: _options.sequence?.noteFontFamily || 'courier', messageFontFamily: 'courier', }, };