dyakovk / jest-matchmedia-mock Goto Github PK
View Code? Open in Web Editor NEW๐ Mock for fully testing any media queries with Jest
License: MIT License
๐ Mock for fully testing any media queries with Jest
License: MIT License
Will this trigger change event when media query doesn't match, or it's just a static mock?
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.
I'm submitting a ...
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.
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.
Hi,
I think currentMediaQuery
should also be cleared. Otherwise, last query continues to exist until it's changed or destroyed.
jest-matchmedia-mock/src/matchmedia-mock.ts
Lines 131 to 133 in f45b33a
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.