Skip to content

Commit

Permalink
Add tests for SEGStoreKitTracker and UIViewController+SEGScreen (segm…
Browse files Browse the repository at this point in the history
…entio#687)

* Adding OCMockito

* Adding test case for SEGStoreKitTracker

* Adding tests for UIViewController+SEGScreen
  • Loading branch information
tonyxiao authored Apr 29, 2017
1 parent 56ff9c2 commit 84f746e
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 10 deletions.
10 changes: 9 additions & 1 deletion Analytics.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
EAA542771EB4035400945DA7 /* TrackingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAA542761EB4035400945DA7 /* TrackingTests.swift */; };
EAA5427D1EB42B8C00945DA7 /* SEGReachability.h in Headers */ = {isa = PBXBuildFile; fileRef = EAA5427B1EB42B8C00945DA7 /* SEGReachability.h */; };
EAA5427E1EB42B8C00945DA7 /* SEGReachability.m in Sources */ = {isa = PBXBuildFile; fileRef = EAA5427C1EB42B8C00945DA7 /* SEGReachability.m */; };
EAA542801EB4382100945DA7 /* StoreKitTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAA5427F1EB4382100945DA7 /* StoreKitTrackerTests.swift */; };
EAA542821EB4410A00945DA7 /* ScreenTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAA542811EB4410A00945DA7 /* ScreenTrackerTests.swift */; };
EADEB8601DECD080005322DA /* Analytics.h in Headers */ = {isa = PBXBuildFile; fileRef = EADEB85E1DECD080005322DA /* Analytics.h */; settings = {ATTRIBUTES = (Public, ); }; };
EADEB86F1DECD0EF005322DA /* Analytics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EADEB85B1DECD080005322DA /* Analytics.framework */; };
EADEB8AE1DECD12B005322DA /* SEGAES256Crypto.h in Headers */ = {isa = PBXBuildFile; fileRef = EADEB8781DECD12B005322DA /* SEGAES256Crypto.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -92,6 +94,8 @@
EAA542761EB4035400945DA7 /* TrackingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrackingTests.swift; sourceTree = "<group>"; };
EAA5427B1EB42B8C00945DA7 /* SEGReachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SEGReachability.h; sourceTree = "<group>"; };
EAA5427C1EB42B8C00945DA7 /* SEGReachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SEGReachability.m; sourceTree = "<group>"; };
EAA5427F1EB4382100945DA7 /* StoreKitTrackerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = StoreKitTrackerTests.swift; path = AnalyticsTests/StoreKitTrackerTests.swift; sourceTree = SOURCE_ROOT; };
EAA542811EB4410A00945DA7 /* ScreenTrackerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScreenTrackerTests.swift; sourceTree = "<group>"; };
EADEB85B1DECD080005322DA /* Analytics.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Analytics.framework; sourceTree = BUILT_PRODUCTS_DIR; };
EADEB85E1DECD080005322DA /* Analytics.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Analytics.h; sourceTree = "<group>"; };
EADEB85F1DECD080005322DA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -242,9 +246,11 @@
EADEB86B1DECD0EF005322DA /* AnalyticsTests */ = {
isa = PBXGroup;
children = (
EADEB8FB1DECD521005322DA /* LSMatcher.h */,
EADEB8E11DECD335005322DA /* Utils */,
EADEB8FB1DECD521005322DA /* LSMatcher.h */,
EADEB8E01DECD335005322DA /* HTTPClientTest.swift */,
EAA542811EB4410A00945DA7 /* ScreenTrackerTests.swift */,
EAA5427F1EB4382100945DA7 /* StoreKitTrackerTests.swift */,
EADEB8E51DECD335005322DA /* FileStorageTest.swift */,
EADEB8E61DECD335005322DA /* UserDefaultsStorageTest.swift */,
EA8F09731E24C5C600B8B93F /* MiddlewareTests.swift */,
Expand Down Expand Up @@ -595,8 +601,10 @@
EADEB8EE1DECD335005322DA /* HTTPClientTest.swift in Sources */,
EADEB8F31DECD335005322DA /* AnalyticsTests.swift in Sources */,
EADEB8F11DECD335005322DA /* FileStorageTest.swift in Sources */,
EAA542801EB4382100945DA7 /* StoreKitTrackerTests.swift in Sources */,
EA8F09741E24C5C600B8B93F /* MiddlewareTests.swift in Sources */,
EADEB8EF1DECD335005322DA /* NSData+SEGGUNZIPP.m in Sources */,
EAA542821EB4410A00945DA7 /* ScreenTrackerTests.swift in Sources */,
EADEB8F01DECD335005322DA /* TestUtils.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
10 changes: 5 additions & 5 deletions Analytics/Classes/Internal/SEGStoreKitTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ - (void)trackTransaction:(SKPaymentTransaction *)transaction forProduct:(SKProdu
[self.analytics track:@"Order Completed" properties:@{
@"orderId" : transaction.transactionIdentifier,
@"affiliation" : @"App Store",
@"currency" : currency,
@"currency" : currency ?: [NSNull null],
@"products" : @[
@{
@"productId" : product.productIdentifier,
@"quantity" : @(transaction.payment.quantity),
@"sku" : transaction.transactionIdentifier,
@"price" : product.price,
@"name" : product.localizedTitle
@"quantity" : @(transaction.payment.quantity),
@"productId" : product.productIdentifier ?: [NSNull null],
@"price" : product.price ?: [NSNull null],
@"name" : product.localizedTitle ?: [NSNull null],
}
]
}];
Expand Down
1 change: 1 addition & 0 deletions Analytics/Classes/Internal/UIViewController+SEGScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
@interface UIViewController (SEGScreen)

+ (void)seg_swizzleViewDidAppear;
+ (UIViewController *)seg_topViewController;

@end
2 changes: 1 addition & 1 deletion Analytics/Classes/Internal/UIViewController+SEGScreen.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ + (UIViewController *)seg_topViewController:(UIViewController *)rootViewControll

- (void)seg_viewDidAppear:(BOOL)animated
{
UIViewController *top = [UIViewController seg_topViewController];
UIViewController *top = [[self class] seg_topViewController];
if (!top) {
SEGLog(@"Could not infer screen.");
return;
Expand Down
2 changes: 2 additions & 0 deletions AnalyticsTests/AnalyticsTests-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#import <Analytics/SEGFileStorage.h>
#import <Analytics/SEGUserDefaultsStorage.h>
#import <Analytics/NSData+SEGGZIP.h>
#import <Analytics/SEGStoreKitTracker.h>
#import <Analytics/UIViewController+SEGScreen.h>

#import "NSData+SEGGUNZIPP.h"
// Temp hack. We should fix the LSNocilla podspec to make this header publicly available
Expand Down
3 changes: 1 addition & 2 deletions AnalyticsTests/ContextTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class ContextTests: QuickSpec {

beforeEach {
let config = SEGAnalyticsConfiguration(writeKey: "foobar")
SEGAnalytics.setup(with: config)
analytics = SEGAnalytics.shared()
analytics = SEGAnalytics(configuration: config)
}

it("throws when used incorrectly") {
Expand Down
49 changes: 49 additions & 0 deletions AnalyticsTests/ScreenTrackerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// ScreenTrackerTests.swift
// Analytics
//
// Created by Tony Xiao on 9/20/16.
// Copyright © 2016 Segment. All rights reserved.
//

import Quick
import Nimble
import Analytics

var staticMockVC: UIViewController!

class mockViewController: UIViewController {
override class func seg_top() -> UIViewController? {
return staticMockVC
}
}

class ScreenTrackerTests: QuickSpec {
override func spec() {

var test: TestMiddleware!

beforeEach {
let config = SEGAnalyticsConfiguration(writeKey: "foobar")
test = TestMiddleware()
config.middlewares = [test]
// This is really not ideal to be using a global
// singleton, but don't have better choices atm
SEGAnalytics.setup(with: config)
}

it("tracks screen with correct title") {
UIViewController.seg_swizzleViewDidAppear()
staticMockVC = mockViewController()
staticMockVC.title = "Mock Screen"
staticMockVC.viewDidLoad()
staticMockVC.viewWillAppear(true)
staticMockVC.viewDidAppear(true)

let payload = test.lastContext?.payload as? SEGScreenPayload
expect(payload?.name) == "Mock Screen"
}

}

}
71 changes: 71 additions & 0 deletions AnalyticsTests/StoreKitTrackerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// StoreKitTrackerTest.swift
// Analytics
//
// Created by Tony Xiao on 9/20/16.
// Copyright © 2016 Segment. All rights reserved.
//

import Quick
import Nimble
import Analytics

class mockTransaction: SKPaymentTransaction {
override var transactionIdentifier: String? {
return "tid"
}
override var transactionState: SKPaymentTransactionState {
return SKPaymentTransactionState.purchased
}
override var payment: SKPayment {
return mockPayment()
}
}

class mockPayment: SKPayment {
override var productIdentifier: String { return "pid" }
}

class mockProduct: SKProduct {
override var productIdentifier: String { return "pid" }
override var price: NSDecimalNumber { return 3 }
override var localizedTitle: String { return "lt" }

}

class mockProductResponse: SKProductsResponse {
override var products: [SKProduct] {
return [mockProduct()]
}
}

class StoreKitTrackerTests: QuickSpec {
override func spec() {

var test: TestMiddleware!
var tracker: SEGStoreKitTracker!
var analytics: SEGAnalytics!

beforeEach {
let config = SEGAnalyticsConfiguration(writeKey: "foobar")
test = TestMiddleware()
config.middlewares = [test]
analytics = SEGAnalytics(configuration: config)
tracker = SEGStoreKitTracker.trackTransactions(for: analytics)
}

it("SKPaymentQueue Observer") {
let transaction = mockTransaction()
expect(transaction.transactionIdentifier) == "tid"
tracker.paymentQueue(SKPaymentQueue(), updatedTransactions: [transaction])

tracker.productsRequest(SKProductsRequest(), didReceive: mockProductResponse())

let payload = test.lastContext?.payload as? SEGTrackPayload

expect(payload?.event) == "Order Completed"
}

}

}
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ SPEC CHECKSUMS:
Quick: 8024e4a47e6cc03a9d5245ef0948264fc6d27cff
SwiftTryCatch: fb6d2b34abe48efd69578dac919293a44f95b481

PODFILE CHECKSUM: e7aa6926a6e0b35e28a27b2d03de5957d7a8f09a
PODFILE CHECKSUM: f411c2db61310dba921a4f6a1cac8592b4318ae3

COCOAPODS: 1.2.1

0 comments on commit 84f746e

Please sign in to comment.