Skip to content

Commit 176c66a

Browse files
amgleitmanAdam Gleitman
andauthored
Fix Platform.select on Mac (#2374)
* Revert accidental change to Platform.select in 0.77 merge * Add extra unit tests * nit: fix formatting --------- Co-authored-by: Adam Gleitman <[email protected]>
1 parent 5c43268 commit 176c66a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/react-native/Libraries/Utilities/Platform.macos.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ const Platform: PlatformType = {
8484
},
8585
select: <T>(spec: PlatformSelectSpec<T>): T =>
8686
// $FlowFixMe[incompatible-return]
87-
'ios' in spec ? spec.macos : 'native' in spec ? spec.native : spec.default,
87+
'macos' in spec
88+
? spec.macos
89+
: 'native' in spec
90+
? spec.native
91+
: spec.default,
8892
};
8993

9094
module.exports = Platform;

packages/react-native/Libraries/Utilities/__tests__/Platform-test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,64 @@
1212

1313
const PlatformAndroid = require('../Platform.android');
1414
const PlatformIOS = require('../Platform.ios');
15+
const PlatformMacOS = require('../Platform.macos'); // [macOS]
1516

1617
describe('Platform', () => {
1718
describe('OS', () => {
1819
it('should have correct value', () => {
1920
expect(PlatformIOS.OS).toEqual('ios');
2021
expect(PlatformAndroid.OS).toEqual('android');
22+
expect(PlatformMacOS.OS).toEqual('macos'); // [macOS]
2123
});
2224
});
2325

2426
describe('select', () => {
2527
it('should return platform specific value', () => {
26-
const obj = {ios: 'ios', android: 'android'};
28+
const obj = {ios: 'ios', android: 'android', macos: 'macos'}; // [macOS]
2729
expect(PlatformIOS.select(obj)).toEqual(obj.ios);
2830
expect(PlatformAndroid.select(obj)).toEqual(obj.android);
31+
expect(PlatformMacOS.select(obj)).toEqual(obj.macos); // [macOS]
2932
});
3033

34+
// [macOS
35+
it('should return correct platform given partial platform overrides', () => {
36+
const iosSpecific = {ios: 'ios', native: 'native'};
37+
expect(PlatformIOS.select(iosSpecific)).toEqual(iosSpecific.ios);
38+
expect(PlatformAndroid.select(iosSpecific)).toEqual(iosSpecific.native);
39+
expect(PlatformMacOS.select(iosSpecific)).toEqual(iosSpecific.native);
40+
41+
const androidSpecific = {android: 'android', native: 'native'};
42+
expect(PlatformIOS.select(androidSpecific)).toEqual(
43+
androidSpecific.native,
44+
);
45+
expect(PlatformAndroid.select(androidSpecific)).toEqual(
46+
androidSpecific.android,
47+
);
48+
expect(PlatformMacOS.select(androidSpecific)).toEqual(
49+
androidSpecific.native,
50+
);
51+
52+
const macosSpecific = {macos: 'macos', native: 'native'};
53+
expect(PlatformIOS.select(macosSpecific)).toEqual(macosSpecific.native);
54+
expect(PlatformAndroid.select(macosSpecific)).toEqual(
55+
macosSpecific.native,
56+
);
57+
expect(PlatformMacOS.select(macosSpecific)).toEqual(macosSpecific.macos);
58+
});
59+
// macOS]
60+
3161
it('should return native value if no specific value was found', () => {
3262
const obj = {native: 'native', default: 'default'};
3363
expect(PlatformIOS.select(obj)).toEqual(obj.native);
3464
expect(PlatformAndroid.select(obj)).toEqual(obj.native);
65+
expect(PlatformMacOS.select(obj)).toEqual(obj.native); // [macOS]
3566
});
3667

3768
it('should return default value if no specific value was found', () => {
3869
const obj = {default: 'default'};
3970
expect(PlatformIOS.select(obj)).toEqual(obj.default);
4071
expect(PlatformAndroid.select(obj)).toEqual(obj.default);
72+
expect(PlatformMacOS.select(obj)).toEqual(obj.default); // [macOS]
4173
});
4274
});
4375
});

0 commit comments

Comments
 (0)