Skip to content

Commit 49351fd

Browse files
amgleitmanAdam Gleitman
andauthored
[0.77-stable] Fix Platform.select on Mac (#2375)
* Revert accidental change to Platform.select in 0.77 merge * Add extra unit tests * nit: fix formatting * Update yarn.lock --------- Co-authored-by: Adam Gleitman <[email protected]>
1 parent 264325d commit 49351fd

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
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
});

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,7 +2743,7 @@ __metadata:
27432743
languageName: unknown
27442744
linkType: soft
27452745

2746-
"@react-native-mac/virtualized-lists@workspace:*, @react-native-mac/virtualized-lists@workspace:packages/virtualized-lists":
2746+
"@react-native-mac/virtualized-lists@npm:0.77.0, @react-native-mac/virtualized-lists@workspace:packages/virtualized-lists":
27472747
version: 0.0.0-use.local
27482748
resolution: "@react-native-mac/virtualized-lists@workspace:packages/virtualized-lists"
27492749
dependencies:
@@ -3038,13 +3038,13 @@ __metadata:
30383038
languageName: unknown
30393039
linkType: soft
30403040

3041-
"@react-native/oss-library-example@workspace:*, @react-native/oss-library-example@workspace:packages/react-native-test-library":
3041+
"@react-native/oss-library-example@npm:0.77.2, @react-native/oss-library-example@workspace:packages/react-native-test-library":
30423042
version: 0.0.0-use.local
30433043
resolution: "@react-native/oss-library-example@workspace:packages/react-native-test-library"
30443044
dependencies:
30453045
"@babel/core": "npm:^7.25.2"
30463046
"@react-native/babel-preset": "npm:0.77.0"
3047-
react-native-macos: "workspace:*"
3047+
react-native-macos: "npm:0.77.0"
30483048
peerDependencies:
30493049
react: "*"
30503050
react-native-macos: "*"
@@ -3087,7 +3087,7 @@ __metadata:
30873087
"@react-native-community/cli": "npm:15.0.0-alpha.2"
30883088
"@react-native-community/cli-platform-android": "npm:15.0.0-alpha.2"
30893089
"@react-native-community/cli-platform-ios": "npm:15.0.0-alpha.2"
3090-
"@react-native/oss-library-example": "workspace:*"
3090+
"@react-native/oss-library-example": "npm:0.77.2"
30913091
"@react-native/popup-menu-android": "workspace:*"
30923092
flow-enums-runtime: "npm:^0.0.6"
30933093
invariant: "npm:^2.2.4"
@@ -10863,12 +10863,12 @@ __metadata:
1086310863
languageName: unknown
1086410864
linkType: soft
1086510865

10866-
"react-native-macos@workspace:*, react-native-macos@workspace:packages/react-native":
10866+
"react-native-macos@npm:0.77.0, react-native-macos@workspace:packages/react-native":
1086710867
version: 0.0.0-use.local
1086810868
resolution: "react-native-macos@workspace:packages/react-native"
1086910869
dependencies:
1087010870
"@jest/create-cache-key-function": "npm:^29.6.3"
10871-
"@react-native-mac/virtualized-lists": "workspace:*"
10871+
"@react-native-mac/virtualized-lists": "npm:0.77.0"
1087210872
"@react-native/assets-registry": "npm:0.77.0"
1087310873
"@react-native/codegen": "npm:0.77.0"
1087410874
"@react-native/community-cli-plugin": "npm:0.77.0"

0 commit comments

Comments
 (0)