Skip to content

Instantly share code, notes, and snippets.

View giovani-pereira-ifood's full-sized avatar

Giovani Nascimento Pereira giovani-pereira-ifood

View GitHub Profile
func test_pressingActionButton_shouldCallDelegate() {
let button = view.actionButton
button.sendActions(for: .touchUpInside)
XCTAssertTrue(delegateSpy.actionCalled)
}
// On the view's code
lazy var actionButton: UIButton = {
let button = UIButton()
button.addTarget(self, action: #selector(didTapActionButton), for: .touchUpInside)
return button
}()
// On the test code
func test_pressingActionButton_shouldCallDelegate() {
let button = view.actionButton
// On the view's code
func setupGestures() {
let tap = UITapGestureRecognizer(target: self, action: #selector(callDelegateTap))
addGestureRecognizer(tap)
}
// On the test code
func test_tappingView_shouldCallDelegate() {
let sut = MyView()
let gesture = sut.gestureRecognizers?.first { $0 is UITapGestureRecognizer }
class MyViewTests: XCTestCase {
private let delegateSpy = DelegateSpy()
private lazy var sut = {
let sut = MyView()
sut.delegate = delegateSpy
return sut
}()
func test_pressingActionButton_shouldCallDelegate() {
perform(.touchUpInside, from: sut.actionButton, target: sut, args: nil)
extension XCTestCase {
public func perform(event: UIControl.Event, from button: UIControl, target: NSObject, args: Any?) {
let action = button.actions(forTarget: target, forControlEvent: event)?.first ?? ""
target.performSelector(onMainThread: Selector(action), with: args, waitUntilDone: true)
}
public func performGestureRecognizer<T>(type: T.Type, on view: UIView) {
let gesture = view.gestureRecognizers?.first { $0 is T }
let target = (gesture?.value(forKey: "_targets") as? [NSObject])?.first
func expand() {
bottomSheetController.expand()
UIAccessibility.post(notification: .layoutChanged, argument: listView)
}
func collapse() {
bottomSheetController.collapse()
UIAccessibility.post(notification: .layoutChanged, argument: searchView)
}
class SearchView: UIView {
...
override func accessibilityPerformMagicTap() -> Bool {
expandList()
return true
}
}
class ListView: UIView {
...
func configureAccessibility() {
isAccessibilityElement = true
accessibilityTraits = .adjustable
}
override func accessibilityIncrement() {
quickAddButton.increment()
UIAccessibility.post(notification: .announcement, argument: "\(quickAdd.value)")
}
override public var accessibilityViewIsModal: Bool {
get {
switch currentListState {
case .collapsed:
return false
case .expanded:
return true
}
}
set { }
func configureAccessibilityElementsOrder() {
accessibilityElements = [
closeButton,
titleLabel,
createButton,
collectionView,
footer
]
}