/
githubmr
/
facebook-react-native
Обзор
Документация
Войти
/
githubmr
/
facebook-react-native
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
packages/react-native/Libraries/Animated/__tests__/AnimatedValue-test.js
231 строка
7 KB
Tim Yung
RN: Flowify `packages/react-native` Mocks & Tests (#51794)
04 июн 2025, 22:03
04 июн 2025, 22:03
5ce99e7
Код
Авторство
О чём код?
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow strict-local * @format */ describe('AnimatedValue', () => { let NativeAnimatedHelper; let AnimatedValue; function createNativeAnimatedValue(): AnimatedValue { return new AnimatedValue(0, {useNativeDriver: true}); } function emitMockUpdate( node: AnimatedValue, mockValue: number, mockOffset: number, ): void { const nativeTag = node.__nativeTag; expect(nativeTag).not.toBe(undefined); NativeAnimatedHelper.nativeEventEmitter.emit('onAnimatedValueUpdate', { tag: nativeTag, value: mockValue, offset: mockOffset, }); } beforeEach(() => { jest.resetModules(); jest.mock('../NativeAnimatedTurboModule', () => ({ __esModule: true, default: { addListener: jest.fn(), createAnimatedNode: jest.fn(), dropAnimatedNode: jest.fn(), removeListeners: jest.fn(), startListeningToAnimatedNodeValue: jest.fn(), stopListeningToAnimatedNodeValue: jest.fn(), extractAnimatedNodeOffset: jest.fn(), // ... }, })); NativeAnimatedHelper = require('../../../src/private/animated/NativeAnimatedHelper').default; AnimatedValue = require('../nodes/AnimatedValue').default; jest.spyOn(NativeAnimatedHelper.API, 'createAnimatedNode'); jest.spyOn(NativeAnimatedHelper.API, 'dropAnimatedNode'); jest.spyOn(NativeAnimatedHelper.API, 'startListeningToAnimatedNodeValue'); jest.spyOn(NativeAnimatedHelper.API, 'setWaitingForIdentifier'); jest.spyOn(NativeAnimatedHelper.API, 'unsetWaitingForIdentifier'); }); it('emits update events for listeners added', () => { const callback = jest.fn(); const node = createNativeAnimatedValue(); node.__attach(); const id = node.addListener(callback); const nativeTag = node.__nativeTag; expect(nativeTag).not.toBe(undefined); emitMockUpdate(node, 123, 50); expect(callback).toBeCalledTimes(1); node.removeListener(id); emitMockUpdate(node, 456, 60); expect(callback).toBeCalledTimes(1); }); it('creates a native node when adding a listener', () => { const node = createNativeAnimatedValue(); node.__attach(); expect(NativeAnimatedHelper.API.createAnimatedNode).not.toBeCalled(); const id = node.addListener(jest.fn()); node.removeListener(id); expect(NativeAnimatedHelper.API.createAnimatedNode).toBeCalledTimes(1); }); it('drops a created native node on detach', () => { const node = createNativeAnimatedValue(); node.__attach(); expect(NativeAnimatedHelper.API.createAnimatedNode).toBeCalledTimes(0); node.addListener(jest.fn()); expect(NativeAnimatedHelper.API.createAnimatedNode).toBeCalledTimes(1); expect(NativeAnimatedHelper.API.dropAnimatedNode).toBeCalledTimes(0); node.__detach(); expect(NativeAnimatedHelper.API.createAnimatedNode).toBeCalledTimes(1); expect(NativeAnimatedHelper.API.dropAnimatedNode).toBeCalledTimes(1); }); it('emits update events for listeners added after re-attach', () => { const callbackA = jest.fn(); const node = createNativeAnimatedValue(); node.__attach(); node.addListener(callbackA); emitMockUpdate(node, 123, 50); expect(callbackA).toBeCalledTimes(1); node.__detach(); expect(NativeAnimatedHelper.API.createAnimatedNode).toBeCalledTimes(1); const callbackB = jest.fn(); node.__attach(); node.addListener(callbackB); emitMockUpdate(node, 456, 60); expect(callbackA).toBeCalledTimes(1); expect(callbackB).toBeCalledTimes(1); }); describe('when NativeAnimatedHelper.API.startListeningToAnimatedNodeValue is called', () => { it('starts listening when addListener is called after __makeNative', () => { const node = new AnimatedValue(0, {useNativeDriver: false}); node.__makeNative(); expect( NativeAnimatedHelper.API.startListeningToAnimatedNodeValue, ).toBeCalledTimes(0); node.addListener(() => {}); expect( NativeAnimatedHelper.API.startListeningToAnimatedNodeValue, ).toBeCalledTimes(1); }); it('starts listening when __makeNative is called after addListener', () => { const node = new AnimatedValue(0, {useNativeDriver: false}); node.addListener(() => {}); expect( NativeAnimatedHelper.API.startListeningToAnimatedNodeValue, ).toBeCalledTimes(0); node.__makeNative(); expect( NativeAnimatedHelper.API.startListeningToAnimatedNodeValue, ).toBeCalledTimes(1); }); it('does not start listening to node when not native', () => { const node = new AnimatedValue(0, {useNativeDriver: false}); node.__attach(); expect( NativeAnimatedHelper.API.startListeningToAnimatedNodeValue, ).toBeCalledTimes(0); node.addListener(() => {}); expect( NativeAnimatedHelper.API.startListeningToAnimatedNodeValue, ).toBeCalledTimes(0); }); }); describe('when extractOffset is called', () => { it('flushes changes to native immediately when native', () => { const node = new AnimatedValue(0, {useNativeDriver: true}); expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes( 0, ); expect( NativeAnimatedHelper.API.unsetWaitingForIdentifier, ).toBeCalledTimes(0); node.extractOffset(); expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes( 1, ); expect( NativeAnimatedHelper.API.unsetWaitingForIdentifier, ).toBeCalledTimes(1); }); it('does not flush changes when not native', () => { const node = new AnimatedValue(0, {useNativeDriver: false}); node.extractOffset(); expect(NativeAnimatedHelper.API.setWaitingForIdentifier).toBeCalledTimes( 0, ); expect( NativeAnimatedHelper.API.unsetWaitingForIdentifier, ).toBeCalledTimes(0); }); }); describe('when receiving an update event', () => { it('calls __onAnimatedValueUpdateReceived with value and offset', () => { const callback = jest.fn(); const node = createNativeAnimatedValue(); node.__attach(); node.addListener(callback); const nativeTag = node.__nativeTag; expect(nativeTag).not.toBe(undefined); emitMockUpdate(node, 123, 50); const spy = jest.spyOn(node, '__onAnimatedValueUpdateReceived'); const mockValue = 100; const mockOffset = 50; emitMockUpdate(node, mockValue, mockOffset); expect(spy).toHaveBeenCalledWith(mockValue, mockOffset); spy.mockRestore(); }); }); });