Skip to content

Commit f89f40f

Browse files
rubennortemeta-codesync[bot]
authored andcommitted
Migrate remaining StyleSheet Jest tests to Fantom
Summary: Migrates the remaining StyleSheet unit tests from regular Jest (`-test.js`) to Fantom (`-itest.js`): `normalizeColor`, `processColor`, `processColorArray`, `processBackgroundImage`, and `StyleSheet`. These run on the React Native client at runtime, so they now run on Hermes in the real runtime. Adaptations (no coverage weakened): - Replaced `jest.spyOn(console, ...)` with manual save/replace/restore of the `console` method (Fantom does not provide `jest.spyOn`), both to assert and to suppress expected output. - Replaced the module-mock delegation check in `normalizeColor` (which relied on `jest.mock`) with a behavioral assertion on the real implementation. - These tests run on the Android runtime under Fantom (Jest defaulted to iOS); platform-specific requires were moved next to their platform branches where needed. Changelog: [Internal] Differential Revision: D108759085
1 parent e0f1f83 commit f89f40f

5 files changed

Lines changed: 36 additions & 25 deletions

File tree

packages/react-native/Libraries/StyleSheet/__tests__/StyleSheet-test.js renamed to packages/react-native/Libraries/StyleSheet/__tests__/StyleSheet-itest.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,36 @@
88
* @format
99
*/
1010

11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1112
import StyleSheet from '../StyleSheet';
1213

1314
const setStyleAttributePreprocessor = StyleSheet.setStyleAttributePreprocessor;
1415

