mirror of
https://github.com/prometheus/prometheus
synced 2024-12-27 17:13:22 +00:00
add global mock for the matchMedia function
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
This commit is contained in:
parent
2cfdaf915f
commit
e1d8891a73
@ -9,7 +9,7 @@ const useMedia = (query: string): boolean => {
|
||||
const handler = () => setMatches(mediaQuery.matches);
|
||||
mediaQuery.addEventListener('change', handler);
|
||||
return () => mediaQuery.removeEventListener('change', handler);
|
||||
}, []);
|
||||
}, [mediaQuery]);
|
||||
|
||||
return matches;
|
||||
};
|
||||
|
@ -10,6 +10,22 @@ const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;
|
||||
customGlobal.fetch = require('jest-fetch-mock');
|
||||
customGlobal.fetchMock = customGlobal.fetch;
|
||||
|
||||
// https://stackoverflow.com/questions/39830580/jest-test-fails-typeerror-window-matchmedia-is-not-a-function
|
||||
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
|
||||
Object.defineProperty(window, 'matchMedia', {
|
||||
writable: true,
|
||||
value: jest.fn().mockImplementation((query) => ({
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
addListener: jest.fn(), // Deprecated
|
||||
removeListener: jest.fn(), // Deprecated
|
||||
addEventListener: jest.fn(),
|
||||
removeEventListener: jest.fn(),
|
||||
dispatchEvent: jest.fn(),
|
||||
})),
|
||||
});
|
||||
|
||||
// CodeMirror in the expression input requires this DOM API. When we upgrade react-scripts
|
||||
// and the associated Jest deps, hopefully this won't be needed anymore.
|
||||
document.getSelection = function () {
|
||||
|
Loading…
Reference in New Issue
Block a user