Repository navigation
Expand file tree
/
Copy pathselection_test.go
More file actions
168 lines (140 loc) · 5.82 KB
/
Copy pathselection_test.go
File metadata and controls
168 lines (140 loc) · 5.82 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package biloba_test
import (
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Selecting text", func() {
BeforeEach(func() {
b.Navigate(fixtureServer + "/selection.html")
Eventually("#passage").Should(b.Exist())
})
selectedText := func() string { return b.Run(`window.getSelection().toString()`).(string) }
Describe("SelectText", func() {
It("selects all of the element's text", func() {
b.SelectText("#passage")
Ω(selectedText()).Should(Equal("The quick brown fox"))
})
It("dispatches a mouseup so selection-driven UIs react", func() {
b.SelectText("#passage")
Ω(b.Run(`window._lastMouseUpTarget`)).Should(Equal("passage"))
Ω("#menu").Should(b.HaveClass("open"))
})
It("works in the matcher form", func() {
Eventually("#passage").Should(b.SelectText())
Ω(selectedText()).Should(Equal("The quick brown fox"))
})
It("times out (poll-by-default) when no element matches", func() {
b.WithTimeout(time.Millisecond * 60).SelectText("#non-existing")
ExpectFailures(SatisfyAll(
ContainSubstring("Timed out after"),
ContainSubstring("could not find DOM element matching selector: #non-existing"),
))
})
It("fails fast under Immediate() when no element matches", func() {
b.Immediate().SelectText("#non-existing")
ExpectFailures(ContainSubstring("could not find DOM element matching selector: #non-existing"))
})
It("is a hard error to configure the bare-matcher form", func() {
b.WithTimeout(time.Second).SelectText()
ExpectFailures(ContainSubstring("SelectText(...) returns a matcher"))
})
})
Describe("SelectText with a substring occurrence", func() {
// #repeated is "the cell divides and the cell grows and the cell thrives"
startOffset := func() float64 {
return b.Run(`window.getSelection().getRangeAt(0).startOffset`).(float64)
}
It("selects the 1st occurrence of the substring by default", func() {
b.SelectText("#repeated", "cell")
Eventually("window.getSelection().toString()").Should(b.EvaluateTo("cell"))
Ω(b.Run(`window._lastMouseUpTarget`)).Should(Equal("repeated"))
Ω("#menu").Should(b.HaveClass("open"))
// "the cell divides..." -> 1st "cell" starts at offset 4
Ω(startOffset()).Should(Equal(4.0))
})
It("selects the nth occurrence when given an Occurrence", func() {
b.SelectText("#repeated", "cell", b.Occurrence(2))
Eventually("window.getSelection().toString()").Should(b.EvaluateTo("cell"))
// the 2nd "cell" sits further along the passage than the 1st, so the range's start offset differs
Ω(startOffset()).Should(Equal(25.0))
})
It("proves Occurrence(2) selects a different span than Occurrence(1)", func() {
b.SelectText("#repeated", "cell", b.Occurrence(1))
first := startOffset()
b.SelectText("#repeated", "cell", b.Occurrence(2))
second := startOffset()
Ω(second).Should(BeNumerically(">", first))
})
It("works in the matcher form (which requires an Occurrence)", func() {
Eventually("#repeated").Should(b.SelectText("cell", b.Occurrence(2)))
Eventually("window.getSelection().toString()").Should(b.EvaluateTo("cell"))
Ω(startOffset()).Should(Equal(25.0))
})
It("fails the spec when the substring is not found", func() {
b.Immediate().SelectText("#repeated", "zzz")
ExpectFailures(ContainSubstring(`could not find occurrence 1 of "zzz" (found 0 occurrence(s))`))
})
It("fails the spec when there are not enough occurrences", func() {
b.Immediate().SelectText("#repeated", "cell", b.Occurrence(4))
ExpectFailures(ContainSubstring(`could not find occurrence 4 of "cell" (found 3 occurrence(s))`))
})
It("times out (poll-by-default) when no element matches", func() {
b.WithTimeout(time.Millisecond*60).SelectText("#non-existing", "cell")
ExpectFailures(SatisfyAll(
ContainSubstring("Timed out after"),
ContainSubstring("could not find DOM element matching selector: #non-existing"),
))
})
})
Describe("SelectRange", func() {
It("selects a sub-range by character offset", func() {
b.SelectRange("#passage", 4, 9)
Ω(selectedText()).Should(Equal("quick"))
})
It("selects a range that spans multiple text nodes", func() {
// #rich textContent is "Hello brave world"; chars 6..11 land inside the nested <strong>
b.SelectRange("#rich", 6, 11)
Ω(selectedText()).Should(Equal("brave"))
})
It("dispatches a mouseup", func() {
b.SelectRange("#passage", 4, 9)
Ω(b.Run(`window._lastMouseUpTarget`)).Should(Equal("passage"))
})
It("works in the matcher form", func() {
Eventually("#passage").Should(b.SelectRange(4, 9))
Ω(selectedText()).Should(Equal("quick"))
})
It("fails the spec when the range is out of bounds", func() {
b.Immediate().SelectRange("#passage", 0, 999)
ExpectFailures(ContainSubstring("out of bounds"))
})
It("times out (poll-by-default) when no element matches", func() {
b.WithTimeout(time.Millisecond*60).SelectRange("#non-existing", 0, 1)
ExpectFailures(SatisfyAll(
ContainSubstring("Timed out after"),
ContainSubstring("could not find DOM element matching selector: #non-existing"),
))
})
It("fails fast under Immediate() when no element matches", func() {
b.Immediate().SelectRange("#non-existing", 0, 1)
ExpectFailures(ContainSubstring("could not find DOM element matching selector: #non-existing"))
})
It("is a hard error to configure the bare-matcher form", func() {
b.WithTimeout(time.Second).SelectRange(0, 1)
ExpectFailures(ContainSubstring("SelectRange(...) returns a matcher"))
})
})
Describe("ClearSelection", func() {
It("clears an active selection", func() {
b.SelectText("#passage")
Ω(selectedText()).Should(Equal("The quick brown fox"))
b.ClearSelection()
Ω(selectedText()).Should(Equal(""))
})
It("is a no-op when nothing is selected", func() {
b.ClearSelection()
Ω(selectedText()).Should(Equal(""))
})
})
})