15-
describe(setStyleAttributePreprocessor, () => {
16-
beforeEach(() => {
17-
jest.resetModules();
18-
jest.spyOn(console, 'warn').mockImplementation(() => {});
19-
});
16+
describe('setStyleAttributePreprocessor', () => {
17+
const originalConsoleWarn = console.warn;
2018

2119
afterEach(() => {
22-
jest.restoreAllMocks();
20+
// $FlowExpectedError[cannot-write]
21+
console.warn = originalConsoleWarn;
2322
});
2423

2524
it('should not show warning when set preprocessor first time', () => {
26-
const spyConsole = jest.spyOn(global.console, 'warn');
25+
const mockConsoleWarn = jest.fn();
26+
// $FlowExpectedError[cannot-write]
27+
console.warn = mockConsoleWarn;
28+
2729
setStyleAttributePreprocessor(
2830
'fontFamily',
2931
(fontFamily: string) => fontFamily,
3032
);
31-
expect(spyConsole).not.toHaveBeenCalled();
33+
expect(mockConsoleWarn).not.toHaveBeenCalled();
3234
});
3335

3436
it('should show warning when overwrite the preprocessor', () => {
35-
const spyConsole = jest.spyOn(global.console, 'warn');
37+
const mockConsoleWarn = jest.fn();
38+
// $FlowExpectedError[cannot-write]
39+
console.warn = mockConsoleWarn;
40+
3641
setStyleAttributePreprocessor(
3742
'fontFamily',
3843
(fontFamily: string) => fontFamily,
@@ -41,7 +46,7 @@ describe(setStyleAttributePreprocessor, () => {
4146
'fontFamily',
4247
(fontFamily: string) => `Scoped-${fontFamily}`,
4348
);
44-
expect(spyConsole).toHaveBeenCalledWith(
49+
expect(mockConsoleWarn).toHaveBeenCalledWith(
4550
'Overwriting fontFamily style attribute preprocessor',
4651
);
4752
});

packages/react-native/Libraries/StyleSheet/__tests__/normalizeColor-test.js renamed to packages/react-native/Libraries/StyleSheet/__tests__/normalizeColor-itest.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
* @format
99
*/
1010

11-
'use strict';
11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

1313
const {OS} = require('../../Utilities/Platform').default;
1414
const normalizeColor = require('../normalizeColor').default;
1515

1616
it('forwards calls to @react-native/normalize-colors', () => {
17-
jest.resetModules().mock('@react-native/normalize-colors', () => jest.fn());
18-
19-
expect(require('../normalizeColor').default('#abc')).not.toBe(null);
20-
expect(require('@react-native/normalize-colors')).toBeCalled();
17+
// normalizeColor delegates string and number inputs to
18+
// @react-native/normalize-colors, which returns a 0xrrggbbaa integer.
19+
expect(normalizeColor('#abcdef')).toBe(0xabcdefff);
2120
});
2221

2322
describe('iOS', () => {

packages/react-native/Libraries/StyleSheet/__tests__/processBackgroundImage-test.js renamed to packages/react-native/Libraries/StyleSheet/__tests__/processBackgroundImage-itest.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @format
99
*/
1010

11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
12+
1113
import type {BackgroundImageValue} from '../StyleSheetTypes';
1214

1315
import processBackgroundImage from '../processBackgroundImage';

packages/react-native/Libraries/StyleSheet/__tests__/processColor-test.js renamed to packages/react-native/Libraries/StyleSheet/__tests__/processColor-itest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @format
99
*/
1010

11-
'use strict';
11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

1313
const {OS} = require('../../Utilities/Platform').default;
1414
const PlatformColorAndroid =

packages/react-native/Libraries/StyleSheet/__tests__/processColorArray-test.js renamed to packages/react-native/Libraries/StyleSheet/__tests__/processColorArray-itest.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @format
99
*/
1010

11-
'use strict';
11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

1313
const {OS} = require('../../Utilities/Platform').default;
1414
const PlatformColorAndroid =
@@ -28,6 +28,13 @@ const platformSpecific =
2828
: x => x;
2929

3030
describe('processColorArray', () => {
31+
const originalConsoleError = console.error;
32+
33+
afterEach(() => {
34+
// $FlowExpectedError[cannot-write]
35+
console.error = originalConsoleError;
36+
});
37+
3138
describe('predefined color name array', () => {
3239
it('should convert array of color name type', () => {
3340
const colorFromStringArray = processColorArray(['red', 'white', 'black']);
@@ -46,8 +53,7 @@ describe('processColorArray', () => {
4653
const expectedIntArray = [0xff0a141e, 0xff1e140a, 0xff3296fa].map(
4754
platformSpecific,
4855
);
49-
// $FlowFixMe[incompatible-type]
50-
expect(colorFromRGBArray).toEqual(platformSpecific(expectedIntArray));
56+
expect(colorFromRGBArray).toEqual(expectedIntArray);
5157
});
5258

5359
it('should convert array of color type hsl(x, y%, z%)', () => {
@@ -59,8 +65,7 @@ describe('processColorArray', () => {
5965
const expectedIntArray = [0xffdb3dac, 0xff234786, 0xff1e541d].map(
6066
platformSpecific,
6167
);
62-
// $FlowFixMe[incompatible-type]
63-
expect(colorFromHSLArray).toEqual(platformSpecific(expectedIntArray));
68+
expect(colorFromHSLArray).toEqual(expectedIntArray);
6469
});
6570

6671
it('should return null if no array', () => {
@@ -69,7 +74,9 @@ describe('processColorArray', () => {
6974
});
7075

7176
it('converts invalid colors to transparent', () => {
72-
const spy = jest.spyOn(console, 'error').mockReturnValue(undefined);
77+
const mockConsoleError = jest.fn();
78+
// $FlowExpectedError[cannot-write]
79+
console.error = mockConsoleError;
7380

7481
const colors = ['red', '???', null, undefined, false];
7582
// $FlowExpectedError[incompatible-type]
@@ -80,13 +87,11 @@ describe('processColorArray', () => {
8087
expect(colorFromStringArray).toEqual(expectedIntArray);
8188

8289
for (const color of colors.slice(1)) {
83-
expect(spy).toHaveBeenCalledWith(
90+
expect(mockConsoleError).toHaveBeenCalledWith(
8491
'Invalid value in color array:',
8592
color,
8693
);
8794
}
88-
89-
spy.mockRestore();
9095
});
9196
});
9297

0 commit comments

Comments
 (0)