Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Apr 13, 2023
1 parent 8a13644 commit 982ef03
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
6 changes: 3 additions & 3 deletions cypress/e2e/actions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { verifyFileSizeGreaterThan } from './util';
import { typeInEditor, verifyFileSizeGreaterThan } from './util';
describe('Check actions', () => {
beforeEach(() => {
cy.clearLocalStorage();
Expand All @@ -10,7 +10,7 @@ describe('Check actions', () => {
cy.get('#markdown')
.invoke('val')
.then((oldText) => {
cy.get('#editor').click('bottom').type('{enter}C --> HistoryTest');
typeInEditor('C --> HistoryTest', { bottom: true, newline: true });
cy.get('#markdown')
.invoke('val')
.then((newText) => {
Expand All @@ -29,7 +29,7 @@ describe('Check actions', () => {
cy.clock(new Date(2022, 0, 1).getTime());

cy.get(`#downloadPNG`).click();
verifyFileSizeGreaterThan('diagram', 'png', 35_000);
verifyFileSizeGreaterThan('diagram', 'png', 34_000);

cy.get(`#downloadSVG`).click();
verifyFileSizeGreaterThan('diagram', 'svg', 10_000);
Expand Down
24 changes: 12 additions & 12 deletions cypress/e2e/diagramUpdate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEditor, cmd } from './util';
import { typeInEditor, cmd } from './util';

describe('Auto sync tests', () => {
beforeEach(() => {
Expand All @@ -10,7 +10,7 @@ describe('Auto sync tests', () => {
cy.contains('Auto sync').click();
cy.get('#view').should('not.have.class', 'outOfSync');
cy.get('#errorContainer').should('not.exist');
getEditor({ bottom: true, newline: true }).type(' C --> Test');
typeInEditor(' C --> Test', { bottom: true });
cy.get('#view').should('have.class', 'outOfSync');
cy.get('#errorContainer').should('contain.text', 'Diagram out of sync.');
cy.getLocalStorage('codeStore').snapshot();
Expand All @@ -19,10 +19,10 @@ describe('Auto sync tests', () => {
it('should update diagram when shortcut is used', () => {
cy.contains('Auto sync').click();
cy.get('#view').should('not.have.class', 'outOfSync');
getEditor().type(' C --> Test');
typeInEditor(' C --> Test');
cy.get('#view').should('have.class', 'outOfSync');
cy.get('#errorContainer').should('contain.text', 'Diagram out of sync.');
getEditor().type(`${cmd}{enter}`);
typeInEditor(`${cmd}{enter}`);
cy.get('#errorContainer').should('not.exist');
cy.get('#view').should('not.have.class', 'outOfSync');
});
Expand All @@ -38,21 +38,21 @@ describe('Auto sync tests', () => {
it('should not dim diagram when code is in sync', () => {
cy.contains('Auto sync').click();
cy.get('#view').should('not.have.class', 'outOfSync');
getEditor().type(' C --> Test');
typeInEditor(' C --> Test');
cy.get('#view').should('have.class', 'outOfSync');
cy.get('[data-cy=sync]').click();
cy.get('#view').should('not.have.class', 'outOfSync');
cy.get('#autoSync').check();
getEditor().type('ing');
typeInEditor('ing');
cy.get('#view').should('not.have.class', 'outOfSync');
cy.getLocalStorage('codeStore').snapshot();
});

it('supports commenting code out/in', () => {
getEditor({ bottom: true, newline: false }).type(`{uparrow}${cmd}/`);
typeInEditor(`{uparrow}${cmd}/`, { bottom: true, newline: false });
cy.get('#view').contains('Car').should('not.exist');

getEditor().type(`{uparrow}${cmd}/`);
typeInEditor(`{uparrow}${cmd}/`);
cy.get('#view').contains('Car').should('exist');
});

Expand All @@ -61,7 +61,7 @@ describe('Auto sync tests', () => {
'/edit#pako:eNpljjEKwzAMRa8SNOcEnlt6gK5eVFvYJsgOqkwpIXevg9smEE1PnyfxF3DFExgISW-CczQ2D21cYU7a-SGYXRwyvTp9jUhuKlVP-eHy7zA-leQsMEmg_QOM0BLG5FujZVMsaCQmC6ahR5ks2Lw2r84ela4-aREwKpVGwKrl_s7ut3fnkjAIcg_XDzuaUhs'
);
cy.get('#errorContainer').should('not.exist');
getEditor({ newline: true }).type(`branch test`);
typeInEditor(`branch test`, { newline: true });
cy.get('#editor').contains('branch test').should('exist');
cy.get('#errorContainer')
.contains(
Expand All @@ -72,9 +72,9 @@ describe('Auto sync tests', () => {

it('should update diagram after entire text is removed', () => {
// https://github.com/mermaid-js/mermaid-live-editor/issues/1102
getEditor().type(`${cmd} a {backspace}`);
getEditor().type('graph LR');
getEditor().type(' {enter} A-->Car');
typeInEditor(`${cmd} a {backspace}`);
typeInEditor('graph LR');
typeInEditor(' {enter} A-->Car');
cy.get('#view').contains('Car').should('exist');
});
});
Expand Down
10 changes: 5 additions & 5 deletions cypress/e2e/history.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEditor, verifyFileSnapshot } from './util';
import { typeInEditor, verifyFileSnapshot } from './util';

describe('Save History', () => {
beforeEach(() => {
Expand Down Expand Up @@ -51,14 +51,14 @@ describe('Save History', () => {
expect(str).to.equal('State already saved.');
});
cy.on('window:confirm', () => true);
getEditor().type(' C --> HistoryTest');
typeInEditor(' C --> HistoryTest');
cy.get('#saveHistory').click();
cy.get('#historyList').find('li').should('have.length', 2);
});

it('should be able to restore and delete', () => {
cy.get('#saveHistory').click();
getEditor().type(' C --> HistoryTest');
typeInEditor(' C --> HistoryTest');
cy.get('#historyList').find('No items in History').should('not.exist');
cy.get('#historyList').find('li').should('have.length', 1);
cy.contains('HistoryTest');
Expand All @@ -68,7 +68,7 @@ describe('Save History', () => {
cy.get('#historyList').find('li').should('have.length', 0);
cy.get('#historyList').contains('No items in History');
cy.get('#saveHistory').click();
getEditor().type(' C --> HistoryTest');
typeInEditor(' C --> HistoryTest');
cy.get('#saveHistory').click();
cy.get('#editor').type('ing');
cy.get('#clearHistory').click();
Expand All @@ -81,7 +81,7 @@ describe('Save History', () => {

// TODO: Fix #639
xit('should auto save history', () => {
getEditor().type(' C --> HistoryTest');
typeInEditor(' C --> HistoryTest');
cy.tick(70_000);
cy.contains('Timeline').click();
cy.get('#historyList').find('li').should('have.length', 1);
Expand Down
25 changes: 18 additions & 7 deletions cypress/e2e/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
export const cmd = `{${Cypress.platform === 'darwin' ? 'meta' : 'ctrl'}}`;
const editor = () => cy.get('#editor textarea:first');

export const getEditor = ({ bottom = true, newline = false } = {}) =>
cy
.get('#editor textarea:first')
.click()
.focused()
.type(`${bottom ? '{pageDown}' : cmd}`)
.type(`${newline ? '{enter}' : cmd}`);
interface EditorOptions {
bottom?: boolean;
newline?: boolean;
}

export const typeInEditor = (
text: string,
{ bottom = true, newline = false }: EditorOptions = {}
) => {
if (bottom) {
editor().type('{pageDown}');
}
if (newline) {
editor().type('{enter}');
}
editor().type(text);
};

const downloadsFolder = Cypress.config('downloadsFolder');

Expand Down
2 changes: 1 addition & 1 deletion cypress/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
"1": "{\"code\":\"graph TD\\n A[Party] -->|Get money| B(Go shopping!!)\\n \",\"mermaid\":\"{\\n \\\"theme\\\": \\\"forest\\\",\\n \\\"test\\\": \\\"hello world\\\"\\n}\",\"autoSync\":true,\"updateDiagram\":true,\"loader\":{\"type\":\"files\",\"config\":{\"codeURL\":\"https://gist.githubusercontent.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/raw/4eb03887e6a41397e80bdcdbf94017c498f8f1e2/code.mmd\",\"configURL\":\"https://gist.githubusercontent.com/sidharthv96/6268a23e673a533dcb198f241fd7012a/raw/4eb03887e6a41397e80bdcdbf94017c498f8f1e2/config.json\"}}}"
}
},
"__version": "11.2.0",
"__version": "12.9.0",
"Auto sync tests": {
"should dim diagram when code is edited": {
"1": "{\"code\":\"flowchart TD\\n A[Christmas] -->|Get money| B(Go shopping)\\n B --> C{Let me think}\\n C -->|One| D[Laptop]\\n C -->|Two| E[iPhone]\\n C -->|Three| F[fa:fa-car Car]\\n C --> Test\",\"mermaid\":\"{\\n \\\"theme\\\": \\\"default\\\"\\n}\",\"autoSync\":false,\"updateDiagram\":false}"
Expand Down

0 comments on commit 982ef03

Please sign in to comment.