i have a footer that has a <p>
element when it's greater than 576px but it's NOT rendered (display: none) if less than 576px.
Works perfect in browser.
Testing it with Jest / Enzyme is very difficult though.
I have a function:
const resizeWindow = (x, y) => {
// @ts-ignore
window.innerWidth = x;
// @ts-ignore
window.innerHeight = y;
window.dispatchEvent(new Event('resize'));
};
This is then called in the test:
...test on desktop...
...test on mobile:-
it('should NOT show <p> tag if width < 576', () => {
resizeWindow(300, 600);
const numberOfPtags = mountedComponent.find('p').length;
expect(numberOfPtags).toBe(0)
});
But it always comes through as 1
.
Even though the code would make it display: none;
with those dimensions.
I found a way of logging out style and it had it as display: block;
which is bizarre as I've only used inline
and none
.
I also logged out the window.innerWidth dimensions and they came out fine, so it obviously is changing them.
What am I missing here?
-- UPDATED --
screenshot of tests showing global.innerWidth
instead of window, checking the width has definitely changed, and still the
tag remains. No idea what i'm doing wrong.
Thought it might be an async issue (eg waiting for the virtual window to resize, put a setTimeout in and the expect inside it... Jest totally skipped the setTimeout all together
to no longer be visible, but it remains.
– Aid19801 Aug 07 '20 at 05:21