Closed dan-klasson closed 5 years ago
@dan-klasson Have you tried using the device
prop? https://github.com/contra/react-responsive#forcing-a-device-with-the-device-prop
@contra I'm testing a component, and the initialization is in a custom hook. Besides, I don't like writing code in a specific way to get my tests to pass. Is there no other way?
@dan-klasson The device prop context stuff was built specifically for testing - https://github.com/contra/react-responsive#supplying-through-context
In your test suite you can mock the device using this, either at the top-level or per test if you want to test different device configurations. It does not require you to do anything in your component differently, and it isn't really any different than what you're trying to do with window.matchMedia.setConfig
except it actually works.
I'm trying to mock the width and screen dimensions with Jest. I'm importing my
matchMedia
mock as described in the Jest documentation.Looking at the
useMediaQuery_test.js
file there's this:I've tried adding that to my test but
setConfig
is undefined.How do I go about to mock this?