RNCommandsHandlerIntegrationTest.m 4.42 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
#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];
}

28
- (void)testRequestPermissions_userAuthorizedPermissions {
yogevbd's avatar
yogevbd committed
29 30 31 32 33 34 35 36
    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];
    
37
    [_uut requestPermissions];
yogevbd's avatar
yogevbd committed
38 39 40
    [_notificationCenter verify];
}

41
- (void)testRequestPermissions_userDeniedPermissions {
yogevbd's avatar
yogevbd committed
42 43 44 45 46 47 48 49
    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];
    
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    [_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];
yogevbd's avatar
yogevbd committed
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
    [_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