CocoaActionSpec.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import Result
  2. import Nimble
  3. import Quick
  4. import ReactiveCocoa
  5. class CocoaActionSpec: QuickSpec {
  6. override func spec() {
  7. var action: Action<Int, Int, NoError>!
  8. beforeEach {
  9. action = Action { value in SignalProducer(value: value + 1) }
  10. expect(action.enabled.value) == true
  11. expect(action.unsafeCocoaAction.enabled).toEventually(beTruthy())
  12. }
  13. #if os(OSX)
  14. it("should be compatible with AppKit") {
  15. let control = NSControl(frame: NSZeroRect)
  16. control.target = action.unsafeCocoaAction
  17. control.action = CocoaAction.selector
  18. control.performClick(nil)
  19. }
  20. #elseif os(iOS)
  21. it("should be compatible with UIKit") {
  22. let control = UIControl(frame: CGRectZero)
  23. control.addTarget(action.unsafeCocoaAction, action: CocoaAction.selector, forControlEvents: UIControlEvents.TouchDown)
  24. control.sendActionsForControlEvents(UIControlEvents.TouchDown)
  25. }
  26. #endif
  27. it("should generate KVO notifications for enabled") {
  28. var values: [Bool] = []
  29. let cocoaAction = action.unsafeCocoaAction
  30. cocoaAction
  31. .rac_valuesForKeyPath("enabled", observer: nil)
  32. .toSignalProducer()
  33. .map { $0! as! Bool }
  34. .start(Observer(next: { values.append($0) }))
  35. expect(values) == [ true ]
  36. let result = action.apply(0).first()
  37. expect(result?.value) == 1
  38. expect(values).toEventually(equal([ true, false, true ]))
  39. _ = cocoaAction
  40. }
  41. it("should generate KVO notifications for executing") {
  42. var values: [Bool] = []
  43. let cocoaAction = action.unsafeCocoaAction
  44. cocoaAction
  45. .rac_valuesForKeyPath("executing", observer: nil)
  46. .toSignalProducer()
  47. .map { $0! as! Bool }
  48. .start(Observer(next: { values.append($0) }))
  49. expect(values) == [ false ]
  50. let result = action.apply(0).first()
  51. expect(result?.value) == 1
  52. expect(values).toEventually(equal([ false, true, false ]))
  53. _ = cocoaAction
  54. }
  55. context("lifetime") {
  56. it("unsafeCocoaAction should not create a retain cycle") {
  57. weak var weakAction: Action<Int, Int, NoError>?
  58. var action: Action<Int, Int, NoError>? = Action { _ in
  59. return SignalProducer(value: 42)
  60. }
  61. weakAction = action
  62. expect(weakAction).notTo(beNil())
  63. _ = action!.unsafeCocoaAction
  64. action = nil
  65. expect(weakAction).to(beNil())
  66. }
  67. }
  68. }
  69. }