react-native-navigation的迁移库

RNNModalManagerTest.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "RNNModalManager.h"
  4. #import "RNNComponentViewController.h"
  5. #import "RNNStackController.h"
  6. @interface MockViewController : UIViewController
  7. @property CGFloat presentViewControllerCalls;
  8. @end
  9. @implementation MockViewController
  10. - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
  11. _presentViewControllerCalls++;
  12. completion();
  13. }
  14. @end
  15. @interface MockModalManager : RNNModalManager
  16. @property (nonatomic, strong) MockViewController* topPresentedVC;
  17. @end
  18. @implementation MockModalManager
  19. @end
  20. @interface RNNModalManagerTest : XCTestCase <RNNModalManagerDelegate> {
  21. CGFloat _modalDismissedCount;
  22. }
  23. @end
  24. @implementation RNNModalManagerTest {
  25. RNNComponentViewController* _vc1;
  26. RNNComponentViewController* _vc2;
  27. RNNComponentViewController* _vc3;
  28. MockModalManager* _modalManager;
  29. }
  30. - (void)setUp {
  31. [super setUp];
  32. _vc1 = [RNNComponentViewController new];
  33. _vc2 = [RNNComponentViewController new];
  34. _vc3 = [RNNComponentViewController new];
  35. _vc1.layoutInfo = [RNNLayoutInfo new];
  36. _vc2.layoutInfo = [RNNLayoutInfo new];
  37. _vc3.layoutInfo = [RNNLayoutInfo new];
  38. _modalManager = [[MockModalManager alloc] init];
  39. _modalManager.topPresentedVC = [MockViewController new];
  40. }
  41. - (void)testDismissMultipleModalsInvokeDelegateWithCorrectParameters {
  42. [_modalManager showModal:_vc1 animated:NO completion:nil];
  43. [_modalManager showModal:_vc2 animated:NO completion:nil];
  44. [_modalManager showModal:_vc3 animated:NO completion:nil];
  45. _modalManager.delegate = self;
  46. [_modalManager dismissAllModalsAnimated:NO completion:nil];
  47. XCTAssertTrue(_modalDismissedCount == 3);
  48. }
  49. - (void)testDismissModal_InvokeDelegateWithCorrectParameters {
  50. [_modalManager showModal:_vc1 animated:NO completion:nil];
  51. [_modalManager showModal:_vc2 animated:NO completion:nil];
  52. [_modalManager showModal:_vc3 animated:NO completion:nil];
  53. _modalManager.delegate = self;
  54. [_modalManager dismissModal:_vc3 completion:nil];
  55. XCTAssertTrue(_modalDismissedCount == 1);
  56. }
  57. - (void)testDismissPreviousModal_InvokeDelegateWithCorrectParameters {
  58. [_modalManager showModal:_vc1 animated:NO completion:nil];
  59. [_modalManager showModal:_vc2 animated:NO completion:nil];
  60. [_modalManager showModal:_vc3 animated:NO completion:nil];
  61. _modalManager.delegate = self;
  62. [_modalManager dismissModal:_vc2 completion:nil];
  63. XCTAssertTrue(_modalDismissedCount == 1);
  64. }
  65. - (void)testDismissAllModals_AfterDismissingPreviousModal_InvokeDelegateWithCorrectParameters {
  66. [_modalManager showModal:_vc1 animated:NO completion:nil];
  67. [_modalManager showModal:_vc2 animated:NO completion:nil];
  68. [_modalManager showModal:_vc3 animated:NO completion:nil];
  69. _modalManager.delegate = self;
  70. [_modalManager dismissModal:_vc2 completion:nil];
  71. XCTAssertTrue(_modalDismissedCount == 1);
  72. [_modalManager dismissAllModalsAnimated:NO completion:nil];
  73. XCTAssertTrue(_modalDismissedCount == 2);
  74. }
  75. - (void)testDismissModal_DismissNilModalDoesntCrash {
  76. _modalManager.delegate = self;
  77. [_modalManager dismissModal:nil completion:nil];
  78. XCTAssertTrue(_modalDismissedCount == 0);
  79. }
  80. - (void)testShowModal_NilModalThrows {
  81. XCTAssertThrows([_modalManager showModal:nil animated:NO completion:nil]);
  82. }
  83. - (void)testShowModal_CallPresentViewController {
  84. [_modalManager showModal:_vc1 animated:NO completion:nil];
  85. XCTAssertTrue(_modalManager.topPresentedVC.presentViewControllerCalls == 1);
  86. }
  87. - (void)testDismissModal_ShouldInvokeDelegateDismissedModal {
  88. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  89. _modalManager.delegate = mockDelegate;
  90. [_modalManager showModal:_vc1 animated:NO completion:nil];
  91. [[mockDelegate expect] dismissedModal:_vc1];
  92. [_modalManager dismissModal:_vc1 completion:nil];
  93. [mockDelegate verify];
  94. }
  95. - (void)testPresentationControllerDidDismiss_ShouldInvokeDelegateDismissedModal {
  96. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  97. _modalManager.delegate = mockDelegate;
  98. UIPresentationController* presentationController = [[UIPresentationController alloc] initWithPresentedViewController:_vc2 presentingViewController:_vc1];
  99. [[mockDelegate expect] dismissedModal:_vc2];
  100. [_modalManager presentationControllerDidDismiss:presentationController];
  101. [mockDelegate verify];
  102. }
  103. - (void)testPresentationControllerDidDismiss_ShouldInvokeDelegateDismissedModalWithPresentedChild {
  104. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  105. _modalManager.delegate = mockDelegate;
  106. RNNStackController* nav = [[RNNStackController alloc] initWithRootViewController:_vc2];
  107. UIPresentationController* presentationController = [[UIPresentationController alloc] initWithPresentedViewController:nav presentingViewController:_vc1];
  108. [[mockDelegate expect] dismissedModal:_vc2];
  109. [_modalManager presentationControllerDidDismiss:presentationController];
  110. [mockDelegate verify];
  111. }
  112. - (void)testApplyOptionsOnInit_shouldShowModalWithDefaultPresentationStyle {
  113. _vc1.options = [RNNNavigationOptions emptyOptions];
  114. [_modalManager showModal:_vc1 animated:NO completion:nil];
  115. XCTAssertEqual(_vc1.modalPresentationStyle, UIModalPresentationPageSheet);
  116. }
  117. - (void)testApplyOptionsOnInit_shouldShowModalWithDefaultTransitionStyle {
  118. _vc1.options = [RNNNavigationOptions emptyOptions];
  119. [_modalManager showModal:_vc1 animated:NO completion:nil];
  120. XCTAssertEqual(_vc1.modalTransitionStyle, UIModalTransitionStyleCoverVertical);
  121. }
  122. - (void)testApplyOptionsOnInit_shouldShowModalWithPresentationStyle {
  123. _vc1.options = [RNNNavigationOptions emptyOptions];
  124. _vc1.options.modalPresentationStyle = [Text withValue:@"overCurrentContext"];
  125. [_modalManager showModal:_vc1 animated:NO completion:nil];
  126. XCTAssertEqual(_vc1.modalPresentationStyle, UIModalPresentationOverCurrentContext);
  127. }
  128. - (void)testApplyOptionsOnInit_shouldShowModalWithTransitionStyle {
  129. _vc1.options = [RNNNavigationOptions emptyOptions];
  130. _vc1.options.modalTransitionStyle = [Text withValue:@"crossDissolve"];
  131. [_modalManager showModal:_vc1 animated:NO completion:nil];
  132. XCTAssertEqual(_vc1.modalTransitionStyle, UIModalTransitionStyleCrossDissolve);
  133. }
  134. #pragma mark RNNModalManagerDelegate
  135. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  136. _modalDismissedCount = viewControllers.count;
  137. }
  138. - (void)dismissedModal:(UIViewController *)viewController {
  139. _modalDismissedCount = 1;
  140. }
  141. @end