Skip to content

Commit 217d445

Browse files
committed
Added more unit tests for iOS.
1 parent a367b09 commit 217d445

9 files changed

+612
-59
lines changed

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHandler.m

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ - (void)remoteNotificationRegisteredError:(nonnull NSNotification *)notification
6161
{
6262
NSError *error = notification.userInfo[RCTUserInfoError];
6363
NSDictionary *errorDetails = @{
64-
@"message": error.localizedDescription,
65-
@"code": @(error.code),
66-
@"details": error.userInfo,
67-
};
64+
@"message": error.localizedDescription,
65+
@"code": @(error.code),
66+
@"details": error.userInfo,
67+
};
6868
[_eventEmitter sendEventWithName:RCTRemoteNotificationRegisteredError
6969
body:errorDetails];
7070
}
@@ -81,10 +81,10 @@ - (void)azureNotificationHubRegisteredError:(nonnull NSNotification *)notificati
8181
{
8282
NSError *error = notification.userInfo[RCTUserInfoError];
8383
NSDictionary *errorDetails = @{
84-
@"message": error.localizedDescription,
85-
@"code": @(error.code),
86-
@"details": error.userInfo,
87-
};
84+
@"message": error.localizedDescription,
85+
@"code": @(error.code),
86+
@"details": error.userInfo,
87+
};
8888

8989
[_eventEmitter sendEventWithName:RCTAzureNotificationHubRegisteredError
9090
body:errorDetails];
@@ -98,14 +98,14 @@ - (void)userNotificationSettingsRegistered:(nonnull NSNotification *)notificatio
9898
{
9999
return;
100100
}
101-
101+
102102
UIUserNotificationSettings *notificationSettings = notification.userInfo[RCTUserInfoNotificationSettings];
103103
NSDictionary *notificationTypes = @{
104-
RCTNotificationTypeAlert: @((notificationSettings.types & UIUserNotificationTypeAlert) > 0),
105-
RCTNotificationTypeSound: @((notificationSettings.types & UIUserNotificationTypeSound) > 0),
106-
RCTNotificationTypeBadge: @((notificationSettings.types & UIUserNotificationTypeBadge) > 0),
107-
};
108-
104+
RCTNotificationTypeAlert: @((notificationSettings.types & UIUserNotificationTypeAlert) > 0),
105+
RCTNotificationTypeSound: @((notificationSettings.types & UIUserNotificationTypeSound) > 0),
106+
RCTNotificationTypeBadge: @((notificationSettings.types & UIUserNotificationTypeBadge) > 0),
107+
};
108+
109109
resolve(notificationTypes);
110110
}
111111

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHub.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ extern NSString *const RCTErrorInvalidArguments;
4646
extern NSString *const RCTErrorMissingConnectionString;
4747
extern NSString *const RCTErrorMissingHubName;
4848

49+
// Completion type used in Azure Notification Hub's native methods
50+
typedef void (^RCTNativeCompletion)(NSError *error);
51+
4952
#endif /* RCTAzureNotificationHub_h */

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHubManager.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,42 @@
2626
// Required for the registrationError event, invoked from AppDelegate
2727
+ (void)didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error;
2828

29+
// Set application icon badge number
30+
- (void)setApplicationIconBadgeNumber:(NSInteger)number;
31+
32+
// Get application icon badge number
33+
- (void)getApplicationIconBadgeNumber:(nonnull RCTResponseSenderBlock)callback;
34+
35+
// Request notification permissions
36+
- (void)requestPermissions:(nonnull NSDictionary *)permissions
37+
resolver:(nonnull RCTPromiseResolveBlock)resolve
38+
rejecter:(nonnull RCTPromiseRejectBlock)reject;
39+
40+
// Abandon notification permissions
41+
- (void)abandonPermissions;
42+
43+
// Check notification permissions
44+
- (void)checkPermissions:(nonnull RCTResponseSenderBlock)callback;
45+
46+
// Present local notification
47+
- (void)presentLocalNotification:(nonnull UILocalNotification *)notification;
48+
49+
// Schedule local notification
50+
- (void)scheduleLocalNotification:(nonnull UILocalNotification *)notification;
51+
52+
// Cancel all local notifications
53+
- (void)cancelAllLocalNotifications;
54+
55+
// Cancel local notifications
56+
- (void)cancelLocalNotifications:(nonnull NSDictionary<NSString *, id> *)userInfo;
57+
58+
// Get initial notification
59+
- (void)getInitialNotification:(nonnull RCTPromiseResolveBlock)resolve
60+
reject:(nonnull __unused RCTPromiseRejectBlock)reject;
61+
62+
// Get scheduled local notifications
63+
- (void)getScheduledLocalNotifications:(nonnull RCTResponseSenderBlock)callback;
64+
2965
// Register with Azure Notification Hub
3066
- (void)register:(nonnull NSString *)deviceToken
3167
config:(nonnull NSDictionary *)config

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHubManager.m

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
175175
}
176176

