react-native-navigation的迁移库

RNNStoreTest.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "RNNStore.h"
  4. @interface RNNStoreTest : XCTestCase
  5. @property (nonatomic, strong) RNNStore *store;
  6. @end
  7. @implementation RNNStoreTest
  8. - (void)setUp {
  9. [super setUp];
  10. self.store = [RNNStore new];
  11. }
  12. - (void)testFindComponentForId_setAndGetsimpleComponentId {
  13. NSString *componentId1 = @"cntId1";
  14. NSString *componentId2 = @"cntId2";
  15. UIViewController *vc1 = [UIViewController new];
  16. UIViewController *vc2 = [UIViewController new];
  17. [self.store setComponent:vc1 componentId:componentId1];
  18. [self.store setComponent:vc2 componentId:componentId2];
  19. UIViewController *ans = [self.store findComponentForId:componentId1];
  20. XCTAssertEqualObjects(vc1, ans);
  21. XCTAssertNotEqualObjects(vc2, ans);
  22. }
  23. - (void)testSetComponent_setNilComponentId {
  24. NSString *componentId1 = nil;
  25. UIViewController *vc1 = [UIViewController new];
  26. [self.store setComponent:vc1 componentId:componentId1];
  27. XCTAssertNil([self.store findComponentForId:componentId1]);
  28. }
  29. - (void)testSetComponent_setDoubleComponentId {
  30. NSString *componentId1 = @"cntId1";
  31. UIViewController *vc1 = [UIViewController new];
  32. UIViewController *vc2 = [UIViewController new];
  33. [self.store setComponent:vc1 componentId:componentId1];
  34. UIViewController *ans = [self.store findComponentForId:componentId1];
  35. XCTAssertEqualObjects(vc1, ans);
  36. XCTAssertThrows([self.store setComponent:vc2 componentId:componentId1]);
  37. }
  38. - (void)testRemoveComponent_removeExistComponent {
  39. NSString *componentId1 = @"cntId1";
  40. UIViewController *vc1 = [UIViewController new];
  41. [self.store setComponent:vc1 componentId:componentId1];
  42. UIViewController *ans = [self.store findComponentForId:componentId1];
  43. XCTAssertEqualObjects(vc1, ans);
  44. [self.store removeComponent:componentId1];
  45. XCTAssertNil([self.store findComponentForId:componentId1]);
  46. }
  47. - (void)testPopWillRemoveVcFromStore {
  48. NSString *vcId = @"cnt_vc_2";
  49. [self setComponentAndRelease:vcId];
  50. XCTAssertNil([self.store findComponentForId:vcId]);
  51. }
  52. - (void)testRemoveComponentByInstance {
  53. NSString *componentId1 = @"cntId1";
  54. UIViewController *vc1 = [UIViewController new];
  55. [self.store setComponent:vc1 componentId:componentId1];
  56. [self.store removeComponentByViewControllerInstance:vc1];
  57. XCTAssertNil([self.store findComponentForId:@"cntId1"]);
  58. }
  59. - (void)testRemoveAllComponentsFromWindowShouldRemoveComponentsInWindow {
  60. UIViewController* overlayVC = [self createMockedViewControllerWithWindow:[UIWindow new]];
  61. NSString* overlayId = @"overlayId";
  62. [self.store setComponent:overlayVC componentId:overlayId];
  63. [self.store removeAllComponentsFromWindow:overlayVC.view.window];
  64. XCTAssertNil([self.store findComponentForId:overlayId]);
  65. }
  66. - (void)testRemoveAllComponentsFromWindowShouldNotRemoveComponentsInOtherWindows {
  67. UIViewController* overlayVC = [self createMockedViewControllerWithWindow:[UIWindow new]];
  68. UIViewController* componentVC = [self createMockedViewControllerWithWindow:[UIWindow new]];
  69. NSString* componentId = @"componentId";
  70. NSString* overlayId = @"overlayId";
  71. [self.store setComponent:overlayVC componentId:overlayId];
  72. [self.store setComponent:componentVC componentId:componentId];
  73. [self.store removeAllComponentsFromWindow:componentVC.view.window];
  74. XCTAssertNil([self.store findComponentForId:componentId]);
  75. XCTAssertNotNil([self.store findComponentForId:overlayId]);
  76. }
  77. - (void)testGetExternalComponentShouldRetrunSavedComponent {
  78. UIViewController* testVC = [UIViewController new];
  79. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  80. layoutInfo.name = @"extId1";
  81. [self.store registerExternalComponent:layoutInfo.name callback:^UIViewController *(NSDictionary *props, RCTBridge *bridge) {
  82. return testVC;
  83. }];
  84. UIViewController* savedComponent = [self.store getExternalComponent:layoutInfo bridge:nil];
  85. XCTAssertEqual(testVC, savedComponent);
  86. }
  87. - (void)testComponentKeyForInstance_UknownComponentShouldReturnNil {
  88. id result = [self.store componentKeyForInstance:[UIViewController new]];
  89. XCTAssertNil(result);
  90. }
  91. - (void)testCleanStore {
  92. [self.store clean];
  93. XCTAssertFalse(self.store.isReadyToReceiveCommands);
  94. }
  95. #pragma mark - private
  96. -(void)setComponentAndRelease:(NSString*)vcId {
  97. @autoreleasepool {
  98. UIViewController *vc2 = [UIViewController new];
  99. [self.store setComponent:vc2 componentId:vcId];
  100. XCTAssertNotNil([self.store findComponentForId:vcId]);
  101. }
  102. }
  103. - (UIViewController *)createMockedViewControllerWithWindow:(UIWindow *)window {
  104. UIWindow* overlayWindow = [UIWindow new];
  105. id mockedViewController = [OCMockObject partialMockForObject:[UIViewController new]];
  106. id mockedView = [OCMockObject partialMockForObject:[UIView new]];
  107. OCMStub([mockedView window]).andReturn(overlayWindow);
  108. OCMStub([mockedViewController view]).andReturn(mockedView);
  109. return mockedViewController;
  110. }
  111. @end