Code Monkey home page Code Monkey logo

chai-in-viewport's Introduction

chai-in-viewport

npm version

Chai plugin to check if a DOM element is currently within the visible viewport

Introduction

chai-in-viewport is an extension to the chai assertion library that provides a new assertion to check if a DOM element is within the browser's currently visible viewport (i.e. is not scrolled out of view)

It is intended for use in integration tests that run in a browser or browser-like environment (e.g. cypress.io). It assumes access to HTMLElement, document and window objects

Installation

Install using npm

npm install chai-in-viewport

Usage

In setup for your tests, import the plugin and enable it within chai

import chai from 'chai'
import chaiInViewport from 'chai-in-viewport'

chai.use(chaiInViewport);

Assertions

chai-in-viewport adds the inViewport assertion, that can be applied to an HTMLElement

expect(element1).to.be.inViewport
expect(element2).to.not.be.inViewport

Limitations

The inViewport assertion currently simply tests the element's getBoundingClientRect against the documentElement's clientWidth and clientHeight. It does not check whether the element is clipped by its parents, if it has visibility: hidden, opacity: 0, is obscured by another element or is otherwise hidden from view on the screen

Testing for visibility in cypress.io

If using cypress.io, it may be useful to use a combination of visible and inViewport assertions, which will also check for the above limitations

cy.get('#el').should('be.visible.and.inViewport');

chai-in-viewport's People

Contributors

borilla avatar

Watchers

James Cloos avatar  avatar

Forkers

iqbmo04

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.