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
This commit is contained in:
parent
720a99a890
commit
3088096684
|
@ -20,7 +20,7 @@ global.Promise = require.requireActual('promise');
|
|||
global.regeneratorRuntime = require.requireActual('regenerator-runtime/runtime');
|
||||
|
||||
global.requestAnimationFrame = function(callback) {
|
||||
setTimeout(callback, 0);
|
||||
return setTimeout(callback, 0);
|
||||
};
|
||||
global.cancelAnimationFrame = function(id) {
|
||||
clearTimeout(id);
|
||||
|
|
Loading…
Reference in New Issue