Skip to content

Commit dcf191b

Browse files
authored
Add test IDs for future e2e testing (#286)
1 parent 470eeb3 commit dcf191b

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

sample/src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function CartIcon({onPress}: {onPress: () => void}) {
298298
const theme = useTheme();
299299

300300
return (
301-
<Pressable onPress={onPress}>
301+
<Pressable onPress={onPress} testID="header-cart-icon">
302302
<Icon name="shopping-basket" size={24} color={theme.colors.secondary} />
303303
</Pressable>
304304
);
@@ -462,13 +462,15 @@ function Routes() {
462462
component={CatalogStack}
463463
options={{
464464
headerShown: false,
465+
tabBarButtonTestID: 'catalog-tab',
465466
tabBarIcon: createNavigationIcon('shop'),
466467
}}
467468
/>
468469
<Tab.Screen
469470
name="Cart"
470471
component={CartScreen}
471472
options={{
473+
tabBarButtonTestID: 'cart-tab',
472474
tabBarIcon: createNavigationIcon('shopping-bag'),
473475
tabBarBadge: totalQuantity > 0 ? totalQuantity : undefined,
474476
}}
@@ -477,6 +479,7 @@ function Routes() {
477479
name="Settings"
478480
component={SettingsScreen}
479481
options={{
482+
tabBarButtonTestID: 'settings-tab',
480483
tabBarIcon: createNavigationIcon('cog'),
481484
}}
482485
/>

sample/src/screens/CartScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function CartScreen(): React.JSX.Element {
176176
/>
177177

178178
<Pressable
179+
testID="checkout-button"
179180
style={styles.cartButton}
180181
disabled={totalQuantity === 0}
181182
onPress={presentCheckout}>

sample/src/screens/CatalogScreen.tsx

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
ActivityIndicator,
3434
} from 'react-native';
3535

36-
import {useShopifyCheckoutSheet} from '@shopify/checkout-sheet-kit';
3736
import useShopify from '../hooks/useShopify';
3837

3938
import type {ShopifyProduct} from '../../@types';
@@ -47,8 +46,7 @@ import {currency} from '../utils';
4746
type Props = NativeStackScreenProps<RootStackParamList, 'CatalogScreen'>;
4847

4948
function CatalogScreen({navigation}: Props) {
50-
const ShopifyCheckout = useShopifyCheckoutSheet();
51-
const {checkoutURL, totalQuantity, addToCart, addingToCart} = useCart();
49+
const {addToCart, addingToCart} = useCart();
5250
const {colors} = useTheme();
5351
const styles = createStyles(colors);
5452
const {queries} = useShopify();
@@ -59,12 +57,6 @@ function CatalogScreen({navigation}: Props) {
5957
fetchProducts();
6058
}, [fetchProducts]);
6159

62-
const presentCheckout = async () => {
63-
if (checkoutURL) {
64-
ShopifyCheckout.present(checkoutURL);
65-
}
66-
};
67-
6860
if (error) {
6961
return (
7062
<View style={styles.loading}>
@@ -91,10 +83,11 @@ function CatalogScreen({navigation}: Props) {
9183
contentInsetAdjustmentBehavior="automatic"
9284
contentContainerStyle={styles.scrollView}>
9385
<View style={styles.productList}>
94-
{data?.products.edges.map(({node}) => (
86+
{data?.products.edges.map(({node}, index) => (
9587
<Product
9688
key={node.id}
9789
product={node}
90+
testID={`product-${index}`}
9891
onPress={() => {
9992
navigation.navigate('ProductDetails', {
10093
product: node,
@@ -107,17 +100,6 @@ function CatalogScreen({navigation}: Props) {
107100
))}
108101
</View>
109102
</ScrollView>
110-
{totalQuantity > 0 && (
111-
<Pressable
112-
style={styles.cartButton}
113-
disabled={totalQuantity === 0}
114-
onPress={presentCheckout}>
115-
<Text style={styles.cartButtonText}>Checkout</Text>
116-
<Text style={styles.cartButtonTextSubtitle}>
117-
{totalQuantity} {totalQuantity === 1 ? 'item' : 'items'}
118-
</Text>
119-
</Pressable>
120-
)}
121103
</SafeAreaView>
122104
);
123105
}
@@ -131,19 +113,25 @@ function Product({
131113
onAddToCart,
132114
loading = false,
133115
onPress,
116+
testID,
134117
}: {
135118
product: ShopifyProduct;
136119
loading?: boolean;
137120
onPress: () => void;
138121
onAddToCart: (variantId: string) => void;
122+
testID: string;
139123
}) {
140124
const {colors} = useTheme();
141125
const styles = createStyles(colors);
142126
const image = product.images?.edges[0]?.node;
143127
const variant = getVariant(product);
144128

145129
return (
146-
<Pressable key={product.id} style={styles.productItem} onPress={onPress}>
130+
<Pressable
131+
key={product.id}
132+
style={styles.productItem}
133+
onPress={onPress}
134+
testID={testID}>
147135
{image?.url && (
148136
<Image
149137
resizeMethod="resize"

sample/src/screens/ProductDetailsScreen.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function ProductDetails({
117117
<View>
118118
<Text style={styles.productTitle}>{product.title}</Text>
119119
<Text style={styles.productDescription}>
120-
{product.description.slice(0, 100)} ...
120+
{product.description.slice(0, 100)}...
121121
</Text>
122122
</View>
123123

@@ -140,7 +140,11 @@ function ProductDetails({
140140
{loading ? (
141141
<ActivityIndicator size="small" color="white" />
142142
) : (
143-
<Text style={styles.addToCartButtonText}>Add to cart</Text>
143+
<Text
144+
testID="add-to-cart-button"
145+
style={styles.addToCartButtonText}>
146+
Add to cart
147+
</Text>
144148
)}
145149
</Pressable>
146150
</View>
@@ -217,7 +221,7 @@ function createStyles(colors: Colors, cornerRadius: number) {
217221
},
218222
addToCartButtonText: {
219223
fontSize: 20,
220-
lineHeight: 20,
224+
lineHeight: 24,
221225
color: colors.secondaryText,
222226
fontWeight: 'bold',
223227
textAlign: 'center',

0 commit comments

Comments
 (0)