diff options
Diffstat (limited to 'cypress/e2e/core-utils.ts')
-rw-r--r-- | cypress/e2e/core-utils.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/cypress/e2e/core-utils.ts b/cypress/e2e/core-utils.ts index 24053941968..4756836387a 100644 --- a/cypress/e2e/core-utils.ts +++ b/cypress/e2e/core-utils.ts @@ -48,3 +48,43 @@ export enum UnifiedSearchFilter { export function getUnifiedSearchFilter(filter: UnifiedSearchFilter) { return getUnifiedSearchModal().find(`[data-cy-unified-search-filters] [data-cy-unified-search-filter="${CSS.escape(filter)}"]`) } + +/** + * Assertion that an element is fully within the current viewport. + * @param $el The element + * @param expected If the element is expected to be fully in viewport or not fully + * @example + * ```js + * cy.get('#my-element') + * .should(beFullyInViewport) + * ``` + */ +export function beFullyInViewport($el: JQuery<HTMLElement>, expected = true) { + const { top, left, bottom, right } = $el.get(0)!.getBoundingClientRect() + const innerHeight = Cypress.$('body').innerHeight()! + const innerWidth = Cypress.$('body').innerWidth()! + const fullyVisible = top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth + + console.debug(`fullyVisible: ${fullyVisible}, top: ${top >= 0}, left: ${left >= 0}, bottom: ${bottom <= innerHeight}, right: ${right <= innerWidth}`) + + if (expected) { + // eslint-disable-next-line no-unused-expressions + expect(fullyVisible, 'Fully within viewport').to.be.true + } else { + // eslint-disable-next-line no-unused-expressions + expect(fullyVisible, 'Not fully within viewport').to.be.false + } +} + +/** + * Opposite of `beFullyInViewport` - resolves when element is not or only partially in viewport. + * @param $el The element + * @example + * ```js + * cy.get('#my-element') + * .should(notBeFullyInViewport) + * ``` + */ +export function notBeFullyInViewport($el: JQuery<HTMLElement>) { + return beFullyInViewport($el, false) +} |