RNNotificationEventHandlerTests.m 6.22 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
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import "RNNotificationEventHandler.h"
#import "RNNotificationUtils.h"

@interface RNNotificationEventHandlerTests : XCTestCase
@property (nonatomic, retain) RNNotificationEventHandler* uut;
@property (nonatomic, retain) RNNotificationsStore* store;
@property (nonatomic, retain) id mockedNotificationCenter;
@end

@implementation RNNotificationEventHandlerTests

- (void)setUp {
    _store = [RNNotificationsStore sharedInstance];
    _uut = [[RNNotificationEventHandler alloc] initWithStore:_store];

    _mockedNotificationCenter = [OCMockObject partialMockForObject:[NSNotificationCenter new]];
    [[[[[OCMockObject niceMockForClass:NSNotificationCenter.class] stub] classMethod] andReturn:_mockedNotificationCenter] defaultCenter];
}

- (void)testDidRegisterForRemoteNotifications_ShouldEmitEventWithDeviceTokenDataString {
    NSData* deviceToken = [@"740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad" dataUsingEncoding:NSUTF32StringEncoding];
    [[_mockedNotificationCenter expect] postNotificationName:RNRegistered object:[OCMArg any] userInfo:[OCMArg checkWithBlock:^BOOL(id obj) {
        return ([[obj objectForKey:@"deviceToken"] isEqualToString:[RNNotificationUtils deviceTokenToString:deviceToken]]);
    }]];
    [_uut didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    [_mockedNotificationCenter verify];
}

- (void)testDidRegisterForRemoteNotifications_ShouldEmitEventWithDeviceTokenString {
    NSString* deviceToken = @"740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad";
    [[_mockedNotificationCenter expect] postNotificationName:RNRegistered object:[OCMArg any] userInfo:[OCMArg checkWithBlock:^BOOL(id obj) {
        return ([[obj objectForKey:@"deviceToken"] isEqualToString:deviceToken]);
    }]];
    [_uut didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    [_mockedNotificationCenter verify];
}

- (void)testDidFailToRegisterForRemoteNotifications_ShouldEmitEvent {
    NSError* error = [NSError errorWithDomain:@"domain" code:1 userInfo:nil];
    [[_mockedNotificationCenter expect] postNotificationName:RNRegistrationFailed object:[OCMArg any] userInfo:[OCMArg checkWithBlock:^BOOL(id obj) {
        return ([[obj valueForKey:@"code"] isEqualToNumber:@(1)] &&
                [[obj valueForKey:@"domain"] isEqualToString:@"domain"]);
    }]];
    
    [_uut didFailToRegisterForRemoteNotificationsWithError:error];
    [_mockedNotificationCenter verify];
}

51 52 53 54 55 56 57 58 59 60 61 62
- (void)testDidReceiveForegroundNotification_ShouldSaveCompletionBlockToStore {
    UNNotification* notification = [self createNotificationWithIdentifier:@"id" andUserInfo:@{}];
    void (^testBlock)(UNNotificationPresentationOptions) = ^void(UNNotificationPresentationOptions options) {};
    
    [_uut didReceiveForegroundNotification:notification withCompletionHandler:testBlock];
    XCTAssertEqual([_store getPresentationCompletionHandler:@"id"], testBlock);
}

- (void)testDidReceiveForegroundNotification_ShouldEmitEvent {
    UNNotification* notification = [self createNotificationWithIdentifier:@"id" andUserInfo:@{@"extraKey": @"extraValue"}];
    void (^testBlock)(UNNotificationPresentationOptions) = ^void(UNNotificationPresentationOptions options) {};
    
yogevbd's avatar
yogevbd committed
63
    [[_mockedNotificationCenter expect] postNotificationName:RNNotificationReceived object:[OCMArg any] userInfo:[OCMArg checkWithBlock:^BOOL(id obj) {
64
        return ([[obj valueForKey:@"identifier"] isEqualToString:@"id"] &&
yogevbd's avatar
yogevbd committed
65
                [[obj valueForKey:@"extraKey"] isEqualToString:@"extraValue"]);
66 67 68 69 70
    }]];
    [_uut didReceiveForegroundNotification:notification withCompletionHandler:testBlock];
    [_mockedNotificationCenter verify];
}

yogevbd's avatar
yogevbd committed
71 72
- (void)testDidReceiveNotificationResponse_ShouldEmitEvent {
    UNNotification* notification = [self createNotificationWithIdentifier:@"id" andUserInfo:@{@"extraKey": @"extraValue"}];
yogevbd's avatar
yogevbd committed
73
    UNNotificationResponse* response = [self createNotificationResponseWithIdentifier:@"actionId" andNotification:notification];
yogevbd's avatar
yogevbd committed
74 75
    void (^testBlock)(void) = ^void() {};
    
yogevbd's avatar
yogevbd committed
76 77 78 79 80
    [[_mockedNotificationCenter expect] postNotificationName:RNNotificationOpened object:[OCMArg any] userInfo:[OCMArg checkWithBlock:^BOOL(id response) {
        NSDictionary* notification = response[@"notification"];
        NSDictionary* action = response[@"action"];
        return ([[notification valueForKey:@"identifier"] isEqualToString:@"id"] &&
                [[notification valueForKey:@"extraKey"] isEqualToString:@"extraValue"] && [action[@"identifier"] isEqualToString:@"actionId"]);
yogevbd's avatar
yogevbd committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    }]];
    [_uut didReceiveNotificationResponse:response completionHandler:testBlock];
    [_mockedNotificationCenter verify];
}

- (void)testDidReceiveNotificationResponse_ShouldSaveCompletionBlockToStore {
    UNNotification* notification = [self createNotificationWithIdentifier:@"id" andUserInfo:@{@"extraKey": @"extraValue"}];
    UNNotificationResponse* response = [self createNotificationResponseWithIdentifier:@"id" andNotification:notification];
    void (^testBlock)(void) = ^void() {};
    
    [_uut didReceiveNotificationResponse:response completionHandler:testBlock];
    XCTAssertEqual([_store getActionCompletionHandler:@"id"], testBlock);
}

95 96 97 98 99 100 101 102 103 104 105
- (UNNotification *)createNotificationWithIdentifier:(NSString *)identifier andUserInfo:(NSDictionary *)userInfo {
    UNNotification* notification = [OCMockObject niceMockForClass:[UNNotification class]];
    UNNotificationContent* content = [OCMockObject niceMockForClass:[UNNotificationContent class]];
    OCMStub([content userInfo]).andReturn(userInfo);
    UNNotificationRequest* request = [OCMockObject partialMockForObject:[UNNotificationRequest requestWithIdentifier:identifier content:content trigger:nil]];
    OCMStub(notification.request).andReturn(request);
    OCMStub(request.content).andReturn(content);
    
    return notification;
}

yogevbd's avatar
yogevbd committed
106 107 108 109 110 111 112 113
- (UNNotificationResponse *)createNotificationResponseWithIdentifier:(NSString *)identifier andNotification:(UNNotification *)notification {
    UNNotificationResponse* notificationResponse = [OCMockObject niceMockForClass:[UNNotificationResponse class]];
    OCMStub(notificationResponse.actionIdentifier).andReturn(identifier);
    OCMStub(notificationResponse.notification).andReturn(notification);
    
    return notificationResponse;
}

114

yogevbd's avatar
yogevbd committed
115 116

@end