177177
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions
178-
resolver:(RCTPromiseResolveBlock)resolve
179-
rejecter:(RCTPromiseRejectBlock)reject)
178+
resolver:(RCTPromiseResolveBlock)resolve
179+
rejecter:(RCTPromiseRejectBlock)reject)
180180
{
181181
if (RCTRunningInAppExtension())
182182
{
@@ -197,8 +197,8 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
197197
UIApplication *app = RCTSharedApplication();
198198
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
199199
{
200-
UIUserNotificationSettings *notificationSettings =
201-
[UIUserNotificationSettings settingsForTypes:(NSUInteger)types categories:nil];
200+
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:(NSUInteger)types
201+
categories:nil];
202202
[app registerUserNotificationSettings:notificationSettings];
203203
}
204204
else
@@ -270,7 +270,7 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
270270
}
271271

272272
RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve
273-
reject:(__unused RCTPromiseRejectBlock)reject)
273+
reject:(__unused RCTPromiseRejectBlock)reject)
274274
{
275275
NSMutableDictionary<NSString *, id> *initialNotification =
276276
[self.bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] mutableCopy];
@@ -326,11 +326,11 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
326326
hubName:_hubName];
327327

328328
// Register for native notifications
329-
dispatch_async(dispatch_get_main_queue(), ^
329+
[RCTAzureNotificationHubUtil runOnMainThread:^
330330
{
331-
[hub registerNativeWithDeviceToken:deviceToken
332-
tags:_tags
333-
completion:^(NSError* error)
331+
[hub registerNativeWithDeviceToken:deviceToken
332+
tags:_tags
333+
completion:^(NSError* error)
334334
{
335335
if (error != nil)
336336
{
@@ -345,7 +345,7 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
345345
userInfo:@{RCTUserInfoSuccess: @YES}];
346346
}
347347
}];
348-
});
348+
}];
349349
}
350350

351351
RCT_EXPORT_METHOD(unregister:(RCTPromiseResolveBlock)resolve
@@ -362,9 +362,9 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
362362
hubName:_hubName];
363363

364364
// Unregister for native notifications
365-
dispatch_async(dispatch_get_main_queue(), ^
365+
[RCTAzureNotificationHubUtil runOnMainThread:^
366366
{
367-
[hub unregisterNativeWithCompletion:^(NSError *error)
367+
[hub unregisterNativeWithCompletion:^(NSError *error)
368368
{
369369
if (error != nil)
370370
{
@@ -373,7 +373,7 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification
373373
userInfo:@{RCTUserInfoError: error}];
374374
}
375375
}];
376-
});
376+
}];
377377
}
378378

379379
@end

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHubUtil.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
// Get notification types with permissions
2626
+ (UIUserNotificationType)getNotificationTypesWithPermissions:(nullable NSDictionary *)permissions;
2727

28+
// Run block on the main thread
29+
+ (void)runOnMainThread:(dispatch_block_t)block;
30+
2831
@end
2932

3033
#endif /* RCTAzureNotificationHubUtil_h */

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHubUtil.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,10 @@ + (UIUserNotificationType)getNotificationTypesWithPermissions:(nullable NSDictio
9090
return types;
9191
}
9292

93+
// Run block on the main thread
94+
+ (void)runOnMainThread:(dispatch_block_t)block
95+
{
96+
dispatch_async(dispatch_get_main_queue(), block);
97+
}
98+
9399
@end

sample/ios/ReactNativeAzureNotificationHubSampleTests/RCTAzureNotificationHandlerTests.m

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,118 @@
66
*/
77

88
#import <XCTest/XCTest.h>
9+
910
#import <RNAzureNotificationHub/RCTAzureNotificationHub.h>
10-
#import <RNAzureNotificationHub/RCTAzureNotificationHubManager.h>
11+
#import <RNAzureNotificationHub/RCTAzureNotificationHandler.h>
12+
#import <RNAzureNotificationHub/RCTAzureNotificationHubUtil.h>
13+
14+
#import "React/RCTEventEmitter.h"
1115

1216
@import OCMock;
1317

1418
@interface RCTAzureNotificationHandlerTests : XCTestCase
1519
@end
1620

