@@ -19,58 +19,8 @@ const config: Configuration = {
1919 colorScheme : ColorScheme . automatic ,
2020} ;
2121
22- jest . mock ( 'react-native' , ( ) => {
23- let listeners : ( typeof jest . fn ) [ ] = [ ] ;
24-
25- const NativeEventEmitter = jest . fn ( ( ) => ( {
26- addListener : jest . fn ( ( _ , callback ) => {
27- listeners . push ( callback ) ;
28- } ) ,
29- removeAllListeners : jest . fn ( ( ) => {
30- listeners = [ ] ;
31- } ) ,
32- emit : jest . fn ( ( _ , data : any ) => {
33- for ( const listener of listeners ) {
34- listener ( data ) ;
35- }
36-
37- // clear listeners
38- listeners = [ ] ;
39- } ) ,
40- } ) ) ;
41-
42- const exampleConfig = {
43- preloading : true ,
44- } ;
45-
46- const ShopifyCheckoutSheetKit = {
47- eventEmitter : NativeEventEmitter ( ) ,
48- version : '0.7.0' ,
49- preload : jest . fn ( ) ,
50- present : jest . fn ( ) ,
51- dismiss : jest . fn ( ) ,
52- invalidateCache : jest . fn ( ) ,
53- getConfig : jest . fn ( async ( ) => exampleConfig ) ,
54- setConfig : jest . fn ( ) ,
55- addEventListener : jest . fn ( ) ,
56- removeEventListeners : jest . fn ( ) ,
57- initiateGeolocationRequest : jest . fn ( ) ,
58- } ;
59-
60- return {
61- Platform : {
62- OS : 'ios' ,
63- } ,
64- PermissionsAndroid : {
65- requestMultiple : jest . fn ( ) ,
66- } ,
67- _listeners : listeners ,
68- NativeEventEmitter,
69- NativeModules : {
70- ShopifyCheckoutSheetKit,
71- } ,
72- } ;
73- } ) ;
22+ // Use the shared manual mock. Individual tests can override if needed.
23+ jest . mock ( 'react-native' ) ;
7424
7525global . console = {
7626 ...global . console ,
@@ -429,27 +379,36 @@ describe('ShopifyCheckoutSheetKit', () => {
429379 { error : clientError , constructor : CheckoutClientError } ,
430380 { error : networkError , constructor : CheckoutHTTPError } ,
431381 { error : expiredError , constructor : CheckoutExpiredError } ,
432- ] ) ( `correctly parses error $error` , ( { error, constructor} ) => {
433- const instance = new ShopifyCheckoutSheet ( ) ;
434- const eventName = 'error' ;
435- const callback = jest . fn ( ) ;
436- instance . addEventListener ( eventName , callback ) ;
437- NativeModules . ShopifyCheckoutSheetKit . addEventListener (
438- eventName ,
439- callback ,
440- ) ;
441- expect ( eventEmitter . addListener ) . toHaveBeenCalledWith (
442- 'error' ,
443- expect . any ( Function ) ,
444- ) ;
445- eventEmitter . emit ( 'error' , error ) ;
446- const calledWith = callback . mock . calls [ 0 ] [ 0 ] ;
447- expect ( calledWith ) . toBeInstanceOf ( constructor ) ;
448- expect ( calledWith ) . not . toHaveProperty ( '__typename' ) ;
449- expect ( calledWith ) . toHaveProperty ( 'code' ) ;
450- expect ( calledWith ) . toHaveProperty ( 'message' ) ;
451- expect ( calledWith ) . toHaveProperty ( 'recoverable' ) ;
452- } ) ;
382+ ] ) (
383+ `correctly parses error $error` ,
384+ ( {
385+ error,
386+ constructor,
387+ } : {
388+ error : any ;
389+ constructor : new ( ...args : any [ ] ) => any ;
390+ } ) => {
391+ const instance = new ShopifyCheckoutSheet ( ) ;
392+ const eventName = 'error' ;
393+ const callback = jest . fn ( ) ;
394+ instance . addEventListener ( eventName , callback ) ;
395+ NativeModules . ShopifyCheckoutSheetKit . addEventListener (
396+ eventName ,
397+ callback ,
398+ ) ;
399+ expect ( eventEmitter . addListener ) . toHaveBeenCalledWith (
400+ 'error' ,
401+ expect . any ( Function ) ,
402+ ) ;
403+ eventEmitter . emit ( 'error' , error ) ;
404+ const calledWith = callback . mock . calls [ 0 ] [ 0 ] ;
405+ expect ( calledWith ) . toBeInstanceOf ( constructor ) ;
406+ expect ( calledWith ) . not . toHaveProperty ( '__typename' ) ;
407+ expect ( calledWith ) . toHaveProperty ( 'code' ) ;
408+ expect ( calledWith ) . toHaveProperty ( 'message' ) ;
409+ expect ( calledWith ) . toHaveProperty ( 'recoverable' ) ;
410+ } ,
411+ ) ;
453412
454413 it ( 'returns an unknown generic error if the error cannot be parsed' , ( ) => {
455414 const instance = new ShopifyCheckoutSheet ( ) ;
@@ -535,9 +494,11 @@ describe('ShopifyCheckoutSheetKit', () => {
535494 'android.permission.ACCESS_FINE_LOCATION' : 'denied' ,
536495 } ;
537496
538- ( PermissionsAndroid . requestMultiple as jest . Mock ) . mockResolvedValue (
539- mockPermissions ,
540- ) ;
497+ (
498+ PermissionsAndroid . requestMultiple as unknown as {
499+ mockResolvedValue : ( v : any ) => void ;
500+ }
501+ ) . mockResolvedValue ( mockPermissions ) ;
541502
542503 new ShopifyCheckoutSheet ( ) ;
543504
@@ -558,9 +519,11 @@ describe('ShopifyCheckoutSheetKit', () => {
558519 'android.permission.ACCESS_FINE_LOCATION' : 'denied' ,
559520 } ;
560521
561- ( PermissionsAndroid . requestMultiple as jest . Mock ) . mockResolvedValue (
562- mockPermissions ,
563- ) ;
522+ (
523+ PermissionsAndroid . requestMultiple as unknown as {
524+ mockResolvedValue : ( v : any ) => void ;
525+ }
526+ ) . mockResolvedValue ( mockPermissions ) ;
564527
565528 new ShopifyCheckoutSheet ( ) ;
566529
0 commit comments