Skip to content

Commit 6022f43

Browse files
authored
Fix color parser (#6504)
Return `NoColor` object when `NoColor` string is passed as the color from JS.
1 parent 8f67d9f commit 6022f43

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

lib/ios/ColorParser.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
@implementation ColorParser
77

88
+ (Color *)parse:(NSDictionary *)json key:(NSString *)key {
9-
if (json[key]) {
10-
return [[Color alloc] initWithValue:[RCTConvert UIColor:json[key]]] ?: [NoColor new];
11-
}
12-
return [NullColor new];
9+
if ([json[key] isEqual:@"NoColor"]) return [NoColor new];
10+
else if (json[key]) return [Color withValue:[RCTConvert UIColor:json[key]]];
11+
12+
return [NullColor new];
1313
}
1414

1515
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#import <XCTest/XCTest.h>
2+
#import "ColorParser.h"
3+
#import "NullColor.h"
4+
#import "NoColor.h"
5+
6+
@interface ColorParserTest : XCTestCase
7+
@end
8+
9+
@implementation ColorParserTest
10+
11+
- (void)setUp {
12+
[super setUp];
13+
}
14+
15+
- (void)testParse_NSNumberColor {
16+
UIColor* expectedColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
17+
NSDictionary* colorDict = @{@"colorKey": @(0xffffffff)};
18+
Color* color = [ColorParser parse:colorDict key:@"colorKey"];
19+
XCTAssertTrue([color.get isEqual:expectedColor]);
20+
}
21+
22+
- (void)testParse_nilColor {
23+
NSDictionary* colorDict = @{};
24+
Color* color = [ColorParser parse:colorDict key:@"colorKey"];
25+
XCTAssertTrue([color isKindOfClass:NullColor.class]);
26+
}
27+
28+
- (void)testParse_NoColor {
29+
NSDictionary* colorDict = @{@"colorKey": @"NoColor"};
30+
Color* color = [ColorParser parse:colorDict key:@"colorKey"];
31+
XCTAssertTrue([color isKindOfClass:NoColor.class]);
32+
}
33+
34+
@end

playground/ios/playground.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
501C86B9239FE9C400E0B631 /* UIImage+Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 501C86B8239FE9C400E0B631 /* UIImage+Utils.m */; };
2222
5022EDCD2405522000852BA6 /* RNNBottomTabsPresenterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = E58D263A2385888C003F36BA /* RNNBottomTabsPresenterTest.m */; };
2323
5022EDCE2405524700852BA6 /* RNNBottomTabsAppearancePresenterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5022EDCB240551EE00852BA6 /* RNNBottomTabsAppearancePresenterTest.m */; };
24+
502734AF24F3E9BA0022163C /* ColorParserTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 502734AE24F3E9110022163C /* ColorParserTest.m */; };
2425
50647FE323E3196800B92025 /* RNNExternalViewControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 50647FE223E3196800B92025 /* RNNExternalViewControllerTests.m */; };
2526
50650A23242FB0F800688104 /* CommandsHandlerCreator.m in Sources */ = {isa = PBXBuildFile; fileRef = 50650A22242FB0F800688104 /* CommandsHandlerCreator.m */; };
2627
5078DF39242BE8AA007B0B4F /* TestingAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5078DF38242BE8AA007B0B4F /* TestingAppDelegate.m */; };
@@ -113,6 +114,7 @@
113114
501C86B7239FE9C400E0B631 /* UIImage+Utils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+Utils.h"; sourceTree = "<group>"; };
114115
501C86B8239FE9C400E0B631 /* UIImage+Utils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIImage+Utils.m"; sourceTree = "<group>"; };
115116
5022EDCB240551EE00852BA6 /* RNNBottomTabsAppearancePresenterTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNBottomTabsAppearancePresenterTest.m; sourceTree = "<group>"; };
117+
502734AE24F3E9110022163C /* ColorParserTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ColorParserTest.m; sourceTree = "<group>"; };
116118
50364D69238E7ECC000E62A2 /* Pods_playground.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Pods_playground.framework; sourceTree = BUILT_PRODUCTS_DIR; };
117119
50364D6B238E7F0A000E62A2 /* ReactNativeNavigation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = ReactNativeNavigation.framework; sourceTree = BUILT_PRODUCTS_DIR; };
118120
50647FE223E3196800B92025 /* RNNExternalViewControllerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNExternalViewControllerTests.m; sourceTree = "<group>"; };
@@ -348,6 +350,7 @@
348350
E58D263F2385888C003F36BA /* RNNStackControllerTest.m */,
349351
E58D26322385888B003F36BA /* RNNNavigationOptionsTest.m */,
350352
E58D263C2385888C003F36BA /* RNNNavigationStackManagerTest.m */,
353+
502734AE24F3E9110022163C /* ColorParserTest.m */,
351354
E58D262C2385888B003F36BA /* RNNOptionsTest.h */,
352355
E58D262D2385888B003F36BA /* RNNOverlayManagerTest.m */,
353356
E58D26262385888B003F36BA /* RNNRootViewControllerTest.m */,
@@ -970,6 +973,7 @@
970973
5007B4382472D9A20002AA4E /* RNNCustomViewController.m in Sources */,
971974
E58D265B2385888C003F36BA /* UIViewController+RNNOptionsTest.m in Sources */,
972975
E58D26532385888C003F36BA /* RNNSideMenuPresenterTest.m in Sources */,
976+
502734AF24F3E9BA0022163C /* ColorParserTest.m in Sources */,
973977
50BCB27623F1A2B100D6C8E5 /* TopBarAppearancePresenterTest.m in Sources */,
974978
50C9A8D4240FB9D000BD699F /* RNNComponentViewController+Utils.m in Sources */,
975979
50647FE323E3196800B92025 /* RNNExternalViewControllerTests.m in Sources */,

0 commit comments

Comments
 (0)