1721
@implementation RCTAzureNotificationHandlerTests
22+
{
23+
@private
24+
RCTAzureNotificationHandler *_notificationHandler;
25+
NSMutableDictionary *_userInfo;
26+
id _eventEmitter;
27+
}
1828

1929
- (void)setUp
2030
{
2131
[super setUp];
32+
33+
_eventEmitter = OCMClassMock([RCTEventEmitter class]);
34+
_notificationHandler = [[RCTAzureNotificationHandler alloc] initWithEventEmitter:_eventEmitter];
35+
NSArray *keys = [NSArray arrayWithObjects:@"userInfoKey", nil];
36+
NSArray *objects = [NSArray arrayWithObjects:@"userInfoObject", nil];
37+
_userInfo = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys];
38+
}
39+
40+
- (void)testLocalNotificationReceived
41+
{
42+
NSNotification* notification = [NSNotification notificationWithName:@"Notification" object:_userInfo userInfo:_userInfo];
43+
44+
[_notificationHandler localNotificationReceived:notification];
45+
46+
OCMVerify([_eventEmitter sendEventWithName:RCTLocalNotificationReceived
47+
body:_userInfo]);
48+
}
49+
50+
- (void)testRemoteNotificationReceived
51+
{
52+
NSNotification* notification = [NSNotification notificationWithName:@"Notification" object:_userInfo userInfo:_userInfo];
53+
_userInfo[RCTUserInfoRemote] = @YES;
54+
55+
[_notificationHandler remoteNotificationReceived:notification];
56+
57+
OCMVerify([_eventEmitter sendEventWithName:RCTRemoteNotificationReceived
58+
body:_userInfo]);
59+
}
60+
61+
- (void)testRemoteNotificationRegistered
62+
{
63+
NSNotification* notification = [NSNotification notificationWithName:@"Notification" object:_userInfo userInfo:_userInfo];
64+
65+
[_notificationHandler remoteNotificationRegistered:notification];
66+
67+
OCMVerify([_eventEmitter sendEventWithName:RCTRemoteNotificationRegistered
68+
body:_userInfo]);
69+
}
70+
71+
- (void)testRemoteNotificationRegisteredError
72+
{
73+
NSDictionary *errorUserInfo = [[NSDictionary alloc]
74+
initWithObjectsAndKeys:NSLocalizedDescriptionKey, NSLocalizedDescriptionKey, nil];
75+
76+
NSError* error = [NSError errorWithDomain:@"Error domain"
77+
code:100
78+
userInfo:errorUserInfo];
79+
80+
NSArray *keys = [NSArray arrayWithObjects:RCTUserInfoError, nil];
81+
NSArray *objects = [NSArray arrayWithObjects:error, nil];
82+
NSDictionary *userInfo = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys];
83+
NSNotification* notification = [NSNotification notificationWithName:@"Notification" object:userInfo userInfo:userInfo];
84+
NSDictionary *expectedErrorDetails = @{
85+
@"message": NSLocalizedDescriptionKey,
86+
@"code": [NSNumber numberWithInt:100],
87+
@"details": errorUserInfo
88+
};
89+
90+
[_notificationHandler remoteNotificationRegisteredError:notification];
91+
92+
OCMVerify([_eventEmitter sendEventWithName:RCTRemoteNotificationRegisteredError
93+
body:expectedErrorDetails]);
94+
}
95+
96+
- (void)testUserNotificationSettingsRegistered
97+
{
98+
__block NSDictionary *notificationTypes = nil;
99+
RCTPromiseResolveBlock resolver = ^(NSDictionary *callbackNotificationTypes)
100+
{
101+
notificationTypes = callbackNotificationTypes;
102+
};
103+
104+
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
105+
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(NSUInteger)types
106+
categories:nil];
107+
108+
NSArray *keys = [NSArray arrayWithObjects:RCTUserInfoNotificationSettings, RCTUserInfoResolveBlock, nil];
109+
NSArray *objects = [NSArray arrayWithObjects:settings, resolver, nil];
110+
NSDictionary *userInfo = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys];
111+
NSNotification* notification = [NSNotification notificationWithName:@"Notification" object:userInfo userInfo:userInfo];
112+
NSDictionary *expectedNotificationTypes = @{
113+
RCTNotificationTypeAlert: @YES,
114+
RCTNotificationTypeSound: @NO,
115+
RCTNotificationTypeBadge: @YES
116+
};
117+
118+
[_notificationHandler userNotificationSettingsRegistered:notification];
119+
120+
XCTAssertEqualObjects(notificationTypes, expectedNotificationTypes);
22121
}
23122

24123
@end

0 commit comments

Comments
 (0)