From 18fededae085b53b01e54a7ed27e32c2318e7cae Mon Sep 17 00:00:00 2001 From: Eric Lewis Date: Fri, 31 May 2019 19:53:21 -0700 Subject: [PATCH] add spec for I18nManager (#24908) Summary: Part of #24875. ## Changelog [General] [Added] - add TM spec for I18nManager Pull Request resolved: https://github.com/facebook/react-native/pull/24908 Reviewed By: fkgozali Differential Revision: D15395163 Pulled By: RSNara fbshipit-source-id: 8fd3a5a8ce5d0f74132efff4fae7224eab03405b --- Libraries/Inspector/resolveBoxStyle.js | 6 ++-- Libraries/Modal/Modal.js | 2 +- Libraries/ReactNative/I18nManager.js | 37 ++++++++++++---------- Libraries/ReactNative/NativeI18nManager.js | 26 +++++++++++++++ jest/setup.js | 9 ++++++ 5 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 Libraries/ReactNative/NativeI18nManager.js diff --git a/Libraries/Inspector/resolveBoxStyle.js b/Libraries/Inspector/resolveBoxStyle.js index 15dfaf3061d810..1f013ed064ba3c 100644 --- a/Libraries/Inspector/resolveBoxStyle.js +++ b/Libraries/Inspector/resolveBoxStyle.js @@ -69,7 +69,8 @@ function resolveBoxStyle( const styleForEnd = style[prefix + 'End']; if (styleForEnd != null) { - if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) { + const constants = I18nManager.getConstants(); + if (constants.isRTL && constants.doLeftAndRightSwapInRTL) { result.left = styleForEnd; } else { result.right = styleForEnd; @@ -78,7 +79,8 @@ function resolveBoxStyle( } const styleForStart = style[prefix + 'Start']; if (styleForStart != null) { - if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) { + const constants = I18nManager.getConstants(); + if (constants.isRTL && constants.doLeftAndRightSwapInRTL) { result.right = styleForStart; } else { result.left = styleForStart; diff --git a/Libraries/Modal/Modal.js b/Libraries/Modal/Modal.js index 40c03122bb2c14..314647a843646d 100644 --- a/Libraries/Modal/Modal.js +++ b/Libraries/Modal/Modal.js @@ -271,7 +271,7 @@ class Modal extends React.Component { } } -const side = I18nManager.isRTL ? 'right' : 'left'; +const side = I18nManager.getConstants().isRTL ? 'right' : 'left'; const styles = StyleSheet.create({ modal: { position: 'absolute', diff --git a/Libraries/ReactNative/I18nManager.js b/Libraries/ReactNative/I18nManager.js index da2767a842068f..e5fd02e7c13dab 100644 --- a/Libraries/ReactNative/I18nManager.js +++ b/Libraries/ReactNative/I18nManager.js @@ -9,21 +9,26 @@ */ 'use strict'; -type I18nManagerStatus = { - isRTL: boolean, - doLeftAndRightSwapInRTL: boolean, - allowRTL: (allowRTL: boolean) => {}, - forceRTL: (forceRTL: boolean) => {}, - swapLeftAndRightInRTL: (flipStyles: boolean) => {}, -}; +import NativeI18nManager from './NativeI18nManager'; -const I18nManager: I18nManagerStatus = require('../BatchedBridge/NativeModules') - .I18nManager || { - isRTL: false, - doLeftAndRightSwapInRTL: true, - allowRTL: () => {}, - forceRTL: () => {}, - swapLeftAndRightInRTL: () => {}, -}; +module.exports = { + getConstants: () => { + return NativeI18nManager.getConstants(); + }, + + allowRTL: (shouldAllow: boolean) => { + NativeI18nManager.allowRTL(shouldAllow); + }, -module.exports = I18nManager; + forceRTL: (shouldForce: boolean) => { + NativeI18nManager.forceRTL(shouldForce); + }, + + swapLeftAndRightInRTL: (flipStyles: boolean) => { + NativeI18nManager.swapLeftAndRightInRTL(flipStyles); + }, + + isRTL: NativeI18nManager.getConstants().isRTL, + doLeftAndRightSwapInRTL: NativeI18nManager.getConstants() + .doLeftAndRightSwapInRTL, +}; diff --git a/Libraries/ReactNative/NativeI18nManager.js b/Libraries/ReactNative/NativeI18nManager.js new file mode 100644 index 00000000000000..0220b2471ba486 --- /dev/null +++ b/Libraries/ReactNative/NativeI18nManager.js @@ -0,0 +1,26 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +'use strict'; + +import type {TurboModule} from '../TurboModule/RCTExport'; +import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +getConstants: () => {| + isRTL: boolean, + doLeftAndRightSwapInRTL: boolean, + |}; + allowRTL: (allowRTL: boolean) => void; + forceRTL: (forceRTL: boolean) => void; + swapLeftAndRightInRTL: (flipStyles: boolean) => void; +} + +export default TurboModuleRegistry.getEnforcing('I18nManager'); diff --git a/jest/setup.js b/jest/setup.js index d85849066c2d82..cdd146462fe7c0 100644 --- a/jest/setup.js +++ b/jest/setup.js @@ -312,6 +312,15 @@ jest addListener: jest.fn(), removeListeners: jest.fn(), }, + I18nManager: { + allowRTL: jest.fn(), + forceRTL: jest.fn(), + swapLeftAndRightInRTL: jest.fn(), + getConstants: () => ({ + isRTL: false, + doLeftAndRightSwapInRTL: true, + }), + }, })) .mock('../Libraries/ReactNative/requireNativeComponent', () => { const React = require('react');