Skip to content

Commit

Permalink
Refactor wait logic in assertCodePaneLineCount
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib committed May 22, 2024
1 parent ea02d95 commit a176e49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cypress/e2e/snippets.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Snippets', () => {
assertSnippetsSearchFieldIsVisible();
assertCodePaneLineCount(8);
filterSnippets('{esc}');
assertCodePaneLineCount(1, 500);
assertCodePaneLineCount(1, true);
typeCode(`${isMac() ? '{cmd}' : '{ctrl}'}k`);
assertSnippetsSearchFieldIsVisible();
assertCodePaneLineCount(8);
Expand All @@ -89,7 +89,7 @@ describe('Snippets', () => {
// Close without persisting
filterSnippets('{esc}');
assertCodePaneContains('<div>Initial <span>code</span></div>');
assertCodePaneLineCount(1, 500);
assertCodePaneLineCount(1, true);

// Re-open and persist
typeCode(`${isMac() ? '{cmd}' : '{ctrl}'}k`);
Expand Down
15 changes: 10 additions & 5 deletions cypress/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { createUrl } from '../../utils';
import { isMac } from '../../src/utils/formatting';
import type { Direction } from '../../src/Playroom/CodeEditor/keymaps/types';

const CYPRESS_DEFAULT_WAIT_TIME = 500;

export const cmdPlus = (keyCombo: string) => {
const platformSpecificKey = isMac() ? 'cmd' : 'ctrl';
return `${platformSpecificKey}+${keyCombo}`;
Expand Down Expand Up @@ -133,14 +135,17 @@ export const assertCodePaneContains = (text: string) => {
});
};

export const assertCodePaneLineCount = (lines: number, wait?: number) => {
export const assertCodePaneLineCount = (
lines: number,
wait: boolean = false
) => {
getCodeEditor().within(() =>
cy.get('.CodeMirror-line').should('have.length', lines)
);

// Wait after check to ensure original focus is restored
if (wait) {
cy.wait(wait);
cy.wait(CYPRESS_DEFAULT_WAIT_TIME); // eslint-disable-line @finsit/cypress/no-unnecessary-waiting
}
};

Expand Down Expand Up @@ -179,15 +184,15 @@ const typeInSearchField = (text: string) =>

export const findInCode = (term: string) => {
// Wait necessary to ensure code pane is focussed
cy.wait(500); // eslint-disable-line @finsit/cypress/no-unnecessary-waiting
cy.wait(CYPRESS_DEFAULT_WAIT_TIME); // eslint-disable-line @finsit/cypress/no-unnecessary-waiting
typeCode(`{${cmdPlus('f')}}`);

typeInSearchField(`${term}{enter}`);
};

export const replaceInCode = (term: string, replaceWith?: string) => {
// Wait necessary to ensure code pane is focussed
cy.wait(500); // eslint-disable-line @finsit/cypress/no-unnecessary-waiting
cy.wait(CYPRESS_DEFAULT_WAIT_TIME); // eslint-disable-line @finsit/cypress/no-unnecessary-waiting
typeCode(`{${cmdPlus('alt+f')}}`);
typeInSearchField(`${term}{enter}`);
if (replaceWith) {
Expand All @@ -197,7 +202,7 @@ export const replaceInCode = (term: string, replaceWith?: string) => {

export const jumpToLine = (line: number | string) => {
// Wait necessary to ensure code pane is focussed
cy.wait(500); // eslint-disable-line @finsit/cypress/no-unnecessary-waiting
cy.wait(CYPRESS_DEFAULT_WAIT_TIME); // eslint-disable-line @finsit/cypress/no-unnecessary-waiting
typeCode(`{${cmdPlus('g')}}`);
typeCode(`${line}{enter}`);
};
Expand Down

0 comments on commit a176e49

Please sign in to comment.