|
| 1 | +#import <XCTest/XCTest.h> |
| 2 | +#import <React/RCTConvert.h> |
| 3 | + |
| 4 | +// Match RNMParticle.mm / pod umbrella so tests compile against the same SDK the library uses. |
| 5 | +#if defined(__has_include) && __has_include(<mParticle_Apple_SDK_ObjC/mParticle.h>) |
| 6 | +#import <mParticle_Apple_SDK_ObjC/mParticle.h> |
| 7 | +#elif defined(__has_include) && __has_include(<mParticle_Apple_SDK/mParticle.h>) |
| 8 | +#import <mParticle_Apple_SDK/mParticle.h> |
| 9 | +#else |
| 10 | +#import <mParticle_Apple_SDK_ObjC/mParticle.h> |
| 11 | +#endif |
| 12 | + |
| 13 | +// Implemented on `RCTConvert` in `RNMParticle.mm` (react-native-mparticle pod). |
| 14 | +@interface RCTConvert (MPCommerceEvent) |
| 15 | ++ (MPCommerceEvent *)MPCommerceEvent:(id)json; |
| 16 | ++ (MPCommerceEventAction)MPCommerceEventAction:(id)json; |
| 17 | ++ (MPPromotionAction)MPPromotionAction:(id)json; |
| 18 | +@end |
| 19 | + |
| 20 | +/** |
| 21 | + * Guards JS → native commerce enum mapping used by the bridge (including New Architecture). |
| 22 | + * Constants must stay aligned with `ProductActionType` / `PromotionActionType` in js/index.tsx. |
| 23 | + * |
| 24 | + * Direct `MPCommerceEventAction` / `MPPromotionAction` tests above validate the table only. |
| 25 | + * JSON → `MPCommerceEvent` tests below exercise the same `+[RCTConvert MPCommerceEvent:]` pipeline |
| 26 | + * used to assemble an `MPCommerceEvent` before `-[MParticle logCommerceEvent:]` (legacy bridge path), |
| 27 | + * including `MPPromotionContainer:` wiring. That catches regressions such as casting JS ints in |
| 28 | + * those helpers instead of calling the mappers. The New Architecture TurboModule `logCommerceEvent` |
| 29 | + * codegen struct path is still not invoked here (would require generated C++ types in this target). |
| 30 | + */ |
| 31 | +@interface RCTConvertCommerceMappingTests : XCTestCase |
| 32 | +@end |
| 33 | + |
| 34 | +@implementation RCTConvertCommerceMappingTests |
| 35 | + |
| 36 | +- (void)testMPCommerceEventAction_mapsReactNativeProductActionTypeConstants |
| 37 | +{ |
| 38 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(1)], MPCommerceEventActionAddToCart); |
| 39 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(2)], MPCommerceEventActionRemoveFromCart); |
| 40 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(3)], MPCommerceEventActionCheckout); |
| 41 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(4)], MPCommerceEventActionCheckoutOptions); |
| 42 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(5)], MPCommerceEventActionClick); |
| 43 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(6)], MPCommerceEventActionViewDetail); |
| 44 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(7)], MPCommerceEventActionPurchase); |
| 45 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(8)], MPCommerceEventActionRefund); |
| 46 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(9)], MPCommerceEventActionAddToWishList); |
| 47 | + XCTAssertEqual([RCTConvert MPCommerceEventAction:@(10)], MPCommerceEventActionRemoveFromWishlist); |
| 48 | +} |
| 49 | + |
| 50 | +- (void)testMPPromotionAction_mapsReactNativePromotionActionTypeConstants |
| 51 | +{ |
| 52 | + // JS: View = 0, Click = 1. Native: Click = 0, View = 1 (MPPromotion.h). |
| 53 | + XCTAssertEqual([RCTConvert MPPromotionAction:@(0)], MPPromotionActionView); |
| 54 | + XCTAssertEqual([RCTConvert MPPromotionAction:@(1)], MPPromotionActionClick); |
| 55 | + XCTAssertEqual([RCTConvert MPPromotionAction:@(99)], MPPromotionActionClick); |
| 56 | +} |
| 57 | + |
| 58 | +#pragma mark - JSON → MPCommerceEvent (integration-style) |
| 59 | + |
| 60 | +- (NSDictionary *)minimalProductJSON |
| 61 | +{ |
| 62 | + return @{ |
| 63 | + @"name" : @"Test Product", |
| 64 | + @"sku" : @"SKU-1", |
| 65 | + @"price" : @19.99, |
| 66 | + @"quantity" : @1, |
| 67 | + @"customAttributes" : @{}, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +- (void)testMPCommerceEventFromJSON_productActionFlowsThroughRCTConvertCommerceEvent |
| 72 | +{ |
| 73 | + NSDictionary *json = @{ |
| 74 | + @"productActionType" : @(7), // Purchase in js/index.tsx |
| 75 | + @"products" : @[ [self minimalProductJSON] ], |
| 76 | + @"impressions" : @[], |
| 77 | + }; |
| 78 | + |
| 79 | + MPCommerceEvent *event = [RCTConvert MPCommerceEvent:json]; |
| 80 | + XCTAssertEqual(event.action, MPCommerceEventActionPurchase); |
| 81 | +} |
| 82 | + |
| 83 | +- (void)testMPCommerceEventFromJSON_promotionActionFlowsThroughMPPromotionContainer |
| 84 | +{ |
| 85 | + NSDictionary *promotion = @{ |
| 86 | + @"id" : @"promo-1", |
| 87 | + @"name" : @"Sale", |
| 88 | + @"creative" : @"banner", |
| 89 | + @"position" : @"home-top", |
| 90 | + }; |
| 91 | + |
| 92 | + NSDictionary *jsonView = @{ |
| 93 | + @"promotionActionType" : @(0), // JS PromotionActionType.View |
| 94 | + @"promotions" : @[ promotion ], |
| 95 | + @"products" : @[], |
| 96 | + @"impressions" : @[], |
| 97 | + }; |
| 98 | + MPCommerceEvent *viewEvent = [RCTConvert MPCommerceEvent:jsonView]; |
| 99 | + XCTAssertNotNil(viewEvent.promotionContainer); |
| 100 | + XCTAssertEqual(viewEvent.promotionContainer.action, MPPromotionActionView); |
| 101 | + |
| 102 | + NSDictionary *jsonClick = @{ |
| 103 | + @"promotionActionType" : @(1), // JS PromotionActionType.Click |
| 104 | + @"promotions" : @[ promotion ], |
| 105 | + @"products" : @[], |
| 106 | + @"impressions" : @[], |
| 107 | + }; |
| 108 | + MPCommerceEvent *clickEvent = [RCTConvert MPCommerceEvent:jsonClick]; |
| 109 | + XCTAssertNotNil(clickEvent.promotionContainer); |
| 110 | + XCTAssertEqual(clickEvent.promotionContainer.action, MPPromotionActionClick); |
| 111 | +} |
| 112 | + |
| 113 | +@end |
0 commit comments