All Projects → dyakovk → jest-matchmedia-mock

dyakovk / jest-matchmedia-mock

Licence: MIT license
🃏 Mock for fully testing any media queries with Jest

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to jest-matchmedia-mock

web-extension-boilerplate
The web extension boilerplate help to set up project quickly using typescript, jest, webpack, githook, prettier and github actions
Stars: ✭ 35 (+0%)
Mutual labels:  jest
jest
Super-fast alternative for babel-jest or ts-jest without type checking
Stars: ✭ 472 (+1248.57%)
Mutual labels:  jest
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (+14.29%)
Mutual labels:  jest
firestore-jest-mock
Jest Helper library for mocking Cloud Firestore
Stars: ✭ 128 (+265.71%)
Mutual labels:  jest
energy-use-case-trading-client
Energy Use Case Web UI for Lition Trading Platform
Stars: ✭ 23 (-34.29%)
Mutual labels:  jest
license-auditor
License Auditor helps you track and validate licenses inside your project.
Stars: ✭ 15 (-57.14%)
Mutual labels:  jest
fly-helper
It's a Tool library, method collection
Stars: ✭ 21 (-40%)
Mutual labels:  jest
ng-mono-repo-starter
Angular Mono Repo Starter
Stars: ✭ 79 (+125.71%)
Mutual labels:  jest
postcss-sort-media-queries
PostCSS plugin for combine and sort CSS media queries with mobile first or desktop first methods.
Stars: ✭ 118 (+237.14%)
Mutual labels:  media-queries
react-unit-test-practice
No description or website provided.
Stars: ✭ 16 (-54.29%)
Mutual labels:  jest
jest-extended-snapshot
Additional Jest matchers for snapshot testing.
Stars: ✭ 18 (-48.57%)
Mutual labels:  jest
xstate-marionettist
Model based testing with Jest, XState and Puppeteer or Playwright made easy
Stars: ✭ 23 (-34.29%)
Mutual labels:  jest
jest-file-snapshot
Jest matcher to write snapshots to a separate file instead of the default snapshot file used by Jest
Stars: ✭ 35 (+0%)
Mutual labels:  jest
Splain
small parser to create more interesting language/sentences
Stars: ✭ 15 (-57.14%)
Mutual labels:  jest
OpenWebSheet
OpenSource Web based spreadsheet
Stars: ✭ 30 (-14.29%)
Mutual labels:  jest
jest-playground
Playing around with Jest - Subscribe to my YouTube channel: https://bit.ly/CognitiveSurge
Stars: ✭ 24 (-31.43%)
Mutual labels:  jest
riteway-jest
Unit tests that always supply a good bug report when they fail for Jest.
Stars: ✭ 24 (-31.43%)
Mutual labels:  jest
angular-unit-testing-examples
Showroom for different Angular unit testing concepts
Stars: ✭ 19 (-45.71%)
Mutual labels:  jest
enzyme-extensions
🎩 Enzyme extensions to test shallowly rendered enzyme wrappers 🏄🏻
Stars: ✭ 30 (-14.29%)
Mutual labels:  jest
sqrs
🚌SQRS is a JavaScript library for implementing CQRS pattern.
Stars: ✭ 23 (-34.29%)
Mutual labels:  jest

🃏 MatchMedia Mock for Jest

Travis (.org) Coveralls github npm GitHub top language npm type definitions node npm peer dependency version

This implementation of the window.matchMedia method allows you to control your media queries and their listening functions. You can update the media query that is currently applied to the document by simply calling one function.

Installation

NPM

npm i --save-dev jest-matchmedia-mock

Yarn

yarn add --dev jest-matchmedia-mock

Usage

import MatchMediaMock from 'jest-matchmedia-mock';

let matchMedia;

describe('Your testing module' => {
  beforeAll(() => {
    matchMedia = new MatchMediaMock();
  });

  afterEach(() => {
    matchMedia.clear();
  });

  test('Your test case', () => {
    const mediaQuery = '(prefers-color-scheme: light)';
    const firstListener = jest.fn();
    const secondListener = jest.fn();
    const mql = window.matchMedia(mediaQuery);

    mql.addEventListener("change", ev => ev.matches && firstListener());
    mql.addEventListener("change", ev => ev.matches && secondListener());

    matchMedia.useMediaQuery(mediaQuery);

    expect(firstListener).toBeCalledTimes(1);
    expect(secondListener).toBeCalledTimes(1);
  })
})

This works if window.matchMedia() is used in a function (or method) which is invoked in the test. If window.matchMedia() is executed directly in the tested file, Jest returns TypeError: window.matchMedia is not a function and doesn't properly execute the test.

In this case, the solution is to move the instantiation of the mock into a separate file and include this one in the test before the tested file:

import matchMedia from './matchMedia.mock.ts'; // Must be imported before the tested file
import { myMethod } from './file-to-test';

describe('myMethod()', () => {
  // Test the method here...
});

API

new MatchMediaMock()

Implements window.matchMedia and returns an instance with methods listed below

matchMedia.useMediaQuery()

Updates the currently used media query, and calls previously added listener functions registered for the passed media query

  • Arguments:

    • mediaQuery: string;
  • Returns: never | void

matchMedia.getMediaQueries()

Returns an array listing the media queries for which the matchMedia has registered listeners

  • Returns: string[]

matchMedia.getListeners()

Returns a copy of the array of listeners for the passed media query

  • Arguments:

    • mediaQuery: string;
  • Returns: MediaQueryListener[]

matchMedia.clear()

Clears all registered media queries and their listeners

matchMedia.destroy()

Clears all registered media queries and their listeners, and destroys the implementation of window.matchMedia

License

MIT

Copyright (c) 2020-present, Kirill Dyakov

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].