very_best_in_place 2.0.3

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