Skip to content

Commit

Permalink
Revert "feat(react-native-storybook): isRunningVisualTests & handled …
Browse files Browse the repository at this point in the history
…errors (#40)"

This reverts commit 8c8bd3c.
  • Loading branch information
michalziolkowski committed Aug 25, 2024
1 parent 7225950 commit a752000
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 83 deletions.
1 change: 0 additions & 1 deletion examples/expo-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"sherlo:demo": "sherlo --config ../../configs/demo.preview.json",
"sherlo:eas": "sherlo --config ../../configs/eas.preview.json",
"sherlo:sync": "sherlo --config ../../configs/sync.preview.json",
"start": "expo start",
"start:android:dev": "adb install builds/development/android.apk && expo start --android --dev-client",
"start:android:go": "expo start --android --go",
"start:ios:dev": "tar -xzvf builds/development/ios.tar.gz -C builds/ && xcrun simctl install booted builds/sherloexpoexample.app && expo start --ios --dev-client",
Expand Down
18 changes: 3 additions & 15 deletions examples/expo-example/src/components/screens/Test/Test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { MaterialIcons } from '@expo/vector-icons';
import { isRunningVisualTests } from '@sherlo/react-native-storybook';
import * as Localization from 'expo-localization';
import { Text, View, StyleSheet, useColorScheme } from 'react-native';
import { useColorScheme } from 'react-native';

const Test = () => {
const theme = useColorScheme(); // 'light' or 'dark'
Expand All @@ -16,29 +17,16 @@ const Test = () => {
<View style={[styles.container, { backgroundColor }]}>
<View style={styles.infoContainer}>
<MaterialIcons name="record-voice-over" size={24} color={textColor} />

<Text style={[styles.text, { color: textColor }]}>Language: {language}</Text>
</View>

<View style={styles.infoContainer}>
<MaterialIcons name="place" size={24} color={textColor} />

<Text style={[styles.text, { color: textColor }]}>Country: {country}</Text>
</View>

<View style={styles.infoContainer}>
<MaterialIcons name="brightness-4" size={24} color={textColor} />

<Text style={[styles.text, { color: textColor }]}>Theme: {theme || 'undefined'}</Text>
</View>

<View style={styles.infoContainer}>
<MaterialIcons name="directions-run" size={24} color={textColor} />

<Text style={[styles.text, { color: textColor }]}>
isRunningVisualTests: {isRunningVisualTests.toString()}
</Text>
</View>
</View>
);
};
Expand Down
34 changes: 9 additions & 25 deletions packages/react-native-storybook/src/helpers/SherloModule.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import base64 from 'base-64';
import { NativeModules } from 'react-native';
import utf8 from 'utf8';

let isExpoGo = false;
try {
const Constants = require('expo-constants').default;
isExpoGo = Constants.appOwnership === 'expo';
} catch {}
import isExpoGo from './isExpoGo';

type SherloModule = {
getInitialMode: () => 'testing' | 'default';
Expand All @@ -25,6 +20,12 @@ if (SherloNativeModule !== null) {
SherloModule = createSherloModule();
} else {
SherloModule = createDummySherloModule();

if (!isExpoGo) {
console.warn(
'@sherlo/react-native-storybook: Sherlo native module is not accessible. Rebuild the app to link it on the native side.'
);
}
}

export default SherloModule;
Expand Down Expand Up @@ -64,30 +65,13 @@ function normalizePath(path: string): string {
}

function createDummySherloModule(): SherloModule {
const noNativeModuleErrorMessage = getNoNativeModuleErrorMessage();

return {
storybookRegistered: async () => {},
getInitialMode: () => 'default',
appendFile: async () => {},
mkdir: async () => {},
readFile: async () => '',
openStorybook: async () => {
throw new Error(noNativeModuleErrorMessage);
},
toggleStorybook: async () => {
throw new Error(noNativeModuleErrorMessage);
},
openStorybook: async () => {},
toggleStorybook: async () => {},
};
}

function getNoNativeModuleErrorMessage() {
if (isExpoGo) {
return [
'@sherlo/react-native-storybook: Accessing Storybook via Expo Go is not supported. Use a developer build or set up Storybook as a standalone app.',
'Learn more: https://docs.sherlo.io/getting-started/setup#storybook-entry-point',
].join('\n\n');
} else {
return '@sherlo/react-native-storybook: Sherlo native module is not accessible. Rebuild the app to link it on the native side.';
}
}
1 change: 1 addition & 0 deletions packages/react-native-storybook/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as isExpoGo } from './isExpoGo';
export { default as RunnerBridge } from './RunnerBridge';
export { default as SherloModule } from './SherloModule';
11 changes: 11 additions & 0 deletions packages/react-native-storybook/src/helpers/isExpoGo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let isExpoGo: boolean;

try {
const Constants = require('expo-constants').default;

isExpoGo = Constants.appOwnership === 'expo';
} catch {
isExpoGo = false;
}

export default isExpoGo;
1 change: 0 additions & 1 deletion packages/react-native-storybook/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as getStorybook } from './getStorybook';
export { default as isRunningVisualTests } from './isRunningVisualTests';
export { default as openStorybook } from './openStorybook';
export { default as registerStorybook } from './registerStorybook';
7 changes: 0 additions & 7 deletions packages/react-native-storybook/src/isRunningVisualTests.ts

