Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ImageLoader.getSize jest mock #34653

Closed
wants to merge 1 commit into from

Conversation

elliottkember
Copy link
Contributor

@elliottkember elliottkember commented Sep 10, 2022

getSize should resolve with an array of [width, height] but this mock resolves with { width, height }.

It should be ReadOnlyArray<number> instead of {width: number, height: number}

The native image loader call is here:

  +getSize: (uri: string) => Promise<$ReadOnlyArray<number>>;

but in the jest setup file:

  getSize: jest.fn(url => Promise.resolve({width: 320, height: 240})),

Summary

My tests were failing on Image.getSize() - TypeError: Invalid attempt to destructure non-iterable instance.

I managed to trace this down to this object being returned by the Jest mock - looks like it's returning a size object instead of a dimensions array.

Workaround

If you are hitting this issue, you can work around this mock by using:

ReactNative.NativeModules.ImageLoader.getSize = jest.fn((_) => Promise.resolve([320, 240]));

Changelog

[JavaScript] [Changed]: Changed the mocked return value of ImageLoader.getSize to be [320, 240] instead of { width: 320, height: 240 }

Test Plan

TBD? I think a test with Image.getSize(path) will cover it. That's where I hit the error with the ios-specific imageLoader's getSize method.

`getSize` should resolve with an array of `[width, height]` but this mock resolves with `{ width, height }`.
@facebook-github-bot
Copy link
Contributor

Hi @elliottkember!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,643,504 +471
android hermes armeabi-v7a 7,055,602 +432
android hermes x86 7,945,085 +500
android hermes x86_64 7,917,120 +416
android jsc arm64-v8a 9,516,021 +0
android jsc armeabi-v7a 8,291,634 +0
android jsc x86 9,455,349 +0
android jsc x86_64 10,046,429 +0

Base commit: 4e70376
Branch: main

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Sep 10, 2022
@elliottkember elliottkember changed the title Fix jest mock for image getSize Fix ImageLoader.getSize jest mock Sep 10, 2022
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 4e70376
Branch: main

@facebook-github-bot
Copy link
Contributor

@NickGerleman has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @elliottkember in 7be829f.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Sep 12, 2022
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
Summary:
`getSize` should resolve with an array of `[width, height]` but this mock resolves with `{ width, height }`.

It should be `ReadOnlyArray<number>` instead of `{width: number, height: number}`

The native image loader call is [here](https://github.com/facebook/react-native/blob/main/Libraries/Image/NativeImageLoaderIOS.js#L18):
```js
  +getSize: (uri: string) => Promise<$ReadOnlyArray<number>>;
```

but in the [jest setup file](https://github.com/facebook/react-native/blob/main/jest/setup.js):
```js
  getSize: jest.fn(url => Promise.resolve({width: 320, height: 240})),
```

My tests were failing on `Image.getSize()` - `TypeError: Invalid attempt to destructure non-iterable instance.`

I managed to trace this down to this object being returned by the Jest mock - looks like it's returning a size object instead of a dimensions array.

## Workaround

If you are hitting this issue, you can work around this mock by using:

```js
ReactNative.NativeModules.ImageLoader.getSize = jest.fn((_) => Promise.resolve([320, 240]));
```

## Changelog

[JavaScript] [Changed]: Changed the mocked return value of `ImageLoader.getSize` to be `[320, 240]` instead of `{ width: 320, height: 240 }`

Pull Request resolved: facebook#34653

Test Plan: TBD? I think a test with `Image.getSize(path)` will cover it. That's where I hit the error with the ios-specific imageLoader's getSize method.

Reviewed By: robhogan

Differential Revision: D39413522

Pulled By: NickGerleman

fbshipit-source-id: 7f18d7acde0cf94da0b4aec8fe2d0cad3fb0cc55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants