Skip to content

Commit 80f0d61

Browse files
author
Jonathan Aguele and Ondrej Rafaj
committed
docs + chaining actions
1 parent f670bb4 commit 80f0d61

File tree

7 files changed

+47
-17
lines changed

7 files changed

+47
-17
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Library that helps you write less code when testing interface in your iOS apps.
1414

1515
## Implementation
1616

17-
After you add SpecTools framework a set of options will become available for most of the UI elements through a spec property.
17+
After you add SpecTools framework a set of options will become available for most of the UI elements through a spec property. Available for iOS and tvOS
1818

1919
These are:
2020
* action
@@ -181,7 +181,7 @@ collectionView.spec.action.tap(item: 3)
181181
collectionView.spec.action.tap(item: 2, section: 1)
182182
```
183183

184-
#### Executing gesture recognizers
184+
#### Executing gesture recognizers (not available on tvOS)
185185

186186
Execute action on any UIGestureRecognizer
187187
```Swift
@@ -194,6 +194,25 @@ Get array of targets from any UIGestureRecognizer
194194
recognizer.spec.action.getTargetInfo()
195195
```
196196

197+
#### Simulating scrolls
198+
199+
Simulate scrolling on any UIScrollView (or table/collection view) while calling all available delegate methods in the right order along the way
200+
- decelerate sets if the scroll view should simulate decelerating after dragging
201+
```Swift
202+
scrollView.spec.action.scroll(to: CGPoint(x: 500, y: 0), decelerate: true)
203+
```
204+
------
205+
Simulate scrolling on any UIScrollView to a specific horizontal page
206+
```Swift
207+
scrollView.spec.action.scroll(horizontalPageIndex: 2, decelerate: false)
208+
```
209+
------
210+
Simulate scrolling on any UIScrollView to a specific vertical page
211+
```Swift
212+
scrollView.spec.action.scroll(verticalPageIndex: 5, decelerate: true)
213+
```
214+
------
215+
197216
### Checks
198217

199218
#### Checking view visibility

SpecTools/Classes/Action/Action+Button.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ extension Action where T: UIButton {
1616

1717
/// Simulate tap on a button
1818
/// - Parameter event: Event type to trigger
19-
public func tap(event: UIControlEvents = .touchUpInside) {
19+
@discardableResult public func tap(event: UIControlEvents = .touchUpInside) -> Action {
2020
element.sendActions(for: event)
21+
return self
2122
}
2223

2324
}

SpecTools/Classes/Action/Action+ScrollView.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension Action where T: UIScrollView {
1616
/// Simulate scroll to a given point
1717
/// - Parameter to: Target offset value
1818
/// - Parameter decelerate: Enables deceleration after dragging
19-
public func scroll(to point: CGPoint, decelerate: Bool = false) {
19+
@discardableResult public func scroll(to point: CGPoint, decelerate: Bool = false) -> Action {
2020
element.delegate?.scrollViewWillBeginDragging?(element)
2121

2222
element.contentOffset = point
@@ -32,22 +32,25 @@ extension Action where T: UIScrollView {
3232
element.delegate?.scrollViewWillBeginDecelerating?(element)
3333
element.delegate?.scrollViewDidEndDecelerating?(element)
3434
}
35+
return self
3536
}
3637

3738
/// Simulate scroll to a given page horizontally
3839
/// - Parameter to: Target page index
3940
/// - Parameter decelerate: Enables deceleration after dragging
40-
public func scroll(horizontalPageIndex index: Int, decelerate: Bool = false) {
41+
@discardableResult public func scroll(horizontalPageIndex index: Int, decelerate: Bool = false) -> Action {
4142
let x = CGFloat(index) * element.frame.width
4243
scroll(to: CGPoint(x: x, y: element.contentOffset.y))
44+
return self
4345
}
4446

4547
/// Simulate scroll to a given page vertically
4648
/// - Parameter to: Target page index
4749
/// - Parameter decelerate: Enables deceleration after dragging
48-
public func scroll(verticalPageIndex index: Int, decelerate: Bool = false) {
50+
@discardableResult public func scroll(verticalPageIndex index: Int, decelerate: Bool = false) -> Action {
4951
let y = CGFloat(index) * element.frame.height
5052
scroll(to: CGPoint(x: element.contentOffset.x, y: y))
53+
return self
5154
}
5255

5356
}

SpecTools/Classes/Action/Action+UICollectionView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ extension Action where T: UICollectionView {
1616
/// Simulate tap on a cell in collection view
1717
/// - Parameter item: Index of an item to tap on
1818
/// - Parameter section: Index of the section to tap on
19-
public func tap(item: Int, section: Int = 0) {
19+
@discardableResult public func tap(item: Int, section: Int = 0) -> Action {
2020
element.delegate?.collectionView?(element, didSelectItemAt: IndexPath(item: item, section: section))
21+
return self
2122
}
2223

2324
}

SpecTools/Classes/Action/Action+UIGestureRecognizer.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public typealias TargetActionInfo = [(target: AnyObject, action: Selector)]
2121
/// Simulate tap on a gesture recognizer
2222
/// - Parameter taps: Number of taps
2323
/// - Parameter touches: Number of touches
24-
public func triggerTap(taps: Int = 1, touches: Int = 1) {
24+
@discardableResult public func triggerTap(taps: Int = 1, touches: Int = 1) -> Action {
2525
if element.isUserInteractionEnabled == false {
2626
fatalError("User interactions are disabled. Gesture recognizer can't be used")
2727
}
@@ -31,9 +31,9 @@ public typealias TargetActionInfo = [(target: AnyObject, action: Selector)]
3131
recognizer.spec.action.execute()
3232
}
3333
}
34+
return self
3435
}
3536

36-
3737
}
3838

3939

@@ -66,11 +66,12 @@ public typealias TargetActionInfo = [(target: AnyObject, action: Selector)]
6666
}
6767

6868
/// Executes all targets on a specific gesture recognizer
69-
public func execute() {
69+
@discardableResult public func execute() -> Action {
7070
let targetsInfo = element.spec.action.getTargetInfo()
7171
for info in targetsInfo {
7272
info.target.performSelector(onMainThread: info.action, with: nil, waitUntilDone: true)
7373
}
74+
return self
7475
}
7576

7677
}

SpecTools/Classes/Action/Action+UITableView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ extension Action where T: UITableView {
1616
/// Simulate tap on a cell in table view
1717
/// - Parameter row: Index of the row to tap on
1818
/// - Parameter section: Index of the section to tap on
19-
public func tap(row: Int, section: Int = 0) {
19+
@discardableResult public func tap(row: Int, section: Int = 0) -> Action {
2020
element.delegate?.tableView?(element, didSelectRowAt: IndexPath(row: row, section: section))
21+
return self
2122
}
2223

2324
}

SpecTools/Classes/Prepare/Prepare+UIViewController.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,33 @@ extension Prepare where T: UIViewController {
4949
// MARK: Preparation methods for UIViewController
5050

5151
/// Will touch view of a view controller in order to get loadView and viewDidLoad called, than manually calls viewWillAppear and viewDidAppear with animations disabled
52-
public func simulatePresentViewController() {
52+
@discardableResult public func simulatePresentViewController() -> Prepare {
5353
element.loadViewIfNeeded()
5454
element.viewWillAppear(false)
5555
element.viewDidAppear(false)
56+
return self
5657
}
5758

5859
/// Set a new size for a view controllers view during runtime
59-
public func set(viewSize: CGSize) {
60+
@discardableResult public func set(viewSize: CGSize) -> Prepare {
6061
element.view.frame.size = viewSize
6162
element.view.setNeedsLayout()
6263
element.view.layoutIfNeeded()
64+
return self
6365
}
6466

6567
/// Set a screensize of a desired device on a view of your view controller, you can specify a custom height. Custom height might be useful when scrollviews are present
66-
public func set(viewSize: DeviceScreenSize, height: CGFloat? = nil) {
68+
@discardableResult public func set(viewSize: DeviceScreenSize, height: CGFloat? = nil) -> Prepare {
6769
var size = DeviceScreenSize.size(for: viewSize)
6870
if height != nil && height! >= 0 {
6971
size.height = height!
7072
}
7173
set(viewSize: size)
74+
return self
7275
}
7376

7477
/// Give view controller a navigation controller
75-
public func assignNavigationController<T>(ofClass classType: T.Type? = nil) where T: UINavigationController {
78+
@discardableResult public func assignNavigationController<T>(ofClass classType: T.Type? = nil) -> Prepare where T: UINavigationController {
7679
var nc: UINavigationController
7780
if classType == nil {
7881
nc = UINavigationController(rootViewController: element)
@@ -81,13 +84,14 @@ extension Prepare where T: UIViewController {
8184
nc = classType!.init(rootViewController: element)
8285
}
8386
nc.spec.prepare.simulatePresentViewController()
87+
return self
8488
}
8589

8690
/// Give view controller a mock navigation controller which mainly allows for testing push/pop functionality
87-
public func assignMockNavigationController() -> MockNavigationController {
91+
@discardableResult public func assignMockNavigationController() -> Prepare {
8892
let nc = MockNavigationController(rootViewController: element)
8993
nc.spec.prepare.simulatePresentViewController()
90-
return nc
94+
return self
9195
}
9296

9397
}

0 commit comments

Comments
 (0)