Skip to content

Commit 23f6571

Browse files
committed
Add JSON stringifier for IAMs in unit tests
- Some unit tests were using an IAM's `jsonRepresentation` to use in server responses - `jsonRepresentation` uses key of "messageId" so we need to replace that with "id" when working with the backend - added `convertIAMtoJson` method that replaces "messageId" with "id" - refactored code to use this method instead of `jsonRepresentation`
1 parent cac1989 commit 23f6571

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ - (void)testIAMClickLaunchesAPIRequestMultipleTimes_Redisplay {
490490
let firstTrigger = [OSTrigger customTriggerWithProperty:@"prop1" withOperator:OSTriggerOperatorTypeExists withValue:nil];
491491

492492
let message = [OSInAppMessageTestHelper testMessageWithTriggers:@[@[firstTrigger]] withRedisplayLimit:limit delay:@(delay)];
493-
let registrationResponse = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message.jsonRepresentation]];
493+
NSDictionary* json = [OSInAppMessageTestHelper convertIAMtoJson:message];
494+
let registrationResponse = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[json]];
494495

495496
[OneSignalClientOverrider setMockResponseForRequest:NSStringFromClass([OSRequestRegisterUser class]) withResponse:registrationResponse];
496497

@@ -1525,7 +1526,8 @@ - (void)testInAppMessageDisplayMultipleTimesSessionDurationTrigger {
15251526
[OneSignal pauseInAppMessages:NO];
15261527
let trigger = [OSTrigger dynamicTriggerWithKind:OS_DYNAMIC_TRIGGER_KIND_SESSION_TIME withOperator:OSTriggerOperatorTypeGreaterThanOrEqualTo withValue:@0];
15271528
let message = [OSInAppMessageTestHelper testMessageWithTriggers:@[@[trigger]] withRedisplayLimit:5 delay:@30];
1528-
let registrationJson = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message.jsonRepresentation]];
1529+
NSDictionary* json = [OSInAppMessageTestHelper convertIAMtoJson:message];
1530+
let registrationJson = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[json]];
15291531

15301532
//Time interval mock
15311533
NSDateComponents* comps = [[NSDateComponents alloc]init];
@@ -1642,7 +1644,8 @@ - (void)testIAMLifecyleEventsFired {
16421644
Mock response JSON and initializes the OneSignal SDK
16431645
*/
16441646
- (void)initOneSignalWithInAppMessage:(OSInAppMessageInternal *)message {
1645-
let registrationJson = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message.jsonRepresentation]];
1647+
NSDictionary* json = [OSInAppMessageTestHelper convertIAMtoJson:message];
1648+
let registrationJson = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[json]];
16461649
[self initOneSignalWithRegistrationJSON:registrationJson];
16471650
}
16481651

iOS_SDK/OneSignalSDK/UnitTests/OSInAppMessagingHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ NS_ASSUME_NONNULL_BEGIN
6161
+ (OSInAppMessageInternal *)testMessageWithTriggers:(NSArray <NSArray<OSTrigger *> *> *)triggers;
6262
+ (OSInAppMessageInternal *)testMessageWithTriggers:(NSArray <NSArray<OSTrigger *> *> *)triggers withRedisplayLimit:(NSInteger)limit delay:(NSNumber *)delay;
6363
+ (OSInAppMessageInternal *)testMessageWithPastEndTime:(BOOL)pastEndTime;
64+
+ (NSDictionary *)convertIAMtoJson:(OSInAppMessageInternal *)message;
6465
+ (NSDictionary *)testRegistrationJsonWithMessages:(NSArray<NSDictionary *> *)messages;
6566
+ (NSDictionary *)testMessageJsonWithTriggerPropertyName:(NSString *)property withId:(NSString *)triggerId withOperator:(OSTriggerOperatorType)type withValue:(id)value;
6667
+ (NSDictionary*)testInAppMessageGetContainsWithHTML:(NSString *)html;

iOS_SDK/OneSignalSDK/UnitTests/OSInAppMessagingHelpers.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ + (OSInAppMessageInternal *)testMessageWithTriggers:(NSArray <NSArray<OSTrigger
245245
return message;
246246
}
247247

248+
// jsonRepresentation uses key of "messageId" but we need to use key of "id" for creating IAM, etc.
249+
+ (NSDictionary *)convertIAMtoJson:(OSInAppMessageInternal *)message {
250+
NSMutableDictionary *json = [message.jsonRepresentation mutableCopy];
251+
json[@"id"] = message.messageId;
252+
[json removeObjectForKey:@"messageId"];
253+
return json;
254+
}
255+
248256
+ (NSDictionary *)testRegistrationJsonWithMessages:(NSArray<NSDictionary *> *)messages {
249257
return @{
250258
@"id" : @"1234",

0 commit comments

Comments
 (0)