react-native-navigation的迁移库

RNNModalManagerTest.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "RNNModalManager.h"
  4. #import "RNNComponentViewController.h"
  5. @interface MockViewController : UIViewController
  6. @property CGFloat presentViewControllerCalls;
  7. @end
  8. @implementation MockViewController
  9. - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
  10. _presentViewControllerCalls++;
  11. completion();
  12. }
  13. @end
  14. @interface MockModalManager : RNNModalManager
  15. @property (nonatomic, strong) MockViewController* topPresentedVC;
  16. @end
  17. @implementation MockModalManager
  18. @end
  19. @interface RNNModalManagerTest : XCTestCase <RNNModalManagerDelegate> {
  20. CGFloat _modalDismissedCount;
  21. }
  22. @end
  23. @implementation RNNModalManagerTest {
  24. RNNComponentViewController* _vc1;
  25. RNNComponentViewController* _vc2;
  26. RNNComponentViewController* _vc3;
  27. MockModalManager* _modalManager;
  28. }
  29. - (void)setUp {
  30. [super setUp];
  31. _vc1 = [RNNComponentViewController new];
  32. _vc2 = [RNNComponentViewController new];
  33. _vc3 = [RNNComponentViewController new];
  34. _modalManager = [[MockModalManager alloc] init];
  35. _modalManager.topPresentedVC = [MockViewController new];
  36. }
  37. - (void)testDismissMultipleModalsInvokeDelegateWithCorrectParameters {
  38. [_modalManager showModal:_vc1 animated:NO completion:nil];
  39. [_modalManager showModal:_vc2 animated:NO completion:nil];
  40. [_modalManager showModal:_vc3 animated:NO completion:nil];
  41. _modalManager.delegate = self;
  42. [_modalManager dismissAllModalsAnimated:NO completion:nil];
  43. XCTAssertTrue(_modalDismissedCount == 3);
  44. }
  45. - (void)testDismissModal_InvokeDelegateWithCorrectParameters {
  46. [_modalManager showModal:_vc1 animated:NO completion:nil];
  47. [_modalManager showModal:_vc2 animated:NO completion:nil];
  48. [_modalManager showModal:_vc3 animated:NO completion:nil];
  49. _modalManager.delegate = self;
  50. [_modalManager dismissModal:_vc3 completion:nil];
  51. XCTAssertTrue(_modalDismissedCount == 1);
  52. }
  53. - (void)testDismissPreviousModal_InvokeDelegateWithCorrectParameters {
  54. [_modalManager showModal:_vc1 animated:NO completion:nil];
  55. [_modalManager showModal:_vc2 animated:NO completion:nil];
  56. [_modalManager showModal:_vc3 animated:NO completion:nil];
  57. _modalManager.delegate = self;
  58. [_modalManager dismissModal:_vc2 completion:nil];
  59. XCTAssertTrue(_modalDismissedCount == 1);
  60. }
  61. - (void)testDismissAllModals_AfterDismissingPreviousModal_InvokeDelegateWithCorrectParameters {
  62. [_modalManager showModal:_vc1 animated:NO completion:nil];
  63. [_modalManager showModal:_vc2 animated:NO completion:nil];
  64. [_modalManager showModal:_vc3 animated:NO completion:nil];
  65. _modalManager.delegate = self;
  66. [_modalManager dismissModal:_vc2 completion:nil];
  67. XCTAssertTrue(_modalDismissedCount == 1);
  68. [_modalManager dismissAllModalsAnimated:NO completion:nil];
  69. XCTAssertTrue(_modalDismissedCount == 2);
  70. }
  71. - (void)testDismissModal_DismissNilModalDoesntCrash {
  72. _modalManager.delegate = self;
  73. [_modalManager dismissModal:nil completion:nil];
  74. XCTAssertTrue(_modalDismissedCount == 0);
  75. }
  76. - (void)testShowModal_NilModalThrows {
  77. XCTAssertThrows([_modalManager showModal:nil animated:NO completion:nil]);
  78. }
  79. - (void)testShowModal_CallPresentViewController {
  80. [_modalManager showModal:_vc1 animated:NO completion:nil];
  81. XCTAssertTrue(_modalManager.topPresentedVC.presentViewControllerCalls == 1);
  82. }
  83. - (void)testDismissModal_ShouldInvokeDelegateDismissedModal {
  84. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  85. _modalManager.delegate = mockDelegate;
  86. [_modalManager showModal:_vc1 animated:NO completion:nil];
  87. [[mockDelegate expect] dismissedModal:_vc1];
  88. [_modalManager dismissModal:_vc1 completion:nil];
  89. [mockDelegate verify];
  90. }
  91. - (void)testPresentationControllerDidDismiss_ShouldInvokeDelegateDismissedModal {
  92. id mockDelegate = [OCMockObject mockForProtocol:@protocol(RNNModalManagerDelegate)];
  93. _modalManager.delegate = mockDelegate;
  94. UIPresentationController* presentationController = [[UIPresentationController alloc] initWithPresentedViewController:_vc2 presentingViewController:_vc1];
  95. [[mockDelegate expect] dismissedModal:_vc2];
  96. [_modalManager presentationControllerDidDismiss:presentationController];
  97. [mockDelegate verify];
  98. }
  99. #pragma mark RNNModalManagerDelegate
  100. - (void)dismissedMultipleModals:(NSArray *)viewControllers {
  101. _modalDismissedCount = viewControllers.count;
  102. }
  103. - (void)dismissedModal:(UIViewController *)viewController {
  104. _modalDismissedCount = 1;
  105. }
  106. @end