react-native/jest
Maksym Rusynyk 3088096684 Return request id in jest mock for requestAnimationFrame
Summary:
`Jest` allows to use fake timers and according to https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame the return value should be `A long integer value, the request id, that uniquely identifies the entry in the callback list`. This allows to use `cancelAnimationFrame`.

In current implementation of `jest/setup.js`, the return value is undefined. Therefore it's not possible to cancel the animation frame request.

```
let id = null;
const registerCallback = (callback) => {
  clearCallback();
  id = requestAnimationFrame(() => {
    id = null;
    callback();
  });
};
const clearCallback = () => {
  if (null !== id) {
    cancelAnimationFrame(id);
    id = null;
  }
};
```

```
jest.useFakeTimers();

const callback = jest.fn();
registerCallback(callback);
clearCallback();

jest.runAllTimers();
expect(callback).toHaveBeenCalledTimes(0); // Will be error in current implementation, since nothing is cleared. And test will pass, after MR is merged
```

This is fake example, but the real usage is when the animation frame request should be removed on `ComponentWillUnmount`.

 [JEST] [BUGFIX] [requestAnimationFrame] - return request id
Closes https://github.com/facebook/react-native/pull/16367

Differential Revision: D6060578

Pulled By: ericnakagawa

fbshipit-source-id: c785a3380f5e267b48ae16fcf34dbbf95fa54178
2017-10-14 14:03:16 -07:00
..
.eslintrc Disallow trailing commas in react-native-github 2017-08-17 16:20:04 -07:00
assetFileTransformer.js Fix test for RN asset 2017-09-07 01:55:56 -07:00
mockComponent.js Revert D5409825: [RN] Convert easy files to Prettier 2017-07-12 19:23:58 -07:00
preprocessor.js @allow-large-files Flow v0.54.0 2017-09-06 03:33:43 -07:00
setup.js Return request id in jest mock for requestAnimationFrame 2017-10-14 14:03:16 -07:00