/
eee_helpers_spec.rb
167 lines (150 loc) · 5.36 KB
/
eee_helpers_spec.rb
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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper' )
describe "wiki" do
it "should return simple text as unaltered text" do
wiki("bob").should contain("bob")
end
it "should return an empty string if called with nil" do
wiki(nil).should == ""
end
it "should convert textile to HTML" do
textile = <<_TEXTILE
paragraph 1 *bold text*
paragraph 2
_TEXTILE
wiki(textile).
should have_selector("p", :content => "paragraph 1 bold text") do |p|
p.should have_selector("strong", :content => "bold text")
end
end
it "should wikify temperatures" do
wiki("250F").should contain("250° F")
end
context "data stored in CouchDB" do
before(:each) do
self.stub!(:_db).and_return("http://example.org/couchdb")
end
it "should lookup kid nicknames" do
RestClient.stub!(:get).and_return('{"marsha":"the oldest Brady girl"}')
wiki("[kid:marsha]").should contain("the oldest Brady girl")
end
it "should wikify recipe URIs" do
RestClient.stub!(:get).
and_return('{"_id":"id-123","title":"Title"}')
wiki("[recipe:id-123]").
should have_selector("a",
:href => "/recipes/id-123",
:content => "Title")
end
end
end
describe "image_link" do
it "should return a link tag pointing to the document's image" do
doc = {
'_id' => "foo",
'_attachments' => { 'sample.jpg' => { } }
}
image_link(doc).
should have_selector("img",
:src => "/images/#{doc['_id']}/sample.jpg")
end
it "should return nil if no attachments" do
image_link({ }).should be_nil
end
it "should return nil if no image attachments" do
doc = { '_attachments' => { 'sample.txt' => { } } }
image_link(doc).should be_nil
end
end
describe "pagination" do
before(:each) do
@query = 'foo'
@results = { 'total_rows' => 41, 'limit' => 20, 'skip' => 0}
end
it "should have a link to other pages" do
pagination(@query, @results).
should have_selector("a",
:content => "2",
:href => "/recipes/search?q=foo&page=2")
end
it "should have 3 pages, when results.size > 2 * page size" do
pagination(@query, @results).
should have_selector("a", :content => "3")
end
it "should have only 2 pages, when results.size == 2 * page size" do
@results['total_rows'] = 40
pagination(@query, @results).
should_not have_selector("a", :content => "3")
end
it "should have a link to the next page if before the last page" do
@results['skip'] = 20
pagination(@query, @results).
should have_selector("a", :content => "Next »")
end
it "should not have a link to the next page if on the last page" do
@results['skip'] = 40
pagination(@query, @results).
should have_selector("span", :content => "Next »")
end
it "should have a link to the previous page if past the first page" do
@results['skip'] = 20
pagination(@query, @results).
should have_selector("a", :content => "« Previous")
end
it "should not have a link to the next page if on the first page" do
pagination(@query, @results).
should have_selector("span", :content => "« Previous")
end
it "should mark the current page" do
pagination(@query, @results).
should have_selector("span.current", :content => "1")
end
context "with sorting applied" do
before(:each) do
@results["sort_order"] = [{ "field" => "sort_foo",
"reverse" => false}]
end
it "should have a link to other pages with sorting applied" do
pagination(@query, @results).
should have_selector("a",
:content => "2",
:href => "/recipes/search?q=foo&sort=sort_foo&page=2")
end
it "should have a link to other pages with reverse sorting applied" do
@results["sort_order"].first["reverse"] = true
pagination(@query, @results).
should have_selector("a",
:content => "2",
:href => "/recipes/search?q=foo&sort=sort_foo&order=desc&page=2")
end
end
end
describe "sort_link" do
it "should link the supplied text" do
sort_link("Foo", "sort_foo", "query", { }).
should have_selector("a",
:content => "Foo")
end
it "should link to the query with the supplied sort field" do
sort_link("Foo", "sort_foo", "query", { }).
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo")
end
it "should link in descending order if already sorted on the sort field in ascending order" do
results = {
"sort_order" => [{ "field" => "sort_foo",
"reverse" => false}]
}
sort_link("Foo", "sort_foo", "query", results).
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo&order=desc")
end
it "should link in ascending order if already sorted on the sort field in descending order" do
results = {
"sort_order" => [{ "field" => "sort_foo",
"reverse" => true}]
}
sort_link("Foo", "sort_foo", "query", results).
should have_selector("a",
:href => "/recipes/search?q=query&sort=sort_foo")
end
end