-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathRNCommandsHandlerIntegrationTest.m
124 lines (98 loc) · 5.94 KB
/
RNCommandsHandlerIntegrationTest.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
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import <objc/runtime.h>
#import "RNCommandsHandler.h"
#import "RNNotificationsStore.h"
@interface RNCommandsHandlerIntegrationTest : XCTestCase
@property (nonatomic, retain) RNCommandsHandler* uut;
@property (nonatomic, retain) id notificationCenter;
@property (nonatomic, retain) id mockUserNotifications;
@end
@implementation RNCommandsHandlerIntegrationTest
- (void)setUp {
_mockUserNotifications = [OCMockObject mockForProtocol:[self getMockUserNotificationCenterProtocol]];
id notificationCenterMock = OCMClassMock([UNUserNotificationCenter class]);
OCMStub(ClassMethod([notificationCenterMock currentNotificationCenter])).andReturn(_mockUserNotifications);
UIApplication* sharedApplication = [OCMockObject mockForClass:[UIApplication class]];
id mockedApplicationClass = OCMClassMock([UIApplication class]);
OCMStub(ClassMethod([mockedApplicationClass sharedApplication])).andReturn(sharedApplication);
_uut = [RNCommandsHandler new];
_notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
}
- (void)testRequestPermissions_userAuthorizedPermissions {
UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
UNNotificationSettings* settings = [UNNotificationSettings new];
[settings setValue:@(UNAuthorizationStatusAuthorized) forKey:@"authorizationStatus"];
[[_notificationCenter expect] requestAuthorizationWithOptions:authOptions completionHandler:[OCMArg invokeBlockWithArgs:@(YES), [NSNull null], nil]];
[[_notificationCenter expect] getNotificationSettingsWithCompletionHandler:[OCMArg invokeBlockWithArgs:settings, nil]];
[[(id)[UIApplication sharedApplication] expect] registerForRemoteNotifications];
[_uut requestPermissions:@{}];
[_notificationCenter verify];
}
- (void)testRequestPermissions_userAuthorizedPermissionsExtraOptions {
UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge |
UNAuthorizationOptionSound |
UNAuthorizationOptionAlert |
UNAuthorizationOptionAnnouncement |
UNAuthorizationOptionProvidesAppNotificationSettings |
UNAuthorizationOptionCriticalAlert |
UNAuthorizationOptionProvisional |
UNAuthorizationOptionCarPlay);
UNNotificationSettings* settings = [UNNotificationSettings new];
[settings setValue:@(UNAuthorizationStatusAuthorized) forKey:@"authorizationStatus"];
[[_notificationCenter expect] requestAuthorizationWithOptions:authOptions completionHandler:[OCMArg invokeBlockWithArgs:@(YES), [NSNull null], nil]];
[[_notificationCenter expect] getNotificationSettingsWithCompletionHandler:[OCMArg invokeBlockWithArgs:settings, nil]];
[[(id)[UIApplication sharedApplication] expect] registerForRemoteNotifications];
[_uut requestPermissions:@{@"carPlay": @YES,
@"criticalAlert": @YES,
@"providesAppNotificationSettings": @YES,
@"announcement": @YES,
@"provisional": @YES}];
[_notificationCenter verify];
}
- (void)testRequestPermissions_userDeniedPermissions {
UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
UNNotificationSettings* settings = [UNNotificationSettings new];
[settings setValue:@(UNAuthorizationStatusDenied) forKey:@"authorizationStatus"];
[[_notificationCenter expect] requestAuthorizationWithOptions:authOptions completionHandler:[OCMArg invokeBlockWithArgs:@(YES), [NSNull null], nil]];
[[_notificationCenter expect] getNotificationSettingsWithCompletionHandler:[OCMArg invokeBlockWithArgs:settings, nil]];
[[(id)[UIApplication sharedApplication] reject] registerForRemoteNotifications];
[_uut requestPermissions:@{}];
[_notificationCenter verify];
}
- (void)testSetCategories_shouldSetCategories {
NSArray* json = @[@{@"identifier": @"categoryId", @"actions": @[@{@"identifier" : @"actionId", @"activationMode": @"foreground"}]}];
[[_notificationCenter expect] setNotificationCategories:[OCMArg checkWithBlock:^BOOL(NSMutableSet<UNNotificationCategory *>* categories) {
UNNotificationCategory* category = categories.allObjects.firstObject;
UNNotificationAction* action = category.actions.firstObject;
return ([category.identifier isEqualToString:@"categoryId"] &&
[action.identifier isEqualToString:@"actionId"] &&
action.options == UNNotificationActionOptionForeground);
}]];
[_uut setCategories:json];
[_notificationCenter verify];
}
- (void)testGetInitialNotification {
NSDictionary* initialNotification = @{};
[[RNNotificationsStore sharedInstance] setInitialNotification:initialNotification];
[self.uut getInitialNotification:^(id result) {
XCTAssertEqual(result, initialNotification);
} reject:^(NSString *code, NSString *message, NSError *error) {
}];
}
- (Protocol *)getMockUserNotificationCenterProtocol {
Protocol *aProtocol = objc_getProtocol("MockUserNotificationCenter");
if (!aProtocol) {
aProtocol = objc_allocateProtocol("MockUserNotificationCenter");
unsigned int methodCount = 0;
Method *methods = class_copyMethodList([UNUserNotificationCenter class], &methodCount);
for (unsigned int i = 0; i < methodCount; i++) {
Method method = methods[i];
protocol_addMethodDescription(aProtocol, method_getName(method), method_getTypeEncoding(method), YES, YES);
}
free(methods);
objc_registerProtocol(aProtocol);
}
return aProtocol;
}
@end