From b40ba17dc222dc2f8ef0036099d56194410e8205 Mon Sep 17 00:00:00 2001 From: Yogev Ben David Date: Mon, 24 Aug 2020 16:15:20 +0300 Subject: [PATCH] Fix color parser (#6504) Return `NoColor` object when `NoColor` string is passed as the color from JS. --- lib/ios/ColorParser.m | 8 ++--- .../ios/NavigationTests/ColorParserTest.m | 34 +++++++++++++++++++ .../ios/playground.xcodeproj/project.pbxproj | 4 +++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 playground/ios/NavigationTests/ColorParserTest.m diff --git a/lib/ios/ColorParser.m b/lib/ios/ColorParser.m index a99efcd36e2..51cf351ad43 100644 --- a/lib/ios/ColorParser.m +++ b/lib/ios/ColorParser.m @@ -6,10 +6,10 @@ @implementation ColorParser + (Color *)parse:(NSDictionary *)json key:(NSString *)key { - if (json[key]) { - return [[Color alloc] initWithValue:[RCTConvert UIColor:json[key]]] ?: [NoColor new]; - } - return [NullColor new]; + if ([json[key] isEqual:@"NoColor"]) return [NoColor new]; + else if (json[key]) return [Color withValue:[RCTConvert UIColor:json[key]]]; + + return [NullColor new]; } @end diff --git a/playground/ios/NavigationTests/ColorParserTest.m b/playground/ios/NavigationTests/ColorParserTest.m new file mode 100644 index 00000000000..b7ba46d1452 --- /dev/null +++ b/playground/ios/NavigationTests/ColorParserTest.m @@ -0,0 +1,34 @@ +#import +#import "ColorParser.h" +#import "NullColor.h" +#import "NoColor.h" + +@interface ColorParserTest : XCTestCase +@end + +@implementation ColorParserTest + +- (void)setUp { + [super setUp]; +} + +- (void)testParse_NSNumberColor { + UIColor* expectedColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]; + NSDictionary* colorDict = @{@"colorKey": @(0xffffffff)}; + Color* color = [ColorParser parse:colorDict key:@"colorKey"]; + XCTAssertTrue([color.get isEqual:expectedColor]); +} + +- (void)testParse_nilColor { + NSDictionary* colorDict = @{}; + Color* color = [ColorParser parse:colorDict key:@"colorKey"]; + XCTAssertTrue([color isKindOfClass:NullColor.class]); +} + +- (void)testParse_NoColor { + NSDictionary* colorDict = @{@"colorKey": @"NoColor"}; + Color* color = [ColorParser parse:colorDict key:@"colorKey"]; + XCTAssertTrue([color isKindOfClass:NoColor.class]); +} + +@end diff --git a/playground/ios/playground.xcodeproj/project.pbxproj b/playground/ios/playground.xcodeproj/project.pbxproj index 8c1e7a37386..261c4196c6d 100644 --- a/playground/ios/playground.xcodeproj/project.pbxproj +++ b/playground/ios/playground.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ 501C86B9239FE9C400E0B631 /* UIImage+Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 501C86B8239FE9C400E0B631 /* UIImage+Utils.m */; }; 5022EDCD2405522000852BA6 /* RNNBottomTabsPresenterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = E58D263A2385888C003F36BA /* RNNBottomTabsPresenterTest.m */; }; 5022EDCE2405524700852BA6 /* RNNBottomTabsAppearancePresenterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5022EDCB240551EE00852BA6 /* RNNBottomTabsAppearancePresenterTest.m */; }; + 502734AF24F3E9BA0022163C /* ColorParserTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 502734AE24F3E9110022163C /* ColorParserTest.m */; }; 50647FE323E3196800B92025 /* RNNExternalViewControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 50647FE223E3196800B92025 /* RNNExternalViewControllerTests.m */; }; 50650A23242FB0F800688104 /* CommandsHandlerCreator.m in Sources */ = {isa = PBXBuildFile; fileRef = 50650A22242FB0F800688104 /* CommandsHandlerCreator.m */; }; 5078DF39242BE8AA007B0B4F /* TestingAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5078DF38242BE8AA007B0B4F /* TestingAppDelegate.m */; }; @@ -113,6 +114,7 @@ 501C86B7239FE9C400E0B631 /* UIImage+Utils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+Utils.h"; sourceTree = ""; }; 501C86B8239FE9C400E0B631 /* UIImage+Utils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+Utils.m"; sourceTree = ""; }; 5022EDCB240551EE00852BA6 /* RNNBottomTabsAppearancePresenterTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNBottomTabsAppearancePresenterTest.m; sourceTree = ""; }; + 502734AE24F3E9110022163C /* ColorParserTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ColorParserTest.m; sourceTree = ""; }; 50364D69238E7ECC000E62A2 /* Pods_playground.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Pods_playground.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 50364D6B238E7F0A000E62A2 /* ReactNativeNavigation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = ReactNativeNavigation.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 50647FE223E3196800B92025 /* RNNExternalViewControllerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNExternalViewControllerTests.m; sourceTree = ""; }; @@ -348,6 +350,7 @@ E58D263F2385888C003F36BA /* RNNStackControllerTest.m */, E58D26322385888B003F36BA /* RNNNavigationOptionsTest.m */, E58D263C2385888C003F36BA /* RNNNavigationStackManagerTest.m */, + 502734AE24F3E9110022163C /* ColorParserTest.m */, E58D262C2385888B003F36BA /* RNNOptionsTest.h */, E58D262D2385888B003F36BA /* RNNOverlayManagerTest.m */, E58D26262385888B003F36BA /* RNNRootViewControllerTest.m */, @@ -970,6 +973,7 @@ 5007B4382472D9A20002AA4E /* RNNCustomViewController.m in Sources */, E58D265B2385888C003F36BA /* UIViewController+RNNOptionsTest.m in Sources */, E58D26532385888C003F36BA /* RNNSideMenuPresenterTest.m in Sources */, + 502734AF24F3E9BA0022163C /* ColorParserTest.m in Sources */, 50BCB27623F1A2B100D6C8E5 /* TopBarAppearancePresenterTest.m in Sources */, 50C9A8D4240FB9D000BD699F /* RNNComponentViewController+Utils.m in Sources */, 50647FE323E3196800B92025 /* RNNExternalViewControllerTests.m in Sources */,