Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add additional tests for undefined platform minification behavior. #27515

Merged
merged 2 commits into from Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-preset-expo/CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
### 💡 Others

- Disable color in snapshot tests in CI. ([#27301](https://github.com/expo/expo/pull/27301) by [@EvanBacon](https://github.com/EvanBacon))
- Add additional tests for undefined platform minification behavior. ([#27515](https://github.com/expo/expo/pull/27515) by [@EvanBacon](https://github.com/EvanBacon))
- Upgrade `babel-plugin-react-native-web` for latest `react-native-web` aliases. ([#27214](https://github.com/expo/expo/pull/27214) by [@EvanBacon](https://github.com/EvanBacon))
- Directly resolve plugins. ([#27041](https://github.com/expo/expo/pull/27041) by [@EvanBacon](https://github.com/EvanBacon))

Expand Down
Expand Up @@ -30,6 +30,62 @@ function stripReactNativeImport(code: string) {
.replace('var _reactNative=require("react-native");', '');
}

it(`removes Platform module without import (undefined behavior)`, () => {
const options = {
...DEFAULT_OPTS,
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'web', isDev: false }),
};

const sourceCode = `
if (Platform.OS === 'ios') {
console.log('ios')
}

Platform.select({
ios: () => console.log('ios'),
web: () => console.log('web'),
android: () => console.log('android'),
})
`;

expect(stripReactNativeImport(babel.transform(sourceCode, options)!.code!)).toEqual(
`(function(){return console.log('web');});`
);
});
it(`supports Platform module default fallback on web`, () => {
const options = {
...DEFAULT_OPTS,
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'web', isDev: false }),
};

const sourceCode = `
Platform.select({
ios: () => console.log('ios'),
default: () => console.log('default'),
})`;

expect(stripReactNativeImport(babel.transform(sourceCode, options)!.code!)).toEqual(
`(function(){return console.log('default');});`
);
});

xit(`removes Platform module and native fallback on web`, () => {
const options = {
...DEFAULT_OPTS,
caller: getCaller({ name: 'metro', engine: 'hermes', platform: 'web', isDev: false }),
};

const sourceCode = `
Platform.select({
native: () => console.log('native'),
default: () => console.log('default'),
})`;

expect(stripReactNativeImport(babel.transform(sourceCode, options)!.code!)).toEqual(
`(function(){return console.log('web');});`
);
});

it(`removes Platform module usage on web`, () => {
const options = {
...DEFAULT_OPTS,
Expand Down