crest_in_place 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/.gitignore +12 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +13 -0
  4. data/CHANGELOG.md +36 -0
  5. data/Gemfile +9 -0
  6. data/README.md +440 -0
  7. data/Rakefile +8 -0
  8. data/crest_in_place-2.1.0.gem +0 -0
  9. data/crest_in_place.gemspec +28 -0
  10. data/lib/assets/javascripts/best_in_place.js +740 -0
  11. data/lib/assets/javascripts/best_in_place.purr.js +10 -0
  12. data/lib/assets/javascripts/jquery.purr.js +135 -0
  13. data/lib/crest_in_place.rb +12 -0
  14. data/lib/crest_in_place/check_version.rb +8 -0
  15. data/lib/crest_in_place/controller_extensions.rb +28 -0
  16. data/lib/crest_in_place/display_methods.rb +44 -0
  17. data/lib/crest_in_place/engine.rb +8 -0
  18. data/lib/crest_in_place/helper.rb +132 -0
  19. data/lib/crest_in_place/railtie.rb +7 -0
  20. data/lib/crest_in_place/test_helpers.rb +42 -0
  21. data/lib/crest_in_place/utils.rb +21 -0
  22. data/lib/crest_in_place/version.rb +3 -0
  23. data/spec/helpers/best_in_place_spec.rb +429 -0
  24. data/spec/integration/double_init_spec.rb +34 -0
  25. data/spec/integration/js_spec.rb +1041 -0
  26. data/spec/integration/live_spec.rb +40 -0
  27. data/spec/integration/text_area_spec.rb +40 -0
  28. data/spec/spec_helper.rb +26 -0
  29. data/spec/support/retry_on_timeout.rb +10 -0
  30. data/test_app/Gemfile +16 -0
  31. data/test_app/README +256 -0
  32. data/test_app/Rakefile +7 -0
  33. data/test_app/app/assets/images/no.png +0 -0
  34. data/test_app/app/assets/images/red_pen.png +0 -0
  35. data/test_app/app/assets/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  36. data/test_app/app/assets/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  37. data/test_app/app/assets/images/ui-bg_flat_10_000000_40x100.png +0 -0
  38. data/test_app/app/assets/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  39. data/test_app/app/assets/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  40. data/test_app/app/assets/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  41. data/test_app/app/assets/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  42. data/test_app/app/assets/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  43. data/test_app/app/assets/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  44. data/test_app/app/assets/images/ui-icons_222222_256x240.png +0 -0
  45. data/test_app/app/assets/images/ui-icons_228ef1_256x240.png +0 -0
  46. data/test_app/app/assets/images/ui-icons_ef8c08_256x240.png +0 -0
  47. data/test_app/app/assets/images/ui-icons_ffd27a_256x240.png +0 -0
  48. data/test_app/app/assets/images/ui-icons_ffffff_256x240.png +0 -0
  49. data/test_app/app/assets/images/yes.png +0 -0
  50. data/test_app/app/assets/javascripts/application.js +35 -0
  51. data/test_app/app/assets/stylesheets/.gitkeep +0 -0
  52. data/test_app/app/assets/stylesheets/jquery-ui-1.8.16.custom.css.erb +357 -0
  53. data/test_app/app/assets/stylesheets/scaffold.css +60 -0
  54. data/test_app/app/assets/stylesheets/style.css.erb +87 -0
  55. data/test_app/app/controllers/admin/users_controller.rb +14 -0
  56. data/test_app/app/controllers/application_controller.rb +3 -0
  57. data/test_app/app/controllers/cuca/cars_controller.rb +16 -0
  58. data/test_app/app/controllers/users_controller.rb +99 -0
  59. data/test_app/app/helpers/application_helper.rb +2 -0
  60. data/test_app/app/helpers/users_helper.rb +29 -0
  61. data/test_app/app/models/cuca/car.rb +5 -0
  62. data/test_app/app/models/user.rb +27 -0
  63. data/test_app/app/views/admin/users/show.html.erb +20 -0
  64. data/test_app/app/views/cuca/cars/show.html.erb +13 -0
  65. data/test_app/app/views/layouts/application.html.erb +14 -0
  66. data/test_app/app/views/users/_form.html.erb +51 -0
  67. data/test_app/app/views/users/double_init.html.erb +72 -0
  68. data/test_app/app/views/users/edit.html.erb +5 -0
  69. data/test_app/app/views/users/email_field.html.erb +1 -0
  70. data/test_app/app/views/users/index.html.erb +25 -0
  71. data/test_app/app/views/users/new.html.erb +5 -0
  72. data/test_app/app/views/users/show.html.erb +141 -0
  73. data/test_app/app/views/users/show_ajax.html.erb +12 -0
  74. data/test_app/config.ru +4 -0
  75. data/test_app/config/application.rb +51 -0
  76. data/test_app/config/boot.rb +13 -0
  77. data/test_app/config/database.yml +22 -0
  78. data/test_app/config/environment.rb +5 -0
  79. data/test_app/config/environments/development.rb +25 -0
  80. data/test_app/config/environments/production.rb +49 -0
  81. data/test_app/config/environments/test.rb +35 -0
  82. data/test_app/config/initializers/backtrace_silencers.rb +7 -0
  83. data/test_app/config/initializers/countries.rb +1 -0
  84. data/test_app/config/initializers/default_date_format.rb +2 -0
  85. data/test_app/config/initializers/inflections.rb +10 -0
  86. data/test_app/config/initializers/mime_types.rb +5 -0
  87. data/test_app/config/initializers/secret_token.rb +7 -0
  88. data/test_app/config/initializers/session_store.rb +8 -0
  89. data/test_app/config/locales/en.yml +5 -0
  90. data/test_app/config/routes.rb +20 -0
  91. data/test_app/db/migrate/20101206205922_create_users.rb +18 -0
  92. data/test_app/db/migrate/20101212170114_add_receive_email_to_user.rb +9 -0
  93. data/test_app/db/migrate/20110115204441_add_description_to_user.rb +9 -0
  94. data/test_app/db/migrate/20111210084202_add_favorite_color_to_users.rb +5 -0
  95. data/test_app/db/migrate/20111210084251_add_favorite_books_to_users.rb +5 -0
  96. data/test_app/db/migrate/20111217215935_add_birth_date_to_users.rb +5 -0
  97. data/test_app/db/migrate/20111224181356_add_money_to_user.rb +5 -0
  98. data/test_app/db/migrate/20120513003308_create_cars.rb +11 -0
  99. data/test_app/db/migrate/20120607172609_add_favorite_movie_to_users.rb +5 -0
  100. data/test_app/db/migrate/20120616170454_add_money_proc_to_users.rb +6 -0
  101. data/test_app/db/migrate/20120620165212_add_height_to_user.rb +5 -0
  102. data/test_app/db/migrate/20130213224102_add_favorite_locale_to_users.rb +5 -0
  103. data/test_app/db/schema.rb +41 -0
  104. data/test_app/db/seeds.rb +19 -0
  105. data/test_app/doc/README_FOR_APP +2 -0
  106. data/test_app/lib/tasks/.gitkeep +0 -0
  107. data/test_app/lib/tasks/cron.rake +7 -0
  108. data/test_app/public/404.html +26 -0
  109. data/test_app/public/422.html +26 -0
  110. data/test_app/public/500.html +26 -0
  111. data/test_app/public/favicon.ico +0 -0
  112. data/test_app/public/robots.txt +5 -0
  113. data/test_app/script/rails +6 -0
  114. data/test_app/test/fixtures/users.yml +17 -0
  115. data/test_app/test/functional/users_controller_test.rb +49 -0
  116. data/test_app/test/performance/browsing_test.rb +9 -0
  117. data/test_app/test/test_helper.rb +13 -0
  118. data/test_app/test/unit/helpers/users_helper_test.rb +4 -0
  119. data/test_app/test/unit/user_test.rb +8 -0
  120. data/test_app/vendor/plugins/.gitkeep +0 -0
  121. metadata +256 -0
