From 4b119a3cee81715097747f7ea085ace618b16f1f Mon Sep 17 00:00:00 2001 From: Vladimir Espinola Date: Thu, 11 Jul 2024 15:24:13 -0400 Subject: [PATCH] added unit test --- Example/Base.lproj/Main.storyboard | 110 +++++++++++------- Example/Example/ProductViewController.m | 23 ++-- Tests/TestCases/ATTNAddToCartEventTests.swift | 28 +++++ .../TestCases/ATTNProductViewEventTests.swift | 30 +++++ attentive-ios-sdk.xcodeproj/project.pbxproj | 8 ++ 5 files changed, 152 insertions(+), 47 deletions(-) create mode 100644 Tests/TestCases/ATTNAddToCartEventTests.swift create mode 100644 Tests/TestCases/ATTNProductViewEventTests.swift diff --git a/Example/Base.lproj/Main.storyboard b/Example/Base.lproj/Main.storyboard index 821d493..85567ee 100644 --- a/Example/Base.lproj/Main.storyboard +++ b/Example/Base.lproj/Main.storyboard @@ -157,48 +157,80 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Example/Example/ProductViewController.m b/Example/Example/ProductViewController.m index 4e00b84..6abbab4 100644 --- a/Example/Example/ProductViewController.m +++ b/Example/Example/ProductViewController.m @@ -26,16 +26,26 @@ - (void)viewDidLoad { - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; - [self showToast:@"Product View event sent"]; + [self showToast:@"Product View event sent" duration:2]; } - (IBAction)addToCartButtonPressed:(id)sender { ATTNItem* item = [self buildItem]; ATTNAddToCartEvent* addToCart = [[ATTNAddToCartEvent alloc] initWithItems:@[ item ]]; + addToCart.deeplink = @"https://www.clientapp.com/flow=payment"; [[ATTNEventTracker sharedInstance] recordEvent:addToCart]; - [self showToast:@"Add To Cart event sent"]; + [self showToast:@"Add To Cart event sent" duration:2]; +} + +- (IBAction)addToCartWithDeeplinkButtonPressed:(id)sender { + ATTNItem* item = [self buildItem]; + ATTNAddToCartEvent* addToCart = [[ATTNAddToCartEvent alloc] initWithItems:@[ item ]]; + addToCart.deeplink = @"https://www.clientapp.com/flow=payment"; + + [[ATTNEventTracker sharedInstance] recordEvent:addToCart]; + [self showToast: [NSString stringWithFormat:@"Add To Cart event sent with requestURL: '%@'", addToCart.deeplink] duration:4]; } - (IBAction)purchaseButtonPressed:(id)sender { @@ -50,7 +60,7 @@ - (IBAction)purchaseButtonPressed:(id)sender { [[ATTNEventTracker sharedInstance] recordEvent:purchase]; - [self showToast:@"Purchase event sent"]; + [self showToast:@"Purchase event sent" duration:2]; } - (ATTNItem*)buildItem { @@ -67,19 +77,16 @@ - (IBAction)customEventButtonPressed:(id)sender { [[ATTNEventTracker sharedInstance] recordEvent:customEvent]; - [self showToast:@"Custom event sent"]; + [self showToast:@"Custom event sent" duration:2]; } - -- (void)showToast:(NSString*)message { +- (void)showToast:(NSString*)message duration:(int)duration { UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:alert animated:YES completion:nil]; - int duration = 1; // duration in seconds - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [alert dismissViewControllerAnimated:YES completion:nil]; }); diff --git a/Tests/TestCases/ATTNAddToCartEventTests.swift b/Tests/TestCases/ATTNAddToCartEventTests.swift new file mode 100644 index 0000000..21b7cd3 --- /dev/null +++ b/Tests/TestCases/ATTNAddToCartEventTests.swift @@ -0,0 +1,28 @@ +// +// ATTNAddToCartEventTests.swift +// attentive-ios-sdk Tests +// +// Created by Vladimir - Work on 2024-07-11. +// + +import XCTest +@testable import ATTNSDKFramework + +final class ATTNAddToCartEventTests: XCTestCase { + func testAddCart_GivenData_ShouldBuildURL() { + let item = ATTNTestEventUtils.buildItem() + let addToCart = ATTNAddToCartEvent(items: [item]) + XCTAssertFalse(addToCart.eventRequests.isEmpty) + XCTAssertNil(addToCart.eventRequests.first?.metadata["requestURL"]) + } + + func testAddCart_GivenData_ShouldBuildURLWithRequestURL() { + let item = ATTNTestEventUtils.buildItem() + let addToCart = ATTNAddToCartEvent(items: [item]) + addToCart.deeplink = "https://www.clientapp.com/flow=payment" + XCTAssertFalse(addToCart.eventRequests.isEmpty) + let requestURL = addToCart.eventRequests.first?.metadata["requestURL"] as? String + XCTAssertNotNil(requestURL) + XCTAssertFalse(requestURL?.isEmpty ?? true) + } +} diff --git a/Tests/TestCases/ATTNProductViewEventTests.swift b/Tests/TestCases/ATTNProductViewEventTests.swift new file mode 100644 index 0000000..3ba71e7 --- /dev/null +++ b/Tests/TestCases/ATTNProductViewEventTests.swift @@ -0,0 +1,30 @@ +// +// ATTNProductViewEventTests.swift +// attentive-ios-sdk Tests +// +// Created by Vladimir - Work on 2024-07-11. +// + +import Foundation + +import XCTest +@testable import ATTNSDKFramework + +final class ATTNProductViewEventTests: XCTestCase { + func testProductView_GivenData_ShouldBuildURL() { + let item = ATTNTestEventUtils.buildItem() + let productView = ATTNProductViewEvent(items: [item]) + XCTAssertFalse(productView.eventRequests.isEmpty) + XCTAssertNil(productView.eventRequests.first?.metadata["requestURL"]) + } + + func testProductView_GivenData_ShouldBuildURLWithRequestURL() { + let item = ATTNTestEventUtils.buildItem() + let productView = ATTNProductViewEvent(items: [item]) + productView.deeplink = "https://www.clientapp.com/flow=payment" + XCTAssertFalse(productView.eventRequests.isEmpty) + let requestURL = productView.eventRequests.first?.metadata["requestURL"] as? String + XCTAssertNotNil(requestURL) + XCTAssertFalse(requestURL?.isEmpty ?? true) + } +} diff --git a/attentive-ios-sdk.xcodeproj/project.pbxproj b/attentive-ios-sdk.xcodeproj/project.pbxproj index ae17bc1..7b21641 100644 --- a/attentive-ios-sdk.xcodeproj/project.pbxproj +++ b/attentive-ios-sdk.xcodeproj/project.pbxproj @@ -53,6 +53,8 @@ FB65536A2C1B72D4008DB3B1 /* ATTNSDKTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB6553692C1B72D4008DB3B1 /* ATTNSDKTests.swift */; }; FB65536C2C1B74A9008DB3B1 /* ATTNAPIProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB65536B2C1B74A9008DB3B1 /* ATTNAPIProtocol.swift */; }; FB84E3032C3F30FF0011936B /* ATTNDeeplinkHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB84E3022C3F30FF0011936B /* ATTNDeeplinkHandling.swift */; }; + FB86C03E2C40611A00E35580 /* ATTNAddToCartEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB86C03D2C40611A00E35580 /* ATTNAddToCartEventTests.swift */; }; + FB86C0402C40669400E35580 /* ATTNProductViewEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB86C03F2C40669400E35580 /* ATTNProductViewEventTests.swift */; }; FB90EF0B2C109CB4004DFC4A /* ATTNAPIITTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB90EF0A2C109CB4004DFC4A /* ATTNAPIITTests.swift */; }; FB90EF0D2C10A7F7004DFC4A /* NSURLSessionDataTaskMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB90EF0C2C10A7F7004DFC4A /* NSURLSessionDataTaskMock.swift */; }; FB90EF0F2C10A81E004DFC4A /* NSURLSessionMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB90EF0E2C10A81E004DFC4A /* NSURLSessionMock.swift */; }; @@ -158,6 +160,8 @@ FB65536B2C1B74A9008DB3B1 /* ATTNAPIProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNAPIProtocol.swift; sourceTree = ""; }; FB65536D2C1B7747008DB3B1 /* ATTNAPISpy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNAPISpy.swift; sourceTree = ""; }; FB84E3022C3F30FF0011936B /* ATTNDeeplinkHandling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNDeeplinkHandling.swift; sourceTree = ""; }; + FB86C03D2C40611A00E35580 /* ATTNAddToCartEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNAddToCartEventTests.swift; sourceTree = ""; }; + FB86C03F2C40669400E35580 /* ATTNProductViewEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNProductViewEventTests.swift; sourceTree = ""; }; FB90EF0A2C109CB4004DFC4A /* ATTNAPIITTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNAPIITTests.swift; sourceTree = ""; }; FB90EF0C2C10A7F7004DFC4A /* NSURLSessionDataTaskMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSURLSessionDataTaskMock.swift; sourceTree = ""; }; FB90EF0E2C10A81E004DFC4A /* NSURLSessionMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSURLSessionMock.swift; sourceTree = ""; }; @@ -337,6 +341,8 @@ FB2984072C0FAE040039759C /* ATTNAPITests.swift */, FB90EF0A2C109CB4004DFC4A /* ATTNAPIITTests.swift */, FB6553692C1B72D4008DB3B1 /* ATTNSDKTests.swift */, + FB86C03D2C40611A00E35580 /* ATTNAddToCartEventTests.swift */, + FB86C03F2C40669400E35580 /* ATTNProductViewEventTests.swift */, ); path = TestCases; sourceTree = ""; @@ -704,7 +710,9 @@ FB90EF0F2C10A81E004DFC4A /* NSURLSessionMock.swift in Sources */, FB2983E42C0F73990039759C /* ATTNAppInfoMock.swift in Sources */, FB2983F82C0F7FB60039759C /* ATTNUserAgentBuilderTests.swift in Sources */, + FB86C03E2C40611A00E35580 /* ATTNAddToCartEventTests.swift in Sources */, FB2983F62C0F7CF10039759C /* ATTNUserIdentityTests.swift in Sources */, + FB86C0402C40669400E35580 /* ATTNProductViewEventTests.swift in Sources */, FB2983F42C0F759D0039759C /* ATTNVisitorServiceTests.swift in Sources */, FB2983FA2C0F80A30039759C /* ATTNPersistentStorageTests.swift in Sources */, FB080C722C1C755F00834BAA /* ATTNCreativeUrlProviderSpy.swift in Sources */,