Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Jest mock dimensions not changing

$
0
0

I am doing testing for the first time so I am pretty much sure that I am doing something wrong.

I was writing test case and my component does this internally.

const {width, height} = Dimensions.get('window')

For my test case, I was considering iPhone 11 which have dimensions as width: 414, height:896, and I want this consistent across all the test cases.

React native testing library while testing sets width as 750 and height as 1334.

I want to change it to iPhone 11 dimensions, I searched web and found articles which uses jest.mock to change function.

So I did something like this

it('renders correctly', () => {     jest.mock("Dimensions", () => ({        get: jest.fn().mockReturnValue({ width: 414, height:896 }),     }))     const {getByTestId} = render(<Home />)

Home component have console.log(width, height) but it is still giving width as 750 and height as 1334 (because of which my test case is failing).

How can I fix it?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>