-
Notifications
You must be signed in to change notification settings - Fork 3
/
ScrollPageControllerSpec.swift
97 lines (81 loc) · 4.43 KB
/
ScrollPageControllerSpec.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import Quick
import Nimble
@testable import <#ModuleName#>
class ScrollPageControllerSpec: QuickSpec {
override func spec() {
describe("ScrollPageController") {
var sut: ScrollPageController!
beforeEach {
sut = ScrollPageController()
}
it("should return nil page offset when no pages") {
expect(sut.pageOffset(for: -1, velocity: 0, in: [])).to(beNil())
expect(sut.pageOffset(for: 0, velocity: 2, in: [])).to(beNil())
expect(sut.pageOffset(for: 1, velocity: -2, in: [])).to(beNil())
}
it("should return nil page fraction when no pages") {
expect(sut.pageFraction(for: -1, in: [])).to(beNil())
expect(sut.pageFraction(for: 0, in: [])).to(beNil())
expect(sut.pageFraction(for: 1, in: [])).to(beNil())
}
describe("three pages") {
var pagePoints: [CGFloat]!
beforeEach {
pagePoints = [0, 100, 300]
}
it("should return correct page fraction") {
expect(sut.pageFraction(for: -1000, in: pagePoints)) == 0
expect(sut.pageFraction(for: 0, in: pagePoints)) == 0
expect(sut.pageFraction(for: 1, in: pagePoints)) == 0.01
expect(sut.pageFraction(for: 50, in: pagePoints)) == 0.5
expect(sut.pageFraction(for: 99, in: pagePoints)) == 0.99
expect(sut.pageFraction(for: 100, in: pagePoints)) == 1
expect(sut.pageFraction(for: 102, in: pagePoints)) == 1.01
expect(sut.pageFraction(for: 200, in: pagePoints)) == 1.5
expect(sut.pageFraction(for: 298, in: pagePoints)) == 1.99
expect(sut.pageFraction(for: 300, in: pagePoints)) == 2
expect(sut.pageFraction(for: 1000, in: pagePoints)) == 2
}
context("touch up") {
var velocity: CGFloat!
beforeEach {
velocity = 0
}
it("should return closest page offset") {
expect(sut.pageOffset(for: -1000, velocity: velocity, in: pagePoints)) == pagePoints[0]
expect(sut.pageOffset(for: 0, velocity: velocity, in: pagePoints)) == pagePoints[0]
expect(sut.pageOffset(for: 49, velocity: velocity, in: pagePoints)) == pagePoints[0]
expect(sut.pageOffset(for: 51, velocity: velocity, in: pagePoints)) == pagePoints[1]
expect(sut.pageOffset(for: 1000, velocity: velocity, in: pagePoints)) == pagePoints[2]
}
}
context("swipe left") {
var velocity: CGFloat!
beforeEach {
velocity = 2
}
it("should return next page offset") {
expect(sut.pageOffset(for: -1000, velocity: velocity, in: pagePoints)) == pagePoints[1]
expect(sut.pageOffset(for: 0, velocity: velocity, in: pagePoints)) == pagePoints[1]
expect(sut.pageOffset(for: 49, velocity: velocity, in: pagePoints)) == pagePoints[1]
expect(sut.pageOffset(for: 51, velocity: velocity, in: pagePoints)) == pagePoints[2]
expect(sut.pageOffset(for: 1000, velocity: velocity, in: pagePoints)) == pagePoints[2]
}
}
context("swipe right") {
var velocity: CGFloat!
beforeEach {
velocity = -2
}
it("should return previous page offset") {
expect(sut.pageOffset(for: -1000, velocity: velocity, in: pagePoints)) == pagePoints[0]
expect(sut.pageOffset(for: 0, velocity: velocity, in: pagePoints)) == pagePoints[0]
expect(sut.pageOffset(for: 49, velocity: velocity, in: pagePoints)) == pagePoints[0]
expect(sut.pageOffset(for: 51, velocity: velocity, in: pagePoints)) == pagePoints[0]
expect(sut.pageOffset(for: 1000, velocity: velocity, in: pagePoints)) == pagePoints[1]
}
}
}
}
}
}