-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathTrackingTests.swift
127 lines (112 loc) · 4.62 KB
/
TrackingTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// TrackingTests.swift
// Analytics
//
// Created by Tony Xiao on 9/16/16.
// Copyright © 2016 Segment. All rights reserved.
//
import Segment
import XCTest
class TrackingTests: XCTestCase {
var passthrough: PassthroughMiddleware!
var analytics: Analytics!
override func setUp() {
super.setUp()
let config = AnalyticsConfiguration(writeKey: "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE")
passthrough = PassthroughMiddleware()
config.sourceMiddleware = [
passthrough,
]
analytics = Analytics(configuration: config)
}
override func tearDown() {
super.tearDown()
analytics.reset()
}
func testHandlesIdentify() {
analytics.identify("testUserId1", traits: [
"firstName": "Peter"
])
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.identify)
let identify = passthrough.lastContext?.payload as? IdentifyPayload
XCTAssertEqual(identify?.userId, "testUserId1")
XCTAssertNotNil(identify?.anonymousId)
XCTAssertEqual(identify?.traits?["firstName"] as? String, "Peter")
}
func testHandlesIdentifyAndUserIdPass() {
analytics.identify("testUserId1", traits: [
"firstName": "Peter"
])
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.identify)
let identify = passthrough.lastContext?.payload as? IdentifyPayload
XCTAssertEqual(identify?.userId, "testUserId1")
XCTAssertNotNil(identify?.anonymousId)
XCTAssertEqual(identify?.traits?["firstName"] as? String, "Peter")
XCTAssertEqual(identify?.traits?["userId"] as? String, "testUserId1")
analytics.identify("testUserId1")
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.identify)
let identify2 = passthrough.lastContext?.payload as? IdentifyPayload
XCTAssertEqual(identify2?.userId, "testUserId1")
XCTAssertEqual(identify2?.traits?["userId"] as? String, "testUserId1")
}
func testHandlesIdentifyWithCustomAnonymousId() {
analytics.identify("testUserId1", traits: [
"firstName": "Peter"
], options: [
"anonymousId": "a_custom_anonymous_id"
])
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.identify)
let identify = passthrough.lastContext?.payload as? IdentifyPayload
XCTAssertEqual(identify?.userId, "testUserId1")
XCTAssertEqual(identify?.anonymousId, "a_custom_anonymous_id")
XCTAssertEqual(identify?.traits?["firstName"] as? String, "Peter")
}
func testHandlesTrack() {
analytics.track("User Signup", properties: [
"method": "SSO"
], options: [
"context": [
"device": [
"token": "1234"
]
]
])
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.track)
let payload = passthrough.lastContext?.payload as? TrackPayload
XCTAssertEqual(payload?.event, "User Signup")
XCTAssertEqual(payload?.properties?["method"] as? String, "SSO")
}
func testHandlesAlias() {
analytics.alias("persistentUserId")
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.alias)
let payload = passthrough.lastContext?.payload as? AliasPayload
XCTAssertEqual(payload?.theNewId, "persistentUserId")
}
func testHandlesScreen() {
analytics.screen("Home", category:"test", properties: [
"referrer": "Google"
])
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.screen)
let screen = passthrough.lastContext?.payload as? ScreenPayload
XCTAssertEqual(screen?.name, "Home")
XCTAssertEqual(screen?.category, "test")
XCTAssertEqual(screen?.properties?["referrer"] as? String, "Google")
}
func testHandlesGroup() {
analytics.group("acme-company", traits: [
"employees": 2333
])
XCTAssertEqual(passthrough.lastContext?.eventType, EventType.group)
let payload = passthrough.lastContext?.payload as? GroupPayload
XCTAssertEqual(payload?.groupId, "acme-company")
XCTAssertEqual(payload?.traits?["employees"] as? Int, 2333)
}
func testHandlesNullValues() {
analytics.track("null test", properties: [
"nullTest": NSNull()
])
let payload = passthrough.lastContext?.payload as? TrackPayload
let isNull = (payload?.properties?["nullTest"] is NSNull)
XCTAssert(isNull)
}
}