react-native-navigation的迁移库

RNNModalManagerTest.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. _modalManager = [[MockModalManager alloc] init];
  36. _modalManager.topPresentedVC = [MockViewController new];
  37. }
  38. - (void)testDismissMultipleModalsInvokeDelegateWithCorrectParameters {
  39. [_modalManager showModal:_vc1 animated:NO completion:nil];
  40. [_modalManager showModal:_vc2 animated:NO completion:nil];
  41. [_modalManager showModal:_vc3 animated:NO completion:nil];
  42. _modalManager.delegate = self;
  43. [_modalManager dismissAllModalsAnimated:NO completion:nil];
  44. XCTAssertTrue(_modalDismissedCount == 3);
  45. }
  46. - (void)testDismissModal_InvokeDelegateWithCorrectParameters {
  47. [_modalManager showModal:_vc1 animated:NO completion:nil];
  48. [_modalManager showModal:_vc2 animated:NO completion:nil];
  49. [_modalManager showModal:_vc3 animated:NO completion:nil];
  50. _modalManager.delegate = self;
  51. [_modalManager dismissModal:_vc3 completion:nil];
  52. XCTAssertTrue(_modalDismissedCount == 1);
  53. }
  54. - (void)testDismissPreviousModal_InvokeDelegateWithCorrectParameters {
  55. [_modalManager showModal:_vc1 animated:NO completion:nil];
  56. [_modalManager showModal:_vc2 animated:NO completion:nil];
  57. [_modalManager showModal:_vc3 animated:NO completion:nil];
  58. _modalManager.delegate = self;
  59. [_modalManager dismissModal:_vc2 completion:nil];
  60. XCTAssertTrue(_modalDismissedCount == 1);
  61. }
  62. - (void)testDismissAllModals_AfterDismissingPreviousModal_InvokeDelegateWithCorrectParameters {
  63. [_modalManager showModal:_vc1 animated:NO completion:nil];
  64. [_modalManager showModal:_vc2 animated:NO completion:nil];
  65. [_modalManager showModal:_vc3 animated:NO completion:nil];
  66. _modalManager.delegate = self;
  67. [_modalManager dismissModal:_vc2 completion:nil];
  68. XCTAssertTrue(_modalDismissedCount == 1);
  69. [_modalManager dismissAllModalsAnimated:NO completion:nil];
  70. XCTAssertTrue(_modalDismissedCount == 2);
  71. }
  72. - (void)testDismissModal_DismissNilModalDoesntCrash {
  73. _modalManager.delegate = self;
  74. [_modalManager dismissModal:nil completion:nil];
  75. XCTAssertTrue(_modalDismissedCount == 0);
  76. }
  77. - (void)testShowModal_NilModalThrows {
  78. XCTAssertThrows([_modalManager showModal:nil animated:NO completion:nil]);
  79. }
  80. - (void)testShowModal_CallPresentViewController {
  81. [_modalManager showModal:_vc1 animated:NO completion:nil];
  82. XCTAssertTrue(_modalManager.topPresentedVC.presentViewControllerCalls == 1);
  83. }
  84. - (void)testDismissModal_ShouldInvokeDelegateDismissedModal {
  85. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  86. _modalManager.delegate = mockDelegate;
  87. [_modalManager showModal:_vc1 animated:NO completion:nil];
  88. [[mockDelegate expect] dismissedModal:_vc1];
  89. [_modalManager dismissModal:_vc1 completion:nil];
  90. [mockDelegate verify];
  91. }
  92. - (void)testPresentationControllerDidDismiss_ShouldInvokeDelegateDismissedModal {
  93. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  94. _modalManager.delegate = mockDelegate;
  95. UIPresentationController* presentationController = [[UIPresentationController alloc] initWithPresentedViewController:_vc2 presentingViewController:_vc1];
  96. [[mockDelegate expect] dismissedModal:_vc2];
  97. [_modalManager presentationControllerDidDismiss:presentationController];
  98. [mockDelegate verify];
  99. }
  100. - (void)testPresentationControllerDidDismiss_ShouldInvokeDelegateDismissedModalWithPresentedChild {
  101. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  102. _modalManager.delegate = mockDelegate;
  103. RNNStackController* nav = [[RNNStackController alloc] initWithRootViewController:_vc2];
  104. UIPresentationController* presentationController = [[UIPresentationController alloc] initWithPresentedViewController:nav presentingViewController:_vc1];
  105. [[mockDelegate expect] dismissedModal:_vc2];
  106. [_modalManager presentationControllerDidDismiss:presentationController];
  107. [mockDelegate verify];
  108. }
  109. #pragma mark RNNModalManagerDelegate
  110. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  111. _modalDismissedCount = viewControllers.count;
  112. }
  113. - (void)dismissedModal:(UIViewController *)viewController {
  114. _modalDismissedCount = 1;
  115. }
  116. @end