Skip to content

Commit 7741b8e

Browse files
[Sample]: fix: preload not working (#330)
* fix: all context values noop * fix: test to throw from context hook
1 parent 3a89437 commit 7741b8e

File tree

3 files changed

+36
-47
lines changed

3 files changed

+36
-47
lines changed

modules/@shopify/checkout-sheet-kit/src/context.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,9 @@ interface Context {
4848
version: Maybe<string>;
4949
}
5050

51-
const noop = () => undefined;
52-
53-
const ShopifyCheckoutSheetContext = React.createContext<Context>({
54-
acceleratedCheckoutsAvailable: false,
55-
addEventListener: noop,
56-
removeEventListeners: noop,
57-
setConfig: noop,
58-
getConfig: async () => undefined,
59-
preload: noop,
60-
present: noop,
61-
invalidate: noop,
62-
dismiss: noop,
63-
version: undefined,
64-
});
51+
const ShopifyCheckoutSheetContext = React.createContext<Context>(
52+
null as unknown as Context,
53+
);
6554

6655
interface Props {
6756
features?: Partial<Features>;
@@ -172,7 +161,13 @@ export function ShopifyCheckoutSheetProvider({
172161
}
173162

174163
export function useShopifyCheckoutSheet() {
175-
return React.useContext(ShopifyCheckoutSheetContext);
164+
const context = React.useContext(ShopifyCheckoutSheetContext);
165+
if (!context) {
166+
throw new Error(
167+
'useShopifyCheckoutSheet must be used from within a ShopifyCheckoutSheetContext',
168+
);
169+
}
170+
return context;
176171
}
177172

178173
export default ShopifyCheckoutSheetContext;

modules/@shopify/checkout-sheet-kit/tests/context.test.tsx

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -389,27 +389,13 @@ describe('useShopifyCheckoutSheet', () => {
389389
});
390390

391391
describe('ShopifyCheckoutSheetContext without provider', () => {
392-
it('uses default context values when no provider is present', async () => {
393-
let hookValue: any;
394-
const onHookValue = (value: any) => {
395-
hookValue = value;
396-
};
397-
398-
render(<HookTestComponent onHookValue={onHookValue} />);
399-
400-
const config = await hookValue.getConfig();
401-
expect(config).toBeUndefined();
402-
403-
// Test all the noop functions to ensure they don't throw
404-
expect(() => hookValue.addEventListener('close', jest.fn())).not.toThrow();
405-
expect(() => hookValue.removeEventListeners('close')).not.toThrow();
406-
expect(() =>
407-
hookValue.setConfig({colorScheme: ColorScheme.automatic}),
408-
).not.toThrow();
409-
expect(() => hookValue.preload('test-url')).not.toThrow();
410-
expect(() => hookValue.present('test-url')).not.toThrow();
411-
expect(() => hookValue.invalidate()).not.toThrow();
412-
expect(() => hookValue.dismiss()).not.toThrow();
413-
expect(hookValue.version).toBeUndefined();
392+
it('throws error when hook is used without provider', () => {
393+
const errorSpy = jest.spyOn(console, 'error').mockImplementation();
394+
395+
expect(() => {
396+
render(<HookTestComponent onHookValue={() => {}} />);
397+
}).toThrow('useShopifyCheckoutSheet must be used from within a ShopifyCheckoutSheetContext');
398+
399+
errorSpy.mockRestore();
414400
});
415401
});

sample/src/App.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ function CartIcon({onPress}: {onPress: () => void}) {
304304
);
305305
}
306306

307-
function AppWithNavigation({children}: PropsWithChildren) {
308-
const {colorScheme, preference} = useTheme();
307+
function AppWithCheckoutKit({children}: PropsWithChildren) {
309308
const {appConfig} = useConfig();
310309

311310
const updatedColors = getColors(
@@ -399,13 +398,20 @@ function AppWithNavigation({children}: PropsWithChildren) {
399398
<ShopifyCheckoutSheetProvider
400399
configuration={checkoutKitConfig}
401400
features={checkoutKitFeatures}>
402-
<NavigationContainer theme={getNavigationTheme(colorScheme, preference)}>
403-
{children}
404-
</NavigationContainer>
401+
{children}
405402
</ShopifyCheckoutSheetProvider>
406403
);
407404
}
408405

406+
function AppWithNavigation(props: {children: React.ReactNode}) {
407+
const {colorScheme, preference} = useTheme();
408+
return (
409+
<NavigationContainer theme={getNavigationTheme(colorScheme, preference)}>
410+
{props.children}
411+
</NavigationContainer>
412+
);
413+
}
414+
409415
function Routes() {
410416
const {totalQuantity} = useCart();
411417
const navigation = useNavigation<NavigationProp<RootStackParamList>>();
@@ -486,11 +492,13 @@ function App() {
486492
return (
487493
<ErrorBoundary>
488494
<AppWithTheme>
489-
<AppWithContext>
490-
<AppWithNavigation>
491-
<Routes />
492-
</AppWithNavigation>
493-
</AppWithContext>
495+
<AppWithCheckoutKit>
496+
<AppWithContext>
497+
<AppWithNavigation>
498+
<Routes />
499+
</AppWithNavigation>
500+
</AppWithContext>
501+
</AppWithCheckoutKit>
494502
</AppWithTheme>
495503
</ErrorBoundary>
496504
);

0 commit comments

Comments
 (0)