Skip to content

Commit

Permalink
Only show warning when overwrite existing preprocessor (#34479)
Browse files Browse the repository at this point in the history
Summary:
from the original design of `StyleSheet.setStyleAttributePreprocessor()` in #11138, the overwriting warning shows when the existing preprocess is be overwritten.  the behavior changes from 33b385825c72. This PR revises the logic back to original design.

## Changelog

[Internal] [Fixed] - Show warning only when overwriting existing preprocessor in `StyleSheet.setStyleAttributePreprocessor()`

Pull Request resolved: #34479

Test Plan:
Unit Test

```
 PASS  Libraries/StyleSheet/__tests__/StyleSheet-test.js
  setStyleAttributePreprocessor
    ✓ should not show warning when set preprocessor first time (2 ms)
    ✓ should show warning when overwrite the preprocessor (1 ms)
```

Reviewed By: dmitryrykun

Differential Revision: D38940676

Pulled By: cipolleschi

fbshipit-source-id: 80cf30fff62f4a02c17f7f42b3260a6011d5fc82
  • Loading branch information
Kudo authored and Dmitry Rykun committed Sep 14, 2022
1 parent 521c712 commit 6d50f7e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Libraries/StyleSheet/StyleSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,12 @@ module.exports = {
return;
}

if (__DEV__ && typeof value.process === 'function') {
if (
__DEV__ &&
typeof value.process === 'function' &&
typeof ReactNativeStyleAttributes[property]?.process === 'function' &&
value.process !== ReactNativeStyleAttributes[property]?.process
) {
console.warn(`Overwriting ${property} style attribute preprocessor`);
}

Expand Down
48 changes: 48 additions & 0 deletions Libraries/StyleSheet/__tests__/StyleSheet-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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.
*
* @emails oncall+react_native
* @format
*/

import {setStyleAttributePreprocessor} from '../StyleSheet';

describe(setStyleAttributePreprocessor, () => {
const originalConsoleWarn = console.warn;

beforeEach(() => {
jest.resetModules();
console.warn = jest.fn();
});

afterEach(() => {
console.warn = originalConsoleWarn;
});

it('should not show warning when set preprocessor first time', () => {
const spyConsole = jest.spyOn(global.console, 'warn');
setStyleAttributePreprocessor(
'fontFamily',
(fontFamily: string) => fontFamily,
);
expect(spyConsole).not.toHaveBeenCalled();
});

it('should show warning when overwrite the preprocessor', () => {
const spyConsole = jest.spyOn(global.console, 'warn');
setStyleAttributePreprocessor(
'fontFamily',
(fontFamily: string) => fontFamily,
);
setStyleAttributePreprocessor(
'fontFamily',
(fontFamily: string) => `Scoped-${fontFamily}`,
);
expect(spyConsole).toHaveBeenCalledWith(
'Overwriting fontFamily style attribute preprocessor',
);
});
});

0 comments on commit 6d50f7e

Please sign in to comment.