@@ -0,0 +1,1041 @@
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
+ :height => "5' 5\"",
10
+ :address => "Via Roma 99",
11
+ :zip => "25123",
12
+ :country => "2",
13
+ :receive_email => false,
14
+ :birth_date => Time.now.utc,
15
+ :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.",
16
+ :money => 100,
17
+ :money_proc => 100,
18
+ :favorite_color => 'Red',
19
+ :favorite_books => "The City of Gold and Lead",
20
+ :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.",
21
+ :favorite_movie => "The Hitchhiker's Guide to the Galaxy"
22
+ end
23
+
24
+ describe "namespaced controllers" do
25
+ it "should be able to use array-notation to describe both object and path" do
26
+ @user.save!
27
+ visit admin_user_path(@user)
28
+
29
+ within("#last_name") { page.should have_content("Napoli") }
30
+ bip_text @user, :last_name, "Other thing"
31
+
32
+ within("#last_name") { page.should have_content("Other thing") }
33
+ end
34
+ end
35
+
36
+ describe "nil option" do
37
+ it "should render an em-dash when the field is empty" do
38
+ @user.name = ""
39
+ @user.save :validate => false
40
+ visit user_path(@user)
41
+
42
+ within("#name") do
43
+ page.should have_content("\u2014")
44
+ end
45
+ end
46
+
47
+ it "should render the default em-dash string when there is an error and if the intial string is em-dash" do
48
+ @user.money = nil
49
+ @user.save!
50
+ visit user_path(@user)
51
+
52
+ bip_text @user, :money, "abcd"
53
+
54
+ within("#money") do
55
+ page.should have_content("\u2014")
56
+ end
57
+ end
58
+
59
+ it "should render the passed nil value if the field is empty" do
60
+ @user.last_name = ""
61
+ @user.save :validate => false
62
+ visit user_path(@user)
63
+
64
+ within("#last_name") do
65
+ page.should have_content("Nothing to show")
66
+ end
67
+ end
68
+
69
+ it "should render html content for nil option" do
70
+ @user.favorite_color = ""
71
+ @user.save!
72
+ visit user_path(@user)
73
+ within("#favorite_color") do
74
+ page.should have_xpath("//span[@class='nil']")
75
+ end
76
+ end
77
+
78
+ it "should render html content for nil option after edit" do
79
+ @user.favorite_color = "Blue"
80
+ @user.save!
81
+ visit user_path(@user)
82
+
83
+ bip_text @user, :favorite_color, ""
84
+
85
+ within("#favorite_color") do
86
+ page.should have_xpath("//span[@class='nil']")
87
+ end
88
+ end
89
+
90
+ it "should display an empty input field the second time I open it" do
91
+ @user.favorite_locale = nil
92
+ @user.save!
93
+ visit user_path(@user)
94
+
95
+ within("#favorite_locale") do
96
+ page.should have_content("N/A")
97
+ end
98
+
99
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_locale
100
+ page.execute_script <<-JS
101
+ $("##{id}").click();
102
+ JS
103
+
104
+ text = page.find("##{id} input").value
105
+ text.should == ""
106
+
107
+ page.execute_script <<-JS
108
+ $("##{id} input[name='favorite_locale']").blur();
109
+ $("##{id} input[name='favorite_locale']").blur();
110
+ JS
111
+ sleep 1
112
+
113
+ page.execute_script <<-JS
114
+ $("##{id}").click();
115
+ JS
116
+
117
+ text = page.find("##{id} input").value
118
+ text.should == ""
119
+ end
120
+ end
121
+
122
+ it "should be able to update last but one item in list" do
123
+ @user.save!
124
+ @user2 = User.create :name => "Test",
125
+ :last_name => "User",
126
+ :email => "test@example.com",
127
+ :height => "5' 5\"",
128
+ :address => "Via Roma 99",
129
+ :zip => "25123",
130
+ :country => "2",
131
+ :receive_email => false,
132
+ :birth_date => Time.now.utc,
133
+ :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.",
134
+ :money => 100,
135
+ :money_proc => 100,
136
+ :favorite_color => 'Red',
137
+ :favorite_books => "The City of Gold and Lead",
138
+ :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."
139
+
140
+ visit users_path
141
+
142
+ within("tr#user_#{@user.id} > .name > span") do
143
+ page.should have_content("Lucia")
144
+ page.should have_xpath("//a[contains(@href,'#{user_path(@user)}')]")
145
+ end
146
+
147
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :name
148
+ page.execute_script <<-JS
149
+ $("#edit_#{@user.id}").click();
150
+ $("##{id} input[name='name']").val('Lisa');
151
+ $("##{id} form").submit();
152
+ JS
153
+
154
+ within("tr#user_#{@user.id} > .name > span") do
155
+ page.should have_content('Lisa')
156
+ end
157
+ end
158
+
159
+ it "should be able to use bip_text to update a text field" do
160
+ @user.save!
161
+ visit user_path(@user)
162
+ within("#email") do
163
+ page.should have_content("lucianapoli@gmail.com")
164
+ end
165
+
166
+ bip_text @user, :email, "new@email.com"
167
+
168
+ visit user_path(@user)
169
+ within("#email") do
170
+ page.should have_content("new@email.com")
171
+ end
172
+ end
173
+
174
+ it "should be able to update a field two consecutive times" do
175
+ @user.save!
176
+ visit user_path(@user)
177
+
178
+ bip_text @user, :email, "new@email.com"
179
+
180
+ within("#email") do
181
+ page.should have_content("new@email.com")
182
+ end
183
+
184
+ bip_text @user, :email, "new_two@email.com"
185
+
186
+ within("#email") do
187
+ page.should have_content("new_two@email.com")
188
+ end
189
+
190
+ visit user_path(@user)
191
+ within("#email") do
192
+ page.should have_content("new_two@email.com")
193
+ end
194
+ end
195
+
196
+ it "should be able to update a field after an error" do
197
+ @user.save!
198
+ visit user_path(@user)
199
+
200
+ bip_text @user, :email, "wrong format"
201
+ page.should have_content("Email has wrong email format")
202
+
203
+ bip_text @user, :email, "another@email.com"
204
+ within("#email") do
205
+ page.should have_content("another@email.com")
206
+ end
207
+
208
+ visit user_path(@user)
209
+ within("#email") do
210
+ page.should have_content("another@email.com")
211
+ end
212
+ end
213
+
214
+ it "should be able to use bip_select to change a select field" do
215
+ @user.save!
216
+ visit user_path(@user)
217
+ within("#country") do
218
+ page.should have_content("Italy")
219
+ end
220
+
221
+ bip_select @user, :country, "France"
222
+
223
+ visit user_path(@user)
224
+ within("#country") do
225
+ page.should have_content("France")
226
+ end
227
+ end
228
+
229
+ it "should apply the inner_class option to a select field" do
230
+ @user.save!
231
+ visit user_path(@user)
232
+
233
+ find('#country span').click
234
+ find('#country').should have_css('select.some_class')
235
+ end
236
+
237
+ it "should be able to use bip_text to change a date field" do
238
+ @user.save!
239
+ today = Time.now.utc.to_date
240
+ visit user_path(@user)
241
+ within("#birth_date") do
242
+ page.should have_content(today)
243
+ end
244
+
245
+ bip_text @user, :birth_date, (today - 1.days)
246
+
247
+ visit user_path(@user)
248
+ within("#birth_date") do
249
+ page.should have_content(today - 1.days)
250
+ end
251
+ end
252
+
253
+ it "should be able to use datepicker to change a date field" do
254
+ @user.save!
255
+ today = Time.now.utc.to_date
256
+ visit user_path(@user)
257
+ within("#birth_date") do
258
+ page.should have_content(today)
259
+ end
260
+
261
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :birth_date
262
+ page.execute_script <<-JS
263
+ $("##{id}").click()
264
+ $(".ui-datepicker-calendar tbody td").not(".ui-datepicker-other-month").first().click()
265
+ JS
266
+
267
+ visit user_path(@user)
268
+ within("#birth_date") do
269
+ page.should have_content(today.beginning_of_month)
270
+ end
271
+ end
272
+
273
+ it "should be able to modify the datepicker options, displaying the date with another format" do
274
+ @user.save!
275
+ today = Time.now.utc.to_date
276
+ visit user_path(@user)
277
+ within("#birth_date") do
278
+ page.should have_content(today)
279
+ end
280
+
281
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :birth_date
282
+ page.execute_script <<-JS
283
+ $("##{id}").click()
284
+ $(".ui-datepicker-calendar tbody td").not(".ui-datepicker-other-month").first().click()
285
+ JS
286
+
287
+ within("#birth_date") do
288
+ page.should have_content(today.beginning_of_month.strftime("%d-%m-%Y"))
289
+ end
290
+ end
291
+
292
+ it "should be able to use bip_bool to change a boolean value" do
293
+ @user.save!
294
+ visit user_path(@user)
295
+
296
+ within("#receive_email") do
297
+ page.should have_content("No thanks")
298
+ end
299
+
300
+ bip_bool @user, :receive_email
301
+
302
+ visit user_path(@user)
303
+ within("#receive_email") do
304
+ page.should have_content("Yes of course")
305
+ end
306
+ end
307
+
308
+ it "should be able to use bip_bool to change a boolean value using an image" do
309
+ @user.save!
310
+ visit user_path(@user)
311
+
312
+ within("#receive_email_image") do
313
+ page.should have_xpath("//img[contains(@src,'no.png')]")
314
+ end
315
+
316
+ bip_bool @user, :receive_email
317
+
318
+ visit user_path(@user)
319
+ within("#receive_email_image") do
320
+ page.should have_xpath("//img[contains(@src,'yes.png')]")
321
+ end
322
+ end
323
+
324
+ it "should correctly use an OK submit button when so configured for an input" do
325
+ @user.save!
326
+ visit user_path(@user)
327
+
328
+ within("#favorite_color") do
329
+ page.should have_content('Red')
330
+ end
331
+
332
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_color
333
+ page.execute_script <<-JS
334
+ $("##{id}").click();
335
+ $("##{id} input[name='favorite_color']").val('Blue');
336
+ JS
337
+
338
+ page.find("##{id} input[type='submit']").value.should == 'Do it!'
339
+ page.should have_css("##{id} input[type='submit'].custom-submit.other-custom-submit")
340
+
341
+ page.execute_script <<-JS
342
+ $("##{id} input[type='submit']").click();
343
+ JS
344
+
345
+ visit user_path(@user)
346
+ within("#favorite_color") do
347
+ page.should have_content('Blue')
348
+ end
349
+ end
350
+
351
+ it "should correctly use a Cancel button when so configured for an input" do
352
+ @user.save!
353
+ visit user_path(@user)
354
+
355
+ within("#favorite_color") do
356
+ page.should have_content('Red')
357
+ end
358
+
359
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_color
360
+ page.execute_script <<-JS
361
+ $("##{id}").click();
362
+ $("##{id} input[name='favorite_color']").val('Blue');
363
+ JS
364
+
365
+ page.find("##{id} input[type='button']").value.should == 'Nope'
366
+ page.should have_css("##{id} input[type='button'].custom-cancel.other-custom-cancel")
367
+
368
+ page.execute_script <<-JS
369
+ $("##{id} input[type='button']").click();
370
+ JS
371
+
372
+ visit user_path(@user)
373
+ within("#favorite_color") do
374
+ page.should have_content('Red')
375
+ end
376
+ end
377
+
378
+ it "should not ask for confirmation on cancel if it is switched off" do
379
+ @user.save!
380
+ visit user_path(@user)
381
+
382
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_movie
383
+ page.execute_script <<-JS
384
+ $("##{id}").click();
385
+ $("##{id} input[name='favorite_movie']").val('No good movie');
386
+ $("##{id} input[type='button']").click();
387
+ JS
388
+
389
+ lambda { page.driver.browser.switch_to.alert }.should raise_exception(Selenium::WebDriver::Error::NoAlertPresentError)
390
+ within("#favorite_movie") do
391
+ page.should have_content("The Hitchhiker's Guide to the Galaxy")
392
+ end
393
+ end
394
+
395
+ it "should not submit input on blur if there's an OK button present" do
396
+ @user.save!
397
+ visit user_path(@user)
398
+
399
+ within("#favorite_color") do
400
+ page.should have_content('Red')
401
+ end
402
+
403
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_color
404
+ page.execute_script <<-JS
405
+ $("##{id}").click();
406
+ $("##{id} input[name='favorite_color']").val('Blue');
407
+ $("##{id} input[name='favorite_color']").blur();
408
+ JS
409
+ sleep 1 # Increase if browser is slow
410
+
411
+ visit user_path(@user)
412
+ within("#favorite_color") do
413
+ page.should have_content('Red')
414
+ end
415
+ end
416
+
417
+ it "should still submit input on blur if there's only a Cancel button present" do
418
+ @user.save!
419
+ visit user_path(@user, :suppress_ok_button => 1)
420
+
421
+ within("#favorite_color") do
422
+ page.should have_content('Red')
423
+ end
424
+
425
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_color
426
+ page.execute_script %{$("##{id}").click();}
427
+ page.should have_no_css("##{id} input[type='submit']")
428
+ page.execute_script <<-JS
429
+ $("##{id} input[name='favorite_color']").val('Blue');
430
+ $("##{id} input[name='favorite_color']").blur();
431
+ $("##{id} input[name='favorite_color']").blur();
432
+ JS
433
+ sleep 1 # Increase if browser is slow
434
+
435
+ visit user_path(@user)
436
+ within("#favorite_color") do
437
+ page.should have_content('Blue')
438
+ end
439
+ end
440
+
441
+ it "should correctly use an OK submit button when so configured for a text area" do
442
+ @user.save!
443
+ visit user_path(@user)
444
+
445
+ within("#favorite_books") do
446
+ page.should have_content('The City of Gold and Lead')
447
+ end
448
+
449
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_books
450
+ page.execute_script <<-JS
451
+ $("##{id}").click();
452
+ $("##{id} textarea").val('1Q84');
453
+ $("##{id} input[type='submit']").click();
454
+ JS
455
+
456
+ visit user_path(@user)
457
+ within("#favorite_books") do
458
+ page.should have_content('1Q84')
459
+ end
460
+ end
461
+
462
+ it "should correctly use a Cancel button when so configured for a text area" do
463
+ @user.save!
464
+ visit user_path(@user)
465
+
466
+ within("#favorite_books") do
467
+ page.should have_content('The City of Gold and Lead')
468
+ end
469
+
470
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_books
471
+ page.execute_script <<-JS
472
+ $("##{id}").click();
473
+ $("##{id} textarea").val('1Q84');
474
+ $("##{id} input[type='button']").click();
475
+ JS
476
+ page.driver.browser.switch_to.alert.accept
477
+
478
+ visit user_path(@user)
479
+ within("#favorite_books") do
480
+ page.should have_content('The City of Gold and Lead')
481
+ end
482
+ end
483
+
484
+ it "should not submit text area on blur if there's an OK button present" do
485
+ @user.save!
486
+ visit user_path(@user)
487
+
488
+ within("#favorite_books") do
489
+ page.should have_content('The City of Gold and Lead')
490
+ end
491
+
492
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_books
493
+ page.execute_script <<-JS
494
+ $("##{id}").click();
495
+ $("##{id} textarea").val('1Q84');
496
+ $("##{id} textarea").blur();
497
+ $("##{id} textarea").blur();
498
+ JS
499
+ sleep 1 # Increase if browser is slow
500
+ page.driver.browser.switch_to.alert.accept
501
+
502
+ visit user_path(@user)
503
+ within("#favorite_books") do
504
+ page.should have_content('The City of Gold and Lead')
505
+ end
506
+ end
507
+
508
+ it "should still submit text area on blur if there's only a Cancel button present" do
509
+ @user.save!
510
+ visit user_path(@user, :suppress_ok_button => 1)
511
+
512
+ within("#favorite_books") do
513
+ page.should have_content('The City of Gold and Lead')
514
+ end
515
+
516
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :favorite_books
517
+ page.execute_script %{$("##{id}").click();}
518
+ page.should have_no_css("##{id} input[type='submit']")
519
+ page.execute_script <<-JS
520
+ $("##{id} textarea").val('1Q84');
521
+ $("##{id} textarea").blur();
522
+ $("##{id} textarea").blur();
523
+ JS
524
+ sleep 1 # Increase if browser is slow
525
+
526
+ visit user_path(@user)
527
+ within("#favorite_books") do
528
+ page.should have_content('1Q84')
529
+ end
530
+ end
531
+
532
+ it "should show validation errors" do
533
+ @user.save!
534
+ visit user_path(@user)
535
+
536
+ bip_text @user, :address, ""
537
+ page.should have_content("Address can't be blank")
538
+ within("#address") do
539
+ page.should have_content("Via Roma 99")
540
+ end
541
+ end
542
+
543
+ it "should fire off a callback when updating a field" do
544
+ @user.save!
545
+ visit user_path(@user)
546
+
547
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :last_name
548
+ page.execute_script <<-JS
549
+ $("##{id}").bind('best_in_place:update', function() { $('body').append('Last name was updated!') });
550
+ JS
551
+
552
+ page.should have_no_content('Last name was updated!')
553
+ bip_text @user, :last_name, 'Another'
554
+ page.should have_content('Last name was updated!')
555
+ end
556
+
557
+ it "should fire off a callback when retrieve success with empty data" do
558
+ @user.save!
559
+ visit user_path(@user)
560
+
561
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :last_name
562
+ page.execute_script <<-JS
563
+ $("##{id}").bind('best_in_place:success', function() { $('body').append('Updated successfully!') });
564
+ JS
565
+
566
+ page.should have_no_content('Updated successfully!')
567
+ bip_text @user, :last_name, 'Empty'
568
+ page.should have_content('Updated successfully!')
569
+ end
570
+
571
+ describe "display_as" do
572
+ it "should render the address with a custom format" do
573
+ @user.save!
574
+ visit user_path(@user)
575
+
576
+ within("#address") do
577
+ page.should have_content("addr => [Via Roma 99]")
578
+ end
579
+ end
580
+
581
+ it "should still show the custom format after an error" do
582
+ @user.save!
583
+ visit user_path(@user)
584
+
585
+ bip_text @user, :address, "inva"
586
+
587
+ within("#address") do
588
+ page.should have_content("addr => [Via Roma 99]")
589
+ end
590
+ end
591
+
592
+ it "should show the new result with the custom format after an update" do
593
+ @user.save!
594
+ visit user_path(@user)
595
+
596
+ bip_text @user, :address, "New address"
597
+
598
+ within("#address") do
599
+ page.should have_content("addr => [New address]")
600
+ end
601
+ end
602
+
603
+ it "should display the original content when editing the form" do
604
+ @user.save!
605
+ retry_on_timeout do
606
+ visit user_path(@user)
607
+
608
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :address
609
+ page.execute_script <<-JS
610
+ $("##{id}").click();
611
+ JS
612
+
613
+ text = page.find("##{id} input").value
614
+ text.should == "Via Roma 99"
615
+ end
616
+ end
617
+
618
+ it "should display the updated content after editing the field two consecutive times" do
619
+ @user.save!
620
+ retry_on_timeout do
621
+ visit user_path(@user)
622
+
623
+ bip_text @user, :address, "New address"
624
+
625
+ sleep 1
626
+
627
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :address
628
+ page.execute_script <<-JS
629
+ $("##{id}").click();
630
+ JS
631
+
632
+ sleep 1
633
+
634
+ text = page.find("##{id} input").value
635
+ text.should == "New address"
636
+ end
637
+ end
638
+
639
+ it "should quote properly the data-original-content attribute" do
640
+ @user.address = "A's & B's"
641
+ @user.save!
642
+ retry_on_timeout do
643
+ visit user_path(@user)
644
+
645
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :address
646
+
647
+ text = page.find("##{id}")["data-original-content"]
648
+ text.should == "A's & B's"
649
+ end
650
+ end
651
+ end
652
+
653
+ describe "display_with" do
654
+ it "should show nil text when original value is nil" do
655
+ @user.description = ""
656
+ @user.save!
657
+
658
+ visit user_path(@user)
659
+
660
+ within("#dw_description") { page.should have_content("\u2014") }
661
+ end
662
+
663
+ it "should render the money using number_to_currency" do
664
+ @user.save!
665
+ visit user_path(@user)
666
+
667
+ within("#money") do
668
+ page.should have_content("$100.00")
669
+ end
670
+ end
671
+
672
+ it "should let me use custom helpers with a lambda" do
673
+ @user.save!
674
+ visit user_path(@user)
675
+
676
+ page.should have_content("100.0 €")
677
+ bip_text @user, :money_custom, "250"
678
+
679
+ within("#money_custom") do
680
+ page.should have_content("250.0 €")
681
+ end
682
+ end
683
+
684
+ it "should still show the custom format after an error" do
685
+ @user.save!
686
+ visit user_path(@user)
687
+
688
+ bip_text @user, :money, "string"
689
+
690
+ page.should have_content("Money is not a number")
691
+
692
+ within("#money") do
693
+ page.should have_content("$100.00")
694
+ end
695
+ end
696
+
697
+ it "should show the new value using the helper after a successful update" do
698
+ @user.save!
699
+ visit user_path(@user)
700
+
701
+ bip_text @user, :money, "240"
702
+
703
+ within("#money") do
704
+ page.should have_content("$240.00")
705
+ end
706
+ end
707
+
708
+ it "should display the original content when editing the form" do
709
+ @user.save!
710
+ retry_on_timeout do
711
+ visit user_path(@user)
712
+
713
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :money
714
+ page.execute_script <<-JS
715
+ $("##{id}").click();
716
+ JS
717
+
718
+ text = page.find("##{id} input").value
719
+ text.should == "100.0"
720
+ end
721
+ end
722
+
723
+ it "should display the updated content after editing the field two consecutive times" do
724
+ @user.save!
725
+
726
+ retry_on_timeout do
727
+ visit user_path(@user)
728
+
729
+ bip_text @user, :money, "40"
730
+
731
+ sleep 1
732
+
733
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :money
734
+ page.execute_script <<-JS
735
+ $("##{id}").click();
736
+ JS
737
+
738
+ sleep 1
739
+
740
+ text = page.find("##{id} input").value
741
+ text.should == "40"
742
+ end
743
+ end
744
+
745
+ it "should show the money in euros" do
746
+ @user.save!
747
+ visit double_init_user_path(@user)
748
+
749
+ within("#alt_money") { page.should have_content("€100.00") }
750
+
751
+ bip_text @user, :money, 58
752
+
753
+ within("#alt_money") { page.should have_content("€58.00") }
754
+ end
755
+
756
+ it "should keep link after edit with display_with :link_to" do
757
+ @user.save!
758
+ visit users_path
759
+ within("tr#user_#{@user.id} > .name > span") do
760
+ page.should have_content("Lucia")
761
+ page.should have_xpath("//a[contains(@href,'#{user_path(@user)}')]")
762
+ end
763
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :name
764
+ page.execute_script <<-JS
765
+ jQuery("#edit_#{@user.id}").click();
766
+ jQuery("##{id} input[name='name']").val('Maria Lucia');
767
+ jQuery("##{id} form").submit();
768
+ JS
769
+ within("tr#user_#{@user.id} > .name > span") do
770
+ page.should have_content("Maria Lucia")
771
+ page.should have_xpath("//a[contains(@href,'#{user_path(@user)}')]")
772
+ end
773
+ end
774
+
775
+ it "should keep link after aborting edit with display_with :link_to" do
776
+ @user.save!
777
+ visit users_path
778
+ within("tr#user_#{@user.id} > .name > span") do
779
+ page.should have_content("Lucia")
780
+ page.should have_xpath("//a[contains(@href,'#{user_path(@user)}')]")
781
+ end
782
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :name
783
+ page.execute_script <<-JS
784
+ jQuery("#edit_#{@user.id}").click();
785
+ jQuery("##{id} input[name='name']").blur();
786
+ jQuery("##{id} input[name='name']").blur();
787
+ JS
788
+ within("tr#user_#{@user.id} > .name > span") do
789
+ page.should have_content("Lucia")
790
+ page.should have_xpath("//a[contains(@href,'#{user_path(@user)}')]")
791
+ end
792
+ end
793
+
794
+ describe "display_with using a lambda" do
795
+ it "should render the money" do
796
+ @user.save!
797
+ visit user_path(@user)
798
+
799
+ within("#money_proc") do
800
+ page.should have_content("$100.00")
801
+ end
802
+ end
803
+
804
+ it "should show the new value using the helper after a successful update" do
805
+ @user.save!
806
+ visit user_path(@user)
807
+
808
+ bip_text @user, :money_proc, "240"
809
+
810
+ within("#money_proc") do
811
+ page.should have_content("$240.00")
812
+ end
813
+ end
814
+
815
+ it "should display the original content when editing the form" do
816
+ @user.save!
817
+ retry_on_timeout do
818
+ visit user_path(@user)
819
+
820
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :money_proc
821
+ page.execute_script <<-JS
822
+ $("##{id}").click();
823
+ JS
824
+
825
+ text = page.find("##{id} input").value
826
+ text.should == "100.0"
827
+ end
828
+ end
829
+
830
+ it "should display the updated content after editing the field two consecutive times" do
831
+ @user.save!
832
+
833
+ retry_on_timeout do
834
+ visit user_path(@user)
835
+
836
+ bip_text @user, :money_proc, "40"
837
+
838
+ sleep 1
839
+
840
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :money_proc
841
+ page.execute_script <<-JS
842
+ $("##{id}").click();
843
+ JS
844
+
845
+ sleep 1
846
+
847
+ text = page.find("##{id} input").value
848
+ text.should == "40"
849
+ end
850
+ end
851
+
852
+ end
853
+
854
+ end
855
+
856
+ it "should display strings with quotes correctly in fields" do
857
+ @user.last_name = "A last name \"with double quotes\""
858
+ @user.save!
859
+
860
+ retry_on_timeout do
861
+ visit user_path(@user)
862
+
863
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :last_name
864
+ page.execute_script <<-JS
865
+ $("##{id}").click();
866
+ JS
867
+
868
+ text = page.find("##{id} input").value
869
+ text.should == "A last name \"with double quotes\""
870
+ end
871
+ end
872
+
873
+ it "should allow me to set texts with quotes with sanitize => false" do
874
+ @user.save!
875
+
876
+ retry_on_timeout do
877
+ visit double_init_user_path(@user)
878
+
879
+ bip_area @user, :description, "A <a href=\"http://google.es\">link in this text</a> not sanitized."
880
+ visit double_init_user_path(@user)
881
+
882
+ page.should have_link("link in this text", :href => "http://google.es")
883
+ end
884
+ end
885
+
886
+ it "should show the input with not-scaped ampersands with sanitize => false" do
887
+ @user.description = "A text with an & and a <b>Raw html</b>"
888
+ @user.save!
889
+
890
+ retry_on_timeout do
891
+ visit double_init_user_path(@user)
892
+
893
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :description
894
+ page.execute_script <<-JS
895
+ $("##{id}").click();
896
+ JS
897
+
898
+ text = page.find("##{id} textarea").value
899
+ text.should == "A text with an & and a <b>Raw html</b>"
900
+ end
901
+ end
902
+
903
+ it "should keep the same value after multipe edits" do
904
+ @user.save!
905
+
906
+ retry_on_timeout do
907
+ visit double_init_user_path(@user)
908
+
909
+ bip_area @user, :description, "A <a href=\"http://google.es\">link in this text</a> not sanitized."
910
+ visit double_init_user_path(@user)
911
+
912
+ page.should have_link("link in this text", :href => "http://google.es")
913
+
914
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :description
915
+ page.execute_script <<-JS
916
+ $("##{id}").click();
917
+ JS
918
+
919
+ page.find("##{id} textarea").value.should eq("A <a href=\"http://google.es\">link in this text</a> not sanitized.")
920
+ end
921
+ end
922
+
923
+ it "should display single- and double-quotes in values appropriately" do
924
+ @user.height = %{5' 6"}
925
+ @user.save!
926
+
927
+ retry_on_timeout do
928
+ visit user_path(@user)
929
+
930
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :height
931
+ page.execute_script <<-JS
932
+ $("##{id}").click();
933
+ JS
934
+
935
+ page.find("##{id} select").value.should eq(%{5' 6"})
936
+ end
937
+ end
938
+
939
+ it "should save single- and double-quotes in values appropriately" do
940
+ @user.height = %{5' 10"}
941
+ @user.save!
942
+
943
+ retry_on_timeout do
944
+ visit user_path(@user)
945
+
946
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :height
947
+ page.execute_script <<-JS
948
+ $("##{id}").click();
949
+ $("##{id} select").val("5' 7\\\"");
950
+ $("##{id} select").blur();
951
+ $("##{id} select").blur();
952
+ JS
953
+
954
+ sleep 1
955
+
956
+ @user.reload
957
+ @user.height.should eq(%{5' 7"})
958
+ end
959
+ end
960
+
961
+ it "should escape javascript in test helpers" do
962
+ @user.save!
963
+
964
+ retry_on_timeout do
965
+ visit user_path(@user)
966
+
967
+ bip_text @user, :last_name, "Other '); alert('hi');"
968
+ sleep 1
969
+
970
+ @user.reload
971
+ @user.last_name.should eq("Other '); alert('hi');")
972
+ end
973
+ end
974
+
975
+ it "should save text in database without encoding" do
976
+ @user.save!
977
+
978
+ retry_on_timeout do
979
+ visit user_path(@user)
980
+
981
+ bip_text @user, :last_name, "Other \"thing\""
982
+ sleep 1
983
+
984
+ @user.reload
985
+ @user.last_name.should eq("Other \"thing\"")
986
+ end
987
+ end
988
+
989
+ it "should not strip html tags" do
990
+ @user.save!
991
+
992
+ retry_on_timeout do
993
+ visit user_path(@user)
994
+
995
+ bip_text @user, :last_name, "<script>alert('hi');</script>"
996
+ within("#last_name") { page.should have_content("<script>alert('hi');</script>") }
997
+
998
+ visit user_path(@user)
999
+
1000
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :last_name
1001
+ page.execute_script <<-JS
1002
+ $("##{id}").click();
1003
+ JS
1004
+
1005
+ page.find("##{id} input").value.should eq("<script>alert('hi');</script>")
1006
+ end
1007
+ end
1008
+
1009
+ it "should generate the select html with the proper current option selected" do
1010
+ @user.save!
1011
+ visit user_path(@user)
1012
+ within("#country") do
1013
+ page.should have_content("Italy")
1014
+ end
1015
+
1016
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :country
1017
+ page.execute_script <<-JS
1018
+ $("##{id}").click();
1019
+ JS
1020
+
1021
+ page.should have_css("##{id} select option[value='2'][selected='selected']")
1022
+ end
1023
+
1024
+ it "should generate the select with the proper current option without reloading the page" do
1025
+ @user.save!
1026
+ visit user_path(@user)
1027
+ within("#country") do
1028
+ page.should have_content("Italy")
1029
+ end
1030
+
1031
+ bip_select @user, :country, "France"
1032
+
1033
+ sleep 1 # Increase if browser is slow
1034
+ id = CrestInPlace::Utils.build_best_in_place_id @user, :country
1035
+ page.execute_script <<-JS
1036
+ $("##{id}").click();
1037
+ JS
1038
+
1039
+ page.should have_css("##{id} select option[value='4'][selected='selected']")
1040
+ end
1041
+ end