-
-
Notifications
You must be signed in to change notification settings - Fork 874
/
Copy pathAnalyticsControllerTests.m
158 lines (127 loc) · 6.36 KB
/
AnalyticsControllerTests.m
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <OCMock/OCMock.h>
#import "BFTask+Private.h"
#import "PFAnalyticsController.h"
#import "PFCommandResult.h"
#import "PFEventuallyQueue.h"
#import "PFRESTCommand.h"
#import "PFTestCase.h"
@interface AnalyticsControllerTests : PFTestCase
@end
@implementation AnalyticsControllerTests
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (id<PFEventuallyQueueProvider>)dataSourceMock {
id queueMock = PFStrictClassMock([PFEventuallyQueue class]);
id dataSource = PFStrictProtocolMock(@protocol(PFEventuallyQueueProvider));
OCMStub([dataSource eventuallyQueue]).andReturn(queueMock);
return dataSource;
}
- (id<PFEventuallyQueueProvider>)dataSourceMockWithCommandResult:(PFCommandResult *)result {
id dataSource = [self dataSourceMock];
id queue = [dataSource eventuallyQueue];
OCMStub([queue enqueueCommandInBackground:OCMOCK_ANY]).andReturn([BFTask taskWithResult:result]);
return dataSource;
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testConstructors {
id dataSource = [self dataSourceMock];
PFAnalyticsController *controller = [[PFAnalyticsController alloc] initWithDataSource:dataSource];
XCTAssertNotNil(controller);
XCTAssertEqual((id)controller.dataSource, dataSource);
controller = [PFAnalyticsController controllerWithDataSource:dataSource];
XCTAssertNotNil(controller);
XCTAssertEqual((id)controller.dataSource, dataSource);
}
- (void)testTrackEventWithInvalidParameters {
id dataSource = [self dataSourceMockWithCommandResult:nil];
PFAnalyticsController *controller = [PFAnalyticsController controllerWithDataSource:dataSource];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
PFAssertThrowsInvalidArgumentException([controller trackEventAsyncWithName:nil dimensions:nil sessionToken:nil]);
#pragma clang diagnostic pop
PFAssertThrowsInvalidArgumentException([controller trackEventAsyncWithName:@" " dimensions:nil sessionToken:nil]);
PFAssertThrowsInvalidArgumentException([controller trackEventAsyncWithName:@"\n" dimensions:nil sessionToken:nil]);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-literal-conversion"
PFAssertThrowsInvalidArgumentException([controller trackEventAsyncWithName:@"f"
dimensions:@{ @2 : @"five" }
sessionToken:nil]);
PFAssertThrowsInvalidArgumentException([controller trackEventAsyncWithName:@"f"
dimensions:@{ @"num" : @5 }
sessionToken:nil]);
#pragma clang diagnostic pop
}
- (void)testTrackEventParameters {
id dataSource = [self dataSourceMock];
id queue = [dataSource eventuallyQueue];
OCMExpect([queue enqueueCommandInBackground:[OCMArg checkWithBlock:^BOOL(id obj) {
PFRESTCommand *command = obj;
XCTAssertNotEqual([command.httpPath rangeOfString:@"boom"].location, NSNotFound);
XCTAssertEqualObjects(command.parameters[@"dimensions"], @{ @"yarr" : @"yolo" });
XCTAssertEqualObjects(command.sessionToken, @"argh");
return YES;
}]]);
PFAnalyticsController *controller = [PFAnalyticsController controllerWithDataSource:dataSource];
[[controller trackEventAsyncWithName:@"boom"
dimensions:@{ @"yarr" : @"yolo" }
sessionToken:@"argh"] waitUntilFinished];
OCMVerifyAll(queue);
}
- (void)testTrackEventResult {
PFCommandResult *result = [PFCommandResult commandResultWithResult:@{}
resultString:nil
httpResponse:nil];
id dataSource = [self dataSourceMockWithCommandResult:result];
PFAnalyticsController *controller = [PFAnalyticsController controllerWithDataSource:dataSource];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[controller trackEventAsyncWithName:@"a"
dimensions:nil
sessionToken:nil] continueWithSuccessBlock:^id(BFTask *task) {
XCTAssertNil(task.result);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testTrackAppOpenedParameters {
id dataSource = [self dataSourceMock];
id queue = [dataSource eventuallyQueue];
OCMExpect([queue enqueueCommandInBackground:[OCMArg checkWithBlock:^BOOL(id obj) {
PFRESTCommand *command = obj;
XCTAssertNotEqual([command.httpPath rangeOfString:@"AppOpened"].location, NSNotFound);
XCTAssertNotNil(command.parameters[@"push_hash"]);
XCTAssertEqualObjects(command.sessionToken, @"argh");
return YES;
}]]);
PFAnalyticsController *controller = [PFAnalyticsController controllerWithDataSource:dataSource];
[[controller trackAppOpenedEventAsyncWithRemoteNotificationPayload:@{ @"aps" : @{@"alert" : @"yolo"} }
sessionToken:@"argh"] waitUntilFinished];
OCMVerifyAll(queue);
}
- (void)testTrackAppOpenedResult {
PFCommandResult *result = [PFCommandResult commandResultWithResult:@{}
resultString:nil
httpResponse:nil];
id dataSource = [self dataSourceMockWithCommandResult:result];
PFAnalyticsController *controller = [PFAnalyticsController controllerWithDataSource:dataSource];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[controller trackAppOpenedEventAsyncWithRemoteNotificationPayload:nil
sessionToken:nil] continueWithSuccessBlock:^id(BFTask *task) {
XCTAssertNil(task.result);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
@end