Last active
November 14, 2024 17:09
-
-
Save paulz/91af10b2655d4b9d1ac34495e2ab357d to your computer and use it in GitHub Desktop.
Make UIAction handler accessible for testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Quick | |
import Nimble | |
class ActionSpec: QuickSpec { | |
override func spec() { | |
describe("UIAction") { | |
it("should invoke handler") { | |
waitUntil { done in | |
let action = UIAction(title: "a title") { action in | |
done() | |
} | |
action.handler(action) | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
class ActionTest: XCTestCase { | |
func testActionHandler() { | |
var handlerWasCalled = false | |
let action = UIAction(title: "a title") { action in | |
handlerWasCalled = true | |
} | |
action.handler(action) | |
XCTAssertTrue(handlerWasCalled, "should be called") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIAction { | |
var handler: UIActionHandler { | |
get { | |
typealias ActionHandlerBlock = @convention(block) (UIAction) -> Void | |
let handler = value(forKey: "handler") as AnyObject | |
return unsafeBitCast(handler, to: ActionHandlerBlock.self) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shame it's hidden in the first place!