best_in_placeish 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,239 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe BestInPlace::BestInPlaceHelpers do
5
+ describe "#best_in_place" do
6
+ before do
7
+ @user = User.new :name => "Lucia",
8
+ :last_name => "Napoli",
9
+ :email => "lucianapoli@gmail.com",
10
+ :address => "Via Roma 99",
11
+ :zip => "25123",
12
+ :country => "2",
13
+ :receive_email => false,
14
+ :description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
15
+ end
16
+
17
+ it "should generate a proper span" do
18
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
19
+ span = nk.css("span")
20
+ span.should_not be_empty
21
+ end
22
+
23
+ describe "general properties" do
24
+ before do
25
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
26
+ @span = nk.css("span")
27
+ end
28
+
29
+ context "when it's an ActiveRecord model" do
30
+ it "should have a proper id" do
31
+ @span.attribute("id").value.should == "best_in_place_user_#{@user.id}_name"
32
+ end
33
+ end
34
+
35
+ context "when it's not an AR model" do
36
+ it "shold generate an html id without any id" do
37
+ nk = Nokogiri::HTML.parse(helper.best_in_place [1,2,3], :first, :path => @user)
38
+ span = nk.css("span")
39
+ span.attribute("id").value.should == "best_in_place_array_first"
40
+ end
41
+ end
42
+
43
+ it "should have the best_in_place class" do
44
+ @span.attribute("class").value.should == "best_in_place"
45
+ end
46
+
47
+ it "should have the correct data-attribute" do
48
+ @span.attribute("data-attribute").value.should == "name"
49
+ end
50
+
51
+ it "should have the correct data-object" do
52
+ @span.attribute("data-object").value.should == "user"
53
+ end
54
+
55
+ it "should have no activator by default" do
56
+ @span.attribute("data-activator").should be_nil
57
+ end
58
+
59
+ it "should have no inner_class by default" do
60
+ @span.attribute("data-inner-class").should be_nil
61
+ end
62
+
63
+ describe "url generation" do
64
+ it "should have the correct default url" do
65
+ @user.save!
66
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
67
+ span = nk.css("span")
68
+ span.attribute("data-url").value.should == "/users/#{@user.id}"
69
+ end
70
+
71
+ it "should use the custom url specified in string format" do
72
+ out = helper.best_in_place @user, :name, :path => "/custom/path"
73
+ nk = Nokogiri::HTML.parse(out)
74
+ span = nk.css("span")
75
+ span.attribute("data-url").value.should == "/custom/path"
76
+ end
77
+
78
+ it "should use the path given in a named_path format" do
79
+ out = helper.best_in_place @user, :name, :path => helper.users_path
80
+ nk = Nokogiri::HTML.parse(out)
81
+ span = nk.css("span")
82
+ span.attribute("data-url").value.should == "/users"
83
+ end
84
+
85
+ it "should use the given path in a hash format" do
86
+ out = helper.best_in_place @user, :name, :path => {:controller => :users, :action => :edit, :id => 23}
87
+ nk = Nokogiri::HTML.parse(out)
88
+ span = nk.css("span")
89
+ span.attribute("data-url").value.should == "/users/23/edit"
90
+ end
91
+ end
92
+
93
+ describe "nil option" do
94
+ it "should have no nil data by default" do
95
+ @span.attribute("data-nil").should be_nil
96
+ end
97
+
98
+ it "should show '' if the object responds with nil for the passed attribute" do
99
+ @user.stub!(:name).and_return(nil)
100
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
101
+ span = nk.css("span")
102
+ span.text.should == ""
103
+ end
104
+
105
+ it "should show '' if the object responds with an empty string for the passed attribute" do
106
+ @user.stub!(:name).and_return("")
107
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
108
+ span = nk.css("span")
109
+ span.text.should == ""
110
+ end
111
+ end
112
+
113
+ it "should have the given inner_class" do
114
+ out = helper.best_in_place @user, :name, :inner_class => "awesome"
115
+ nk = Nokogiri::HTML.parse(out)
116
+ span = nk.css("span")
117
+ span.attribute("data-inner-class").value.should == "awesome"
118
+ end
119
+
120
+ it "should have the given activator" do
121
+ out = helper.best_in_place @user, :name, :activator => "awesome"
122
+ nk = Nokogiri::HTML.parse(out)
123
+ span = nk.css("span")
124
+ span.attribute("data-activator").value.should == "awesome"
125
+ end
126
+ end
127
+
128
+
129
+ context "with a text field attribute" do
130
+ before do
131
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
132
+ @span = nk.css("span")
133
+ end
134
+
135
+ it "should render the name as text" do
136
+ @span.text.should == "Lucia"
137
+ end
138
+
139
+ it "should have an input data-type" do
140
+ @span.attribute("data-type").value.should == "input"
141
+ end
142
+
143
+ it "should have no data-collection" do
144
+ @span.attribute("data-collection").should be_nil
145
+ end
146
+ end
147
+
148
+ context "with a boolean attribute" do
149
+ before do
150
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox)
151
+ @span = nk.css("span")
152
+ end
153
+
154
+ it "should have a checkbox data-type" do
155
+ @span.attribute("data-type").value.should == "checkbox"
156
+ end
157
+
158
+ it "should have the default data-collection" do
159
+ data = ["No", "Yes"]
160
+ @span.attribute("data-collection").value.should == data.to_json
161
+ end
162
+
163
+ it "should render the current option as No" do
164
+ @span.text.should == "No"
165
+ end
166
+
167
+ describe "custom collection" do
168
+ before do
169
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox, :collection => ["Nain", "Da"])
170
+ @span = nk.css("span")
171
+ end
172
+
173
+ it "should show the message with the custom values" do
174
+ @span.text.should == "Nain"
175
+ end
176
+
177
+ it "should render the proper data-collection" do
178
+ @span.attribute("data-collection").value.should == ["Nain", "Da"].to_json
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ context "with a select attribute" do
185
+ before do
186
+ @countries = COUNTRIES.to_a
187
+ nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, :type => :select, :collection => @countries)
188
+ @span = nk.css("span")
189
+ end
190
+
191
+ it "should have a select data-type" do
192
+ @span.attribute("data-type").value.should == "select"
193
+ end
194
+
195
+ it "should have a proper data collection" do
196
+ @span.attribute("data-collection").value.should == @countries.to_json
197
+ end
198
+
199
+ it "should show the current country" do
200
+ @span.text.should == "Italy"
201
+ end
202
+ end
203
+ end
204
+
205
+ describe "#best_in_place_if" do
206
+ context "when the parameters are valid" do
207
+ before(:each) do
208
+ @output = "Some Value"
209
+ @field = :somefield
210
+ @object = mock("object", @field => @output)
211
+ @options = mock("options")
212
+ end
213
+ context "when the condition is true" do
214
+ before {@condition = true}
215
+ context "when the options parameter is left off" do
216
+ it "should call best_in_place with the rest of the parameters and empty options" do
217
+ helper.should_receive(:best_in_place).with(@object, @field, {})
218
+ helper.best_in_place_if @condition, @object, @field
219
+ end
220
+ end
221
+ context "when the options parameter is included" do
222
+ it "should call best_in_place with the rest of the parameters" do
223
+ helper.should_receive(:best_in_place).with(@object, @field, @options)
224
+ helper.best_in_place_if @condition, @object, @field, @options
225
+ end
226
+ end
227
+ end
228
+ context "when the condition is false" do
229
+ before {@condition = false}
230
+ it "should return the value of the field when the options value is left off" do
231
+ helper.best_in_place_if(@condition, @object, @field).should eq @output
232
+ end
233
+ it "should return the value of the field when the options value is included" do
234
+ helper.best_in_place_if(@condition, @object, @field, @options).should eq @output
235
+ end
236
+ end
237
+ end
238
+ end
239
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe "Double initialization bug", :js => true do
5
+ before do
6
+ @user = User.new :name => "Lucia",
7
+ :last_name => "Napoli",
8
+ :email => "lucianapoli@gmail.com",
9
+ :address => "Via Roma 99",
10
+ :zip => "25123",
11
+ :country => "2",
12
+ :receive_email => false,
13
+ :description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
14
+ end
15
+
16
+ it "should be able to change a boolean value" do
17
+ @user.save!
18
+ visit double_init_user_path(@user)
19
+
20
+ within("#receive_email") do
21
+ page.should have_content("No thanks")
22
+ end
23
+
24
+ bip_bool @user, :receive_email
25
+
26
+ visit double_init_user_path(@user)
27
+ within("#receive_email") do
28
+ page.should have_content("Yes of course")
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,165 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe "JS behaviour", :js => true do
5
+ before do
6
+ @user = User.new :name => "Lucia",
7
+ :last_name => "Napoli",
8
+ :email => "lucianapoli@gmail.com",
9
+ :address => "Via Roma 99",
10
+ :zip => "25123",
11
+ :country => "2",
12
+ :receive_email => false,
13
+ :description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
14
+ end
15
+
16
+ describe "nil option" do
17
+ it "should render the default '-' string when the field is empty" do
18
+ @user.name = ""
19
+ @user.save :validate => false
20
+ visit user_path(@user)
21
+
22
+ within("#name") do
23
+ page.should have_content("-")
24
+ end
25
+ end
26
+
27
+ it "should render the passed nil value if the field is empty" do
28
+ @user.last_name = ""
29
+ @user.save :validate => false
30
+ visit user_path(@user)
31
+
32
+ within("#last_name") do
33
+ page.should have_content("Nothing to show")
34
+ end
35
+ end
36
+ end
37
+
38
+ it "should be able to use bip_text to update a text field" do
39
+ @user.save!
40
+ visit user_path(@user)
41
+ within("#email") do
42
+ page.should have_content("lucianapoli@gmail.com")
43
+ end
44
+
45
+ bip_text @user, :email, "new@email.com"
46
+
47
+ visit user_path(@user)
48
+ within("#email") do
49
+ page.should have_content("new@email.com")
50
+ end
51
+ end
52
+
53
+ it "should be able to update a field two consecutive times" do
54
+ @user.save!
55
+ visit user_path(@user)
56
+
57
+ bip_text @user, :email, "new@email.com"
58
+
59
+ within("#email") do
60
+ page.should have_content("new@email.com")
61
+ end
62
+
63
+ bip_text @user, :email, "new_two@email.com"
64
+
65
+ within("#email") do
66
+ page.should have_content("new_two@email.com")
67
+ end
68
+
69
+ visit user_path(@user)
70
+ within("#email") do
71
+ page.should have_content("new_two@email.com")
72
+ end
73
+ end
74
+
75
+ it "should be able to update a field after an error" do
76
+ @user.save!
77
+ visit user_path(@user)
78
+
79
+ bip_text @user, :email, "wrong format"
80
+ page.should have_content("Email has wrong email format")
81
+
82
+ bip_text @user, :email, "another@email.com"
83
+ within("#email") do
84
+ page.should have_content("another@email.com")
85
+ end
86
+
87
+ visit user_path(@user)
88
+ within("#email") do
89
+ page.should have_content("another@email.com")
90
+ end
91
+ end
92
+
93
+ it "should be able to use bil_select to change a select field" do
94
+ @user.save!
95
+ visit user_path(@user)
96
+ within("#country") do
97
+ page.should have_content("Italy")
98
+ end
99
+
100
+ bip_select @user, :country, "France"
101
+
102
+ visit user_path(@user)
103
+ within("#country") do
104
+ page.should have_content("France")
105
+ end
106
+ end
107
+
108
+ it "should be able to use bip_bool to change a boolean value" do
109
+ @user.save!
110
+ visit user_path(@user)
111
+
112
+ within("#receive_email") do
113
+ page.should have_content("No thanks")
114
+ end
115
+
116
+ bip_bool @user, :receive_email
117
+
118
+ visit user_path(@user)
119
+ within("#receive_email") do
120
+ page.should have_content("Yes of course")
121
+ end
122
+ end
123
+
124
+ it "should show validation errors" do
125
+ @user.save!
126
+ visit user_path(@user)
127
+
128
+ bip_text @user, :address, ""
129
+ page.should have_content("Address can't be blank")
130
+ within("#address") do
131
+ page.should have_content("Via Roma 99")
132
+ end
133
+ end
134
+
135
+ it "should show validation errors using respond_with in the controller" do
136
+ @user.save!
137
+ visit user_path(@user)
138
+
139
+ bip_text @user, :last_name, "a"
140
+ page.should have_content("last_name has invalid length")
141
+ end
142
+
143
+ it "should be able to update a field after an error using respond_with in the controller" do
144
+ @user.save!
145
+ visit user_path(@user)
146
+
147
+ bip_text @user, :last_name, "a"
148
+
149
+ within("#last_name") do
150
+ page.should have_content("Napoli")
151
+ end
152
+
153
+ bip_text @user, :last_name, "Another"
154
+
155
+ within("#last_name") do
156
+ page.should have_content("Another")
157
+ end
158
+
159
+ visit user_path(@user)
160
+ within("#last_name") do
161
+ page.should have_content("Another")
162
+ end
163
+ end
164
+ end
165
+
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe "JS behaviour", :js => true do
5
+ before do
6
+ @user = User.new :name => "Lucia",
7
+ :last_name => "Napoli",
8
+ :email => "lucianapoli@gmail.com",
9
+ :address => "Via Roma 99",
10
+ :zip => "25123",
11
+ :country => "2",
12
+ :receive_email => false,
13
+ :description => "User description"
14
+ end
15
+
16
+ it "should be able to use bip_text to update a text area" do
17
+ @user.save!
18
+ visit user_path(@user)
19
+ within("#description") do
20
+ page.should have_content("User description")
21
+ end
22
+
23
+ bip_area @user, :description, "A new description"
24
+
25
+ visit user_path(@user)
26
+ within("#description") do
27
+ page.should have_content("A new description")
28
+ end
29
+ end
30
+ end