Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Espinola committed Jul 11, 2024
1 parent 3eb8212 commit 4b119a3
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 47 deletions.
110 changes: 71 additions & 39 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -157,48 +157,80 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="ProductImage" translatesAutoresizingMaskIntoConstraints="NO" id="9xR-K8-uf1">
<rect key="frame" x="87" y="144" width="240" height="128"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" text="T-Shirt" textAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="vq5-I3-w6P">
<rect key="frame" x="164" y="280" width="86" height="37"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<color key="textColor" systemColor="labelColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oAv-pI-GrN">
<rect key="frame" x="88" y="325" width="238" height="63"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Add To Cart"/>
<connections>
<action selector="addToCartButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="wpQ-qK-0S1"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oiK-SR-ICR">
<rect key="frame" x="87" y="412" width="238" height="63"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Purchase"/>
<connections>
<action selector="purchaseButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="ZGF-RD-AUi"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oL8-3x-dTc">
<rect key="frame" x="87" y="500" width="238" height="63"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Send Custom Event"/>
<connections>
<action selector="customEventButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="CaV-GA-xkL"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillProportionally" spacing="16" translatesAutoresizingMaskIntoConstraints="NO" id="pId-os-gqS">
<rect key="frame" x="36" y="152.5" width="342" height="556"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="b6b-XP-epa">
<rect key="frame" x="0.0" y="0.0" width="342" height="180"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="ProductImage" translatesAutoresizingMaskIntoConstraints="NO" id="9xR-K8-uf1">
<rect key="frame" x="110" y="0.0" width="122" height="122"/>
</imageView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" text="T-Shirt" textAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="vq5-I3-w6P">
<rect key="frame" x="128" y="130" width="86" height="50"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<color key="textColor" systemColor="labelColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
</subviews>
<constraints>
<constraint firstAttribute="height" constant="180" id="kmO-R9-Ay6"/>
</constraints>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="36" translatesAutoresizingMaskIntoConstraints="NO" id="Zev-3g-6U9">
<rect key="frame" x="0.0" y="196" width="342" height="360"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oAv-pI-GrN">
<rect key="frame" x="0.0" y="0.0" width="342" height="63"/>
<constraints>
<constraint firstAttribute="height" constant="63" id="wXo-ak-iSl"/>
</constraints>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Add To Cart"/>
<connections>
<action selector="addToCartButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="wpQ-qK-0S1"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oiK-SR-ICR">
<rect key="frame" x="0.0" y="99" width="342" height="63"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Purchase"/>
<connections>
<action selector="purchaseButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="ZGF-RD-AUi"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oL8-3x-dTc">
<rect key="frame" x="0.0" y="198" width="342" height="63"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Send Custom Event"/>
<connections>
<action selector="customEventButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="CaV-GA-xkL"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="x1u-jl-cmP" userLabel="Add To Cart with Deeplink">
<rect key="frame" x="0.0" y="297" width="342" height="63"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="tinted" title="Add To Cart with Deeplink"/>
<connections>
<action selector="addToCartWithDeeplinkButtonPressed:" destination="Xj3-Ru-nwo" eventType="touchUpInside" id="06j-W6-5HX"/>
</connections>
</button>
</subviews>
</stackView>
</subviews>
</stackView>
</subviews>
<viewLayoutGuide key="safeArea" id="KyE-6k-rz2"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="KyE-6k-rz2" firstAttribute="trailing" secondItem="pId-os-gqS" secondAttribute="trailing" constant="36" id="3qv-hS-kpv"/>
<constraint firstItem="pId-os-gqS" firstAttribute="centerX" secondItem="KyE-6k-rz2" secondAttribute="centerX" id="8Wb-r3-0QI"/>
<constraint firstItem="pId-os-gqS" firstAttribute="top" secondItem="KyE-6k-rz2" secondAttribute="top" priority="999" id="Bzl-W3-Wz1"/>
<constraint firstItem="KyE-6k-rz2" firstAttribute="bottom" secondItem="pId-os-gqS" secondAttribute="bottom" priority="999" id="ILO-JJ-ljv"/>
<constraint firstItem="pId-os-gqS" firstAttribute="leading" secondItem="KyE-6k-rz2" secondAttribute="leading" constant="36" id="MJ3-FE-55x"/>
<constraint firstItem="pId-os-gqS" firstAttribute="centerY" secondItem="KyE-6k-rz2" secondAttribute="centerY" id="VZQ-ac-khU"/>
</constraints>
</view>
<tabBarItem key="tabBarItem" title="Product" image="cart.fill" catalog="system" id="Kez-RS-Jz1"/>
<navigationItem key="navigationItem" id="gu9-Nv-e2w"/>
Expand Down
23 changes: 15 additions & 8 deletions Example/Example/ProductViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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];
});
Expand Down
28 changes: 28 additions & 0 deletions Tests/TestCases/ATTNAddToCartEventTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
30 changes: 30 additions & 0 deletions Tests/TestCases/ATTNProductViewEventTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading

0 comments on commit 4b119a3

Please sign in to comment.