This file was deleted.

13 changes: 5 additions & 8 deletions packages/react-native-storybook/src/openStorybook.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { SherloModule } from './helpers';
import { handleAsyncError } from './utils';

function openStorybook(): void {
SherloModule.openStorybook().catch((error) => {
function openStorybook(): Promise<void> {
return SherloModule.openStorybook().catch((error) => {
if (error.code === 'NOT_REGISTERED') {
handleAsyncError(
new Error(
'To use `openStorybook()`, you need to first call `registerStorybook()`.\n\nLearn more: https://docs.sherlo.io/getting-started/setup?storybook-entry-point=integrated#storybook-entry-point'
)
console.log(
'To use `openStorybook()`, you need to first call `registerStorybook()`.\n\nLearn more: https://docs.sherlo.io/getting-started/setup?storybook-entry-point=integrated#storybook-entry-point'
);
} else {
handleAsyncError(error);
throw error;
}
});
}
Expand Down
21 changes: 7 additions & 14 deletions packages/react-native-storybook/src/registerStorybook.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { ReactElement } from 'react';
import { AppRegistry, DevSettings } from 'react-native';
import { SherloModule } from './helpers';
import { handleAsyncError } from './utils';

let ExpoDevMenu: any;
try {
ExpoDevMenu = require('expo-dev-menu');
} catch {}

function registerStorybook(StorybookComponent: () => ReactElement) {
AppRegistry.registerComponent('SherloStorybook', () => StorybookComponent);
Expand All @@ -19,22 +13,21 @@ export default registerStorybook;

/* ========================================================================== */

let hasAddedDevMenuItem = false;
let ExpoDevMenu: any;
try {
ExpoDevMenu = require('expo-dev-menu');
} catch {}

function addToggleStorybookToDevMenu() {
// Add menu item once in development build
if (!__DEV__ || hasAddedDevMenuItem) return;
// Only add the menu item in development builds
if (!__DEV__) return;

const MENU_LABEL = 'Toggle Storybook';
const toggleStorybook = () => {
SherloModule.toggleStorybook().catch(handleAsyncError);
};
const toggleStorybook = () => SherloModule.toggleStorybook();

DevSettings.addMenuItem(MENU_LABEL, toggleStorybook);

if (ExpoDevMenu) {
ExpoDevMenu.registerDevMenuItems([{ name: MENU_LABEL, callback: toggleStorybook }]);
}

hasAddedDevMenuItem = true;
}
11 changes: 0 additions & 11 deletions packages/react-native-storybook/src/utils/handleAsyncError.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/react-native-storybook/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as getGlobalStates } from './getGlobalStates';
export { default as handleAsyncError } from './handleAsyncError';
export { default as isObject } from './isObject';

0 comments on commit a752000

Please sign in to comment.