|
| 1 | +import XCTest |
| 2 | +@testable import TSAlertController |
| 3 | + |
| 4 | +final class TSAlertControllerTests: XCTestCase { |
| 5 | + |
| 6 | + func testTSAlertController_WhenInitialized_ShouldSetTitleMessageAndPreferredStyle() { |
| 7 | + // given |
| 8 | + let alertController = TSAlertController( |
| 9 | + title: "The title of the alert", |
| 10 | + message: "Descriptive text that provides more details about the reason for the alert.", |
| 11 | + preferredStyle: .alert |
| 12 | + ) |
| 13 | + let mockViewController = MockViewController() |
| 14 | + |
| 15 | + // when |
| 16 | + mockViewController.present(alertController, animated: true) |
| 17 | + |
| 18 | + // then |
| 19 | + XCTAssertEqual(alertController.title, "The title of the alert") |
| 20 | + XCTAssertEqual(alertController.message, "Descriptive text that provides more details about the reason for the alert.") |
| 21 | + XCTAssertEqual(alertController.preferredStyle, .alert) |
| 22 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 23 | + } |
| 24 | + |
| 25 | + func testTSAlertController_WhenIntializedWithTextFields_ShouldSetTextFields() { |
| 26 | + // given |
| 27 | + let alertController = TSAlertController( |
| 28 | + title: "The title of the alert", |
| 29 | + preferredStyle: .alert |
| 30 | + ) |
| 31 | + let mockViewController = MockViewController() |
| 32 | + |
| 33 | + alertController.addTextField() |
| 34 | + alertController.addTextField() |
| 35 | + |
| 36 | + // when |
| 37 | + mockViewController.present(alertController, animated: true) |
| 38 | + |
| 39 | + // then |
| 40 | + XCTAssertEqual(alertController.textFields?.count, 2) |
| 41 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 42 | + } |
| 43 | + |
| 44 | + func testTSAlertController_WhenInitializedWithTwoButtonsAndAutomaticAxis_ShouldSetCorrectActionsOrderAndCount() { |
| 45 | + // given |
| 46 | + let alertController = TSAlertController( |
| 47 | + title: "The title of the alert", |
| 48 | + preferredStyle: .alert |
| 49 | + ) |
| 50 | + let mockViewController = MockViewController() |
| 51 | + |
| 52 | + let ok = TSAlertAction(title: "OK", style: .default) |
| 53 | + let cancel = TSAlertAction(title: "Cancel", style: .cancel) |
| 54 | + alertController.addAction(ok) |
| 55 | + alertController.addAction(cancel) |
| 56 | + |
| 57 | + // when |
| 58 | + mockViewController.present(alertController, animated: true) |
| 59 | + |
| 60 | + // then |
| 61 | + let actions = alertController.actions |
| 62 | + XCTAssertEqual(actions.count, 2) |
| 63 | + XCTAssertEqual(actions[0], cancel) // In LTR, the Cancel button is positioned at the far right. |
| 64 | + XCTAssertEqual(alertController.viewConfiguration.buttonGroupAxis, .horizontal) |
| 65 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 66 | + } |
| 67 | + |
| 68 | + func testTSAlertController_WhenIntializedWithFourButtonsAndAutomaticAxis_ShouldSetCorrectActionsOrderAndCount() { |
| 69 | + // given |
| 70 | + let alertController = TSAlertController( |
| 71 | + title: "The title of the alert", |
| 72 | + preferredStyle: .alert |
| 73 | + ) |
| 74 | + let mockViewController = MockViewController() |
| 75 | + |
| 76 | + let home = TSAlertAction(title: "Go to Home", style: .default) |
| 77 | + let profile = TSAlertAction(title: "Go to Profile", style: .default) |
| 78 | + let settings = TSAlertAction(title: "Go to Settings", style: .default) |
| 79 | + let cancel = TSAlertAction(title: "Cancel", style: .cancel) |
| 80 | + alertController.addAction(home) |
| 81 | + alertController.addAction(profile) |
| 82 | + alertController.addAction(cancel) |
| 83 | + alertController.addAction(settings) |
| 84 | + |
| 85 | + // when |
| 86 | + mockViewController.present(alertController, animated: true) |
| 87 | + |
| 88 | + // then |
| 89 | + let actions = alertController.actions |
| 90 | + XCTAssertEqual(actions.count, 4) |
| 91 | + XCTAssertEqual(actions[3], cancel) // If the button axis is vertical, the Cancel button is positioned at the very bottom. |
| 92 | + XCTAssertEqual(actions, [home, profile, settings, cancel]) |
| 93 | + XCTAssertEqual(alertController.viewConfiguration.buttonGroupAxis, .vertical) |
| 94 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 95 | + } |
| 96 | + |
| 97 | + func testTSAlertController_WhenInitalizedWithFourButtonsAndAutomaticAxis_ShouldSetCorrectActionsOrderAndCount() { |
| 98 | + // given |
| 99 | + let alertController = TSAlertController( |
| 100 | + title: "The title of the alert", |
| 101 | + preferredStyle: .alert |
| 102 | + ) |
| 103 | + let mockViewController = MockViewController() |
| 104 | + |
| 105 | + alertController.addAction(.init(title: "Cancel", style: .cancel)) |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + func testTSAlertController_WhenIntlaizedWithAlertStyle_ShouldSetDefaultConfiguration() { |
| 110 | + // given |
| 111 | + let alertController = TSAlertController( |
| 112 | + title: "The title of the alert", |
| 113 | + preferredStyle: .alert |
| 114 | + ) |
| 115 | + let mockViewController = MockViewController() |
| 116 | + |
| 117 | + // when |
| 118 | + mockViewController.present(alertController, animated: true) |
| 119 | + |
| 120 | + // then |
| 121 | + let config = alertController.configuration |
| 122 | + XCTAssertEqual(config.enteringTransition, .fadeInAndScaleDown) |
| 123 | + XCTAssertEqual(config.exitingTransition, .fadeOut) |
| 124 | + XCTAssertEqual(config.headerAnimation, .none) |
| 125 | + XCTAssertEqual(config.buttonGroupAnimation, .none) |
| 126 | + XCTAssertEqual(config.prefersGrabberVisible, false) |
| 127 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 128 | + } |
| 129 | + |
| 130 | + func testTSAlertController_WhenIntializedWithAlertStyle_SouldSetEnforceCorrectConfiguration() { |
| 131 | + // given |
| 132 | + let alertController = TSAlertController( |
| 133 | + title: "The title of the alert", |
| 134 | + preferredStyle: .alert |
| 135 | + ) |
| 136 | + let mockViewController = MockViewController() |
| 137 | + |
| 138 | + // when |
| 139 | + alertController.configuration.prefersGrabberVisible = true |
| 140 | + mockViewController.present(alertController, animated: true) |
| 141 | + |
| 142 | + // then |
| 143 | + let config = alertController.configuration |
| 144 | + XCTAssertEqual(config.prefersGrabberVisible, false) |
| 145 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 146 | + } |
| 147 | + |
| 148 | + func testTSAlertController_WhenIntializedWithFloatingSheetStyle_ShouldSetDefaultConfiguration() { |
| 149 | + // given |
| 150 | + let alertController = TSAlertController( |
| 151 | + title: "The title of the alert", |
| 152 | + preferredStyle: .floatingSheet |
| 153 | + ) |
| 154 | + let mockViewController = MockViewController() |
| 155 | + |
| 156 | + // when |
| 157 | + mockViewController.present(alertController, animated: true) |
| 158 | + |
| 159 | + // then |
| 160 | + let config = alertController.configuration |
| 161 | + XCTAssertEqual(config.enteringTransition, .slideUp) |
| 162 | + XCTAssertEqual(config.exitingTransition, .slideDown) |
| 163 | + XCTAssertEqual(config.headerAnimation, .none) |
| 164 | + XCTAssertEqual(config.buttonGroupAnimation, .none) |
| 165 | + XCTAssertEqual(config.prefersGrabberVisible, true) |
| 166 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 167 | + } |
| 168 | + |
| 169 | + func testTSAlertController_WhenIntilizedWithFloatinSheetStyle_ShouldSetEnforceCorrectConfiguration() { |
| 170 | + // given |
| 171 | + let alertController = TSAlertController( |
| 172 | + title: "The title of the alert", |
| 173 | + preferredStyle: .floatingSheet |
| 174 | + ) |
| 175 | + let mockViewController = MockViewController() |
| 176 | + |
| 177 | + // when |
| 178 | + mockViewController.present(alertController, animated: true) |
| 179 | + |
| 180 | + // then |
| 181 | + XCTAssertTrue(true) |
| 182 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 183 | + } |
| 184 | + |
| 185 | + func testTSAlertController_WhenIntializedWithAlertStyle_ShouldSetDefaultViewConfiguration() { |
| 186 | + // given |
| 187 | + let alertController = TSAlertController( |
| 188 | + title: "The title of the alert", |
| 189 | + preferredStyle: .alert |
| 190 | + ) |
| 191 | + let mockViewController = MockViewController() |
| 192 | + |
| 193 | + // when |
| 194 | + mockViewController.present(alertController, animated: true) |
| 195 | + |
| 196 | + // then |
| 197 | + let viewConfig = alertController.viewConfiguration |
| 198 | + XCTAssertEqual(viewConfig.size.width, .proportional(minimumRatio: 0.75, maximumRatio: 0.75)) |
| 199 | + XCTAssertEqual(viewConfig.spacing.keyboardSpacing, 100) |
| 200 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 201 | + } |
| 202 | + |
| 203 | + func testTSAlertController_WhenInitializedWithFloatingSheetStyle_ShouldSetDefaultViewConfiguration() { |
| 204 | + // given |
| 205 | + let alertController = TSAlertController( |
| 206 | + title: "The title of the alert", |
| 207 | + preferredStyle: .floatingSheet |
| 208 | + ) |
| 209 | + let mockViewController = MockViewController() |
| 210 | + |
| 211 | + // when |
| 212 | + mockViewController.present(alertController, animated: true) |
| 213 | + |
| 214 | + // then |
| 215 | + let viewConfig = alertController.viewConfiguration |
| 216 | + XCTAssertEqual(viewConfig.size.width, .proportional(minimumRatio: 0.95, maximumRatio: 0.95)) |
| 217 | + XCTAssertEqual(viewConfig.spacing.keyboardSpacing, 20) |
| 218 | + XCTAssertEqual(mockViewController.presentViewControllerTarget, alertController) |
| 219 | + } |
| 220 | + |
| 221 | +} |
0 commit comments