Skip to content

Commit 38f699a

Browse files
authored
Merge pull request #96 from CatalystCode/thcao/iOS-tests
[iOS] Adding initial unit tests
2 parents 54c2b22 + bac9ad8 commit 38f699a

File tree

5 files changed

+88
-49
lines changed

5 files changed

+88
-49
lines changed

azure-pipelines.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,20 @@ steps:
6969
xcWorkspacePath: '../sample/ios/ReactNativeAzureNotificationHubSample.xcworkspace'
7070
xcodeVersion: '10'
7171
signingOption: 'nosign'
72-
useXcpretty: 'false'
72+
useXcpretty: 'false'
73+
74+
- task: Xcode@5
75+
displayName: 'Running iOS unit tests'
76+
inputs:
77+
actions: 'test'
78+
scheme: 'ReactNativeAzureNotificationHubSample'
79+
sdk: 'iphonesimulator'
80+
configuration: 'Release'
81+
xcWorkspacePath: '../sample/ios/ReactNativeAzureNotificationHubSample.xcworkspace'
82+
xcodeVersion: '10'
83+
signingOption: 'nosign'
84+
useXcpretty: 'false'
85+
destination: 'platform=iOS Simulator, name=iPhone 8'
86+
destinationPlatformOption: 'iOS'
87+
destinationTypeOption: 'simulators'
88+
destinationSimulators: 'iPhone 8'

docs/android-installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
The documentation that follows assumes you have generated a React Native Android project using the `react-native-cli`, i.e.:
66

77
```
8-
react-native init myapp
8+
react-native init ReactNativeAzureNotificationHubSample
99
```
1010

1111
In addition to the standard React Native requirements, you will also need to install the following Android SDK components with your prefered SDK management tools:
@@ -193,7 +193,7 @@ const channelShowBadge = true;
193193
const channelEnableLights = true;
194194
const channelEnableVibration = true;
195195

