From 8d6ec66b480ef57f324cd1e68e05f976ea163ed5 Mon Sep 17 00:00:00 2001 From: Kyle Rosenberg Date: Wed, 31 Jul 2024 12:35:41 -0700 Subject: [PATCH] Update AccessibilityInfo mocks to return promises (#45825) Summary: Many `AccessibilityInfo` functions (`isReduceMotionEnabled`, `isBoldTextEnabled`, etc.) return promises, but the mocked versions of them in jest/setup.js aren't returning promises. All of these functions live in [packages/react-native/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js](https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js), where you can verify their return types are Promises. When using `react-native/jest-preset` and running tests that utilize one of these functions, you'll get an error: ``` AccessibilityInfo.isReduceMotionEnabled().then() is not a function ``` https://github.com/facebook/react-native/pull/29381 was opened in 2020 but closed after becoming stale. My PR is nearly identical but covers additional Promise-returning functions that have been added to `AccessibilityInfo` since then. ## Changelog: [GENERAL] [FIXED] - Update the react-native/jest-preset mock of AccessibilityInfo to better match its API Pull Request resolved: https://github.com/facebook/react-native/pull/45825 Test Plan: I've tested by making the change locally in my project's `node_modules/react-native/jest/setup.js` file and confirming that I no longer get an error when running this test: ``` it("should pass", async () => { await AccessibilityInfo.isReduceMotionEnabled().then(enabled => { expect(enabled).toBe(false); }); }); ``` Before: ``` TypeError: Cannot read properties of undefined (reading 'then') 16 | 17 | it.only("should pass", async () => { > 18 | await AccessibilityInfo.isReduceMotionEnabled().then(enabled => { | ^ 19 | expect(enabled).toBe(false); 20 | }); 21 | }); ``` After: No type error, and test passes. Reviewed By: robhogan Differential Revision: D60519836 Pulled By: tdn120 fbshipit-source-id: 24fc77b0f9693e131686a0a45b81fbd33ff65f01 --- packages/react-native/jest/setup.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/react-native/jest/setup.js b/packages/react-native/jest/setup.js index 83f701e1845..7fbfa8052dd 100644 --- a/packages/react-native/jest/setup.js +++ b/packages/react-native/jest/setup.js @@ -146,17 +146,17 @@ jest remove: jest.fn(), })), announceForAccessibility: jest.fn(), - isAccessibilityServiceEnabled: jest.fn(), - isBoldTextEnabled: jest.fn(), - isGrayscaleEnabled: jest.fn(), - isInvertColorsEnabled: jest.fn(), - isReduceMotionEnabled: jest.fn(), - prefersCrossFadeTransitions: jest.fn(), - isReduceTransparencyEnabled: jest.fn(), + isAccessibilityServiceEnabled: jest.fn(() => Promise.resolve(false)), + isBoldTextEnabled: jest.fn(() => Promise.resolve(false)), + isGrayscaleEnabled: jest.fn(() => Promise.resolve(false)), + isInvertColorsEnabled: jest.fn(() => Promise.resolve(false)), + isReduceMotionEnabled: jest.fn(() => Promise.resolve(false)), + prefersCrossFadeTransitions: jest.fn(() => Promise.resolve(false)), + isReduceTransparencyEnabled: jest.fn(() => Promise.resolve(false)), isScreenReaderEnabled: jest.fn(() => Promise.resolve(false)), setAccessibilityFocus: jest.fn(), sendAccessibilityEvent: jest.fn(), - getRecommendedTimeoutMillis: jest.fn(), + getRecommendedTimeoutMillis: jest.fn(() => Promise.resolve(false)), }, })) .mock('../Libraries/Components/Clipboard/Clipboard', () => ({