Skip to content

Instantly share code, notes, and snippets.

@giovani-pereira-ifood
Last active June 11, 2021 13:44
Show Gist options
  • Save giovani-pereira-ifood/9ace37f5da267bf972907e489ddf494f to your computer and use it in GitHub Desktop.
Save giovani-pereira-ifood/9ace37f5da267bf972907e489ddf494f to your computer and use it in GitHub Desktop.
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
let selectorString = String(describing: target)
.components(separatedBy: ", ")
.first?
.replacingOccurrences(of: "(action=", with: "")
.replacingOccurrences(of: "Optional(", with: "")
?? ""
view.perform(.init(stringLiteral: selectorString))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment