Coder Social home page Coder Social logo

jest-matchmedia-mock's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jest-matchmedia-mock's Issues

Not working with Jest 27.5.1

I have this test file:

import { renderHook } from '@testing-library/react-hooks';
import matchMedia from '@/common/testUtils';
import { useMatchMedia } from './../useMatchMedia';

describe('useMatchMedia', () => {
    it('returns boolean', () => {
        const { result } = renderHook(() => useMatchMedia('(max-width: 575px)'));
        expect(result.current).toBe(expect.any(Boolean));
    });
});

with this hook

import { useState, useEffect } from 'react';

export const useMatchMedia = (query: string) => {
    const [matches, setMatches] = useState(false);

    useEffect(() => {
        const media = window.matchMedia(query);
        if (media.matches !== matches) {
            setMatches(media.matches);
        }
        const listener = () => setMatches(media.matches);
        window.addEventListener('resize', listener);
        return () => window.removeEventListener('resize', listener);
    }, [matches, query]);

    return matches;
};

and have this error

TypeError: window.matchMedia is not a function

Thanks.

Support ESM

I'm submitting a ...

  • Bug report
  • Feature request
  • Docs update

What is the current behavior/state of the project?
The project does not support ESM build because of an incorrect TS type definition and yields the following error

import MatchMediaMock from "jest-matchmedia-mock";
new MatchMediaMock();
// ts-error: Type 'typeof import(".../node_modules/jest-matchmedia-mock/dist/matchmedia-mock")' has no construct signatures.

What is the desired behavior?
Having the TS type definition reflect the runtime behavior when imported in an ESM module.

Support multiple queries in "useMediaQuery"

Right now it's imposible to specify multiple queries that will match.
Would be great to have an ability to specify a list, otherwise some cases are just impossible to test.

How to simulate changes in the "matches" property?

Sorry I'm not sure if this is a deficiency in the component or just I don't understand how to use it.

In my application I have a number of media queries, each query has a listener, and the listener is fired when the "matches" property changes from true to false or vice versa. What I want to simulate is any specific media query changing from matched to not matched, so I want my test to do something like this:

const mediaQuery1 = '(prefers-color-scheme: light)';
const mediaQuery2 = '(min-width: 640px)';
const listener1 = jest.fn();
const listener2 = jest.fn();
const mql1 = window.matchMedia(mediaQuery1);
const mql2 = window.matchMedia(mediaQuery2);
mql1.addListener(listener1);
mql2.addListener(listener2);

matchMedia.useMediaQuery(mediaQuery1, true);
matchMedia.useMediaQuery(mediaQuery2, true);
expect(listener1).toBeCalledTimes(1);
expect(listener2).toBeCalledTimes(1);
listener1.mockReset();
listener2.mockReset();
matchMedia.useMediaQuery(mediaQuery2, false);
expect(listener1).toBeCalledTimes(0);
expect(listener2).toBeCalledTimes(1);

However, it seems that jest-matchmedia-mock has the concept of "current media query" which is the only one that's matched. When I call useMediaQuery() to change the current query, listeners attached to other media queries are not fired. Is there any way to achieve the above result?

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.