196-
class myapp extends Component {
196+
export default class App extends Component {
197197
constructor(props) {
198198
super(props);
199199
PushNotificationEmitter.addListener(DEVICE_NOTIF_EVENT, this._onRemoteNotification);

ios/RCTAzureNotificationHubManager/RCTAzureNotificationHubManager.h

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

29+
// Register with Azure Notification Hub
30+
- (void)register:(nonnull NSString *)deviceToken
31+
config:(nonnull NSDictionary *)config
32+
resolver:(nonnull RCTPromiseResolveBlock)resolve
33+
rejecter:(nonnull RCTPromiseRejectBlock)reject;
34+
35+
// Unregister with Azure Notification Hub
36+
- (void)unregister:(nonnull RCTPromiseResolveBlock)resolve
37+
rejecter:(nonnull RCTPromiseRejectBlock)reject;
38+
2939
@end

sample/ios/Podfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ target 'ReactNativeAzureNotificationHubSample' do
3737

3838
target 'ReactNativeAzureNotificationHubSampleTests' do
3939
inherit! :search_paths
40-
# Pods for testing
40+
use_frameworks!
41+
pod 'OCMockito', '~> 5.0'
4142
end
4243

4344
use_native_modules!

sample/ios/ReactNativeAzureNotificationHubSampleTests/ReactNativeAzureNotificationHubSampleTests.m

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,80 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#import <UIKit/UIKit.h>
98
#import <XCTest/XCTest.h>
9+
#import <RNAzureNotificationHub/RCTAzureNotificationHubManager.h>
1010

11-
#import <React/RCTLog.h>
12-
#import <React/RCTRootView.h>
13-
14-
#define TIMEOUT_SECONDS 600
15-
#define TEXT_TO_LOOK_FOR @"Welcome to React"
11+
@import OCMockito;
1612

1713
@interface ReactNativeAzureNotificationHubSampleTests : XCTestCase
18-
1914
@end
2015

2116
@implementation ReactNativeAzureNotificationHubSampleTests
17+
{
18+
RCTAzureNotificationHubManager *hubManager;
19+
NSMutableDictionary *config;
20+
RCTPromiseResolveBlock resolver;
21+
RCTPromiseRejectBlock rejecter;
22+
}
2223

23-
- (BOOL)findSubviewInView:(UIView *)view matching:(BOOL(^)(UIView *view))test
24+
- (void)setUp
2425
{
25-
if (test(view)) {
26-
return YES;
27-
}
28-
for (UIView *subview in [view subviews]) {
29-
if ([self findSubviewInView:subview matching:test]) {
30-
return YES;
31-
}
32-
}
33-
return NO;
26+
[super setUp];
27+
hubManager = [[RCTAzureNotificationHubManager alloc] init];
28+
config = [[NSMutableDictionary alloc] init];
29+
resolver = ^(id result) {};
30+
rejecter = ^(NSString *code, NSString *message, NSError *error) {};
3431
}
3532

36-
- (void)testRendersWelcomeScreen
33+
- (void)testRegisterNoConnectionString
3734
{
38-
UIViewController *vc = [[[RCTSharedApplication() delegate] window] rootViewController];
39-
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:TIMEOUT_SECONDS];
40-
BOOL foundElement = NO;
35+
__block NSString *errorMsg;
36+
rejecter = ^(NSString *code, NSString *message, NSError *error)
37+
{
38+
errorMsg = message;
39+
};
4140

42-
__block NSString *redboxError = nil;
43-
#ifdef DEBUG
44-
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
45-
if (level >= RCTLogLevelError) {
46-
redboxError = message;
47-
}
48-
});
49-
#endif
41+
[hubManager register:@""
42+
config:config
43+
resolver:resolver
44+
rejecter:rejecter];
5045

51-
while ([date timeIntervalSinceNow] > 0 && !foundElement && !redboxError) {
52-
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
53-
[[NSRunLoop mainRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
46+
XCTAssertEqualObjects(errorMsg, @"Connection string cannot be null.");
47+
}
5448

55-
foundElement = [self findSubviewInView:vc.view matching:^BOOL(UIView *view) {
56-
if ([view.accessibilityLabel isEqualToString:TEXT_TO_LOOK_FOR]) {
57-
return YES;
58-
}
59-
return NO;
60-
}];
61-
}
62-
63-
#ifdef DEBUG
64-
RCTSetLogFunction(RCTDefaultLogFunction);
65-
#endif
49+
- (void)testRegisterNoHubName
50+
{
51+
__block NSString *errorMsg;
52+
rejecter = ^(NSString *code, NSString *message, NSError *error)
53+
{
54+
errorMsg = message;
55+
};
6656

67-
XCTAssertNil(redboxError, @"RedBox error: %@", redboxError);
68-
XCTAssertTrue(foundElement, @"Couldn't find element with text '%@' in %d seconds", TEXT_TO_LOOK_FOR, TIMEOUT_SECONDS);
57+
[config setObject:@"Connection String" forKey:@"connectionString"];
58+
[hubManager register:@""
59+
config:config
60+
resolver:resolver
61+
rejecter:rejecter];
62+
63+
XCTAssertEqualObjects(errorMsg, @"Hub name cannot be null.");
6964
}
7065

66+
- (void)testRegisterSuccessfully
67+
{
68+
__block bool hasError = false;
69+
rejecter = ^(NSString *code, NSString *message, NSError *error)
70+
{
71+
hasError = true;
72+
};
73+
74+
[config setObject:@"Connection String" forKey:@"connectionString"];
75+
[config setObject:@"Hub Name" forKey:@"hubName"];
76+
[hubManager register:@""
77+
config:config
78+
resolver:resolver
79+
rejecter:rejecter];
80+
81+
XCTAssertEqual(hasError, false);
82+
}
7183

7284
@end

0 commit comments

Comments
 (0)