Skip to content

Commit 8f9ffd9

Browse files
committed
task: quick timing fix for app launch in tests
1 parent 80b22fa commit 8f9ffd9

File tree

4 files changed

+25
-60
lines changed

4 files changed

+25
-60
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ ObjCStarter/Podfile.lock
3737
Package.resolved
3838

3939
Pods
40+
Podfile.lock

CleverTapSDKTests/BaseTestCase.m

+23-14
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,44 @@ + (void)initialize {
2121

2222
- (void)setUp {
2323
[CleverTap setDebugLevel:3];
24-
[CleverTap setCredentialsWithAccountID:@"test" token:@"test" region:@"eu1"];
24+
BOOL cleverTapInitialized = [[CleverTap sharedInstance] profileGetCleverTapID] != nil;
25+
26+
if (!cleverTapInitialized) {
27+
[CleverTap setCredentialsWithAccountID:@"test" token:@"test" region:@"eu1"];
28+
}
29+
2530
self.eventDetails = [NSMutableArray array];
26-
2731
self.cleverTapInstance = [CleverTap sharedInstance];
2832
self.additionalInstance = [CleverTap instanceWithConfig:[[CleverTapInstanceConfig alloc]initWithAccountId:@"test" accountToken:@"test" accountRegion:@"eu1"]];
2933
self.responseJson = @{ @"key1": @"value1", @"key2": @[@"value2A", @"value2B"] }; // TODO
3034
self.responseHeaders = @{@"Content-Type":@"application/json"};
3135

3236

33-
// [HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
34-
// return [request.URL.host isEqualToString:@"eu1.clevertap-prod.com"];
35-
// } withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
36-
//
37-
//
38-
// return [HTTPStubsResponse responseWithJSONObject:self.responseJson statusCode:200 headers:self.responseHeaders];
39-
// }];
37+
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
38+
return [request.URL.host isEqualToString:@"eu1.clevertap-prod.com"];
39+
} withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
40+
return [HTTPStubsResponse responseWithJSONObject:self.responseJson statusCode:200 headers:self.responseHeaders];
41+
}];
4042

4143
[HTTPStubs onStubActivation:^(NSURLRequest * _Nonnull request, id<HTTPStubsDescriptor> _Nonnull stub, HTTPStubsResponse * _Nonnull responseStub) {
4244
NSArray *data = [NSJSONSerialization JSONObjectWithData:[request OHHTTPStubs_HTTPBody] options:NSJSONReadingMutableContainers error:nil];
43-
4445
self.lastBatchHeader = [data objectAtIndex:0];
4546
self.lastEvent = [data objectAtIndex:1];
4647
[self.eventDetails addObject:[[EventDetail alloc]initWithEvent:[data objectAtIndex:1] name:stub.name]];
4748
NSLog(@"LAST EVENT");
4849
NSLog(@"%@", self.lastEvent);
4950
}];
50-
[CleverTap notfityTestAppLaunch];
51+
if (!cleverTapInitialized) {
52+
[CleverTap notfityTestAppLaunch];
53+
XCTestExpectation *expectation = [self expectationWithDescription:@"Wait For App Launch"];
54+
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
55+
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
56+
[expectation fulfill];
57+
});
58+
[self waitForExpectationsWithTimeout:2.0 handler:^(NSError *error) {
59+
// no-op
60+
}];
61+
}
5162
}
5263

5364
- (void)tearDown {
@@ -66,8 +77,6 @@ - (void)stubRequestsWithName:(NSString*)name {
6677
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
6778
return [request.URL.host isEqualToString:@"eu1.clevertap-prod.com"];
6879
} withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
69-
70-
7180
return [HTTPStubsResponse responseWithJSONObject:self.responseJson statusCode:200 headers:self.responseHeaders];
7281
}].name = name;
7382
}
@@ -107,7 +116,7 @@ - (EventDetail*)getFilteredEventWithStubName: (NSString*)stubName eventName: (NS
107116
return nil;
108117
}];
109118
NSArray *filteredEvents = [self.eventDetails filteredArrayUsingPredicate: pred];
110-
return (EventDetail*)filteredEvents[0];
119+
return (filteredEvents && [filteredEvents count] > 0) ? (EventDetail*)filteredEvents[0] : nil;
111120
}
112121

113122
@end

CleverTapSDKTests/EventTests.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ - (void)test_event_recorded {
6969
}];
7070
}
7171

72-
- (void)test_chared_event_recorded {
72+
- (void)test_charged_event_recorded {
7373
NSString *stubName = @"Test Charged Record Event";
7474
[self stubRequestsWithName:stubName];
7575
XCTestExpectation *expectation = [self expectationWithDescription:@"Test Charged Record Event"];

Podfile.lock

-45
This file was deleted.

0 commit comments

Comments
 (0)