Skip to content

Commit

Permalink
(jest/setup): Add missing methods to Image mock (#36996)
Browse files Browse the repository at this point in the history
Summary:
Fixes #35518

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[INTERNAL] [FIXED] - Add missing methods to Image mock

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: #36996

Test Plan:
I used the example provided in the issue:

```javascript
import { useEffect } from "react"
import { Image } from "react-native"
import { render } from 'testing-library/react-native';

const MyComponent = () => {
    useEffect(() => {
        Image.getSize('uri', (width, height) => {
            console.log({width, height})
        })
    }, [])

    return null
}

describe('Foo', () => {
    it('something', () => {
        render(<MyComponent/>)
    })
})
```

Before the fix:
<img width="779" alt="Capture d’écran 2023-04-20 à 09 31 48" src="https://user-images.githubusercontent.com/17070498/233293066-e8673755-217f-48b4-a856-b9356ec3624e.png">

And after the fix:
<img width="346" alt="Capture d’écran 2023-04-20 à 09 32 40" src="https://user-images.githubusercontent.com/17070498/233293261-b6d02307-e67a-444f-a2de-e31eae32c0f1.png">

Reviewed By: cortinico

Differential Revision: D45179320

Pulled By: rshest

fbshipit-source-id: 323d04cdf720e23690e44f6c8621289e74b95c17
  • Loading branch information
Antoine Doubovetzky authored and facebook-github-bot committed Apr 24, 2023
1 parent e66bdf0 commit 4da2013
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/react-native/jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,17 @@ jest
Constants: {},
},
}))
.mock('../Libraries/Image/Image', () =>
mockComponent('../Libraries/Image/Image'),
)
.mock('../Libraries/Image/Image', () => {
const Image = mockComponent('../Libraries/Image/Image');
Image.getSize = jest.fn();
Image.getSizeWithHeaders = jest.fn();
Image.prefetch = jest.fn();
Image.prefetchWithMetadata = jest.fn();
Image.queryCache = jest.fn();
Image.resolveAssetSource = jest.fn();

return Image;
})
.mock('../Libraries/Text/Text', () =>
mockComponent('../Libraries/Text/Text', MockNativeMethods),
)
Expand Down

0 comments on commit 4da2013

Please sign in to comment.