RNCommandsHandlerIntegrationTest.m 3.99 KB
Newer Older
yogevbd's avatar
yogevbd committed
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
#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)testRequestPermissionsWithCategories_userAuthorizedPermissions {
    NSArray* json = @[@{@"identifier": @"identifier"}];
    UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
    UNNotificationSettings* settings = [UNNotificationSettings new];
    [settings setValue:@(UNAuthorizationStatusAuthorized) forKey:@"authorizationStatus"];

    [[_notificationCenter expect] setNotificationCategories:[OCMArg any]];
    [[_notificationCenter expect] requestAuthorizationWithOptions:authOptions completionHandler:[OCMArg invokeBlockWithArgs:@(YES), [NSNull null], nil]];
    [[_notificationCenter expect] getNotificationSettingsWithCompletionHandler:[OCMArg invokeBlockWithArgs:settings, nil]];
    [[(id)[UIApplication sharedApplication] expect] registerForRemoteNotifications];
    
    [_uut requestPermissionsWithCategories:json];
    [_notificationCenter verify];
}

- (void)testRequestPermissionsWithCategories_userDeniedPermissions {
    NSArray* json = @[@{@"identifier": @"identifier"}];
    UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
    UNNotificationSettings* settings = [UNNotificationSettings new];
    [settings setValue:@(UNAuthorizationStatusDenied) forKey:@"authorizationStatus"];
    
    [[_notificationCenter expect] setNotificationCategories:[OCMArg any]];
    [[_notificationCenter expect] requestAuthorizationWithOptions:authOptions completionHandler:[OCMArg invokeBlockWithArgs:@(YES), [NSNull null], nil]];
    [[_notificationCenter expect] getNotificationSettingsWithCompletionHandler:[OCMArg invokeBlockWithArgs:settings, nil]];
    [[(id)[UIApplication sharedApplication] reject] registerForRemoteNotifications];
    
    [_uut requestPermissionsWithCategories: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