solidus_reviews 1.2.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +6 -0
- data/.gem_release.yml +5 -0
- data/.gitignore +13 -6
- data/.rspec +1 -1
- data/.rubocop.yml +3 -321
- data/.rubocop_todo.yml +197 -0
- data/CHANGELOG.md +155 -0
- data/Gemfile +14 -17
- data/{LICENSE.md → LICENSE} +1 -1
- data/README.md +63 -41
- data/Rakefile +3 -20
- data/app/assets/stylesheets/spree/frontend/solidus_reviews.css +4 -0
- data/app/controllers/spree/feedback_reviews_controller.rb +2 -8
- data/app/controllers/spree/reviews_controller.rb +29 -2
- data/app/decorators/controllers/solidus_reviews/spree/{admin/products_controller_decorator.rb → products_controller_decorator.rb} +0 -4
- data/app/decorators/models/solidus_reviews/spree/product_decorator.rb +6 -5
- data/app/helpers/spree/reviews_helper.rb +6 -6
- data/app/models/spree/feedback_review.rb +2 -2
- data/app/models/spree/permission_sets/review_display.rb +11 -0
- data/app/models/spree/permission_sets/review_management.rb +11 -0
- data/app/models/spree/review.rb +25 -10
- data/app/models/spree/reviews_ability.rb +7 -2
- data/app/models/spree/reviews_configuration.rb +10 -1
- data/app/overrides/add_reviews_tab_to_admin.rb +12 -4
- data/app/overrides/add_reviews_to_admin_configuration_sidebar.rb +12 -5
- data/app/views/spree/admin/review_settings/edit.html.erb +7 -0
- data/app/views/spree/admin/reviews/edit.html.erb +4 -2
- data/app/views/spree/admin/reviews/index.html.erb +9 -5
- data/app/views/spree/api/reviews/_feedback_review.json.jbuilder +1 -1
- data/app/views/spree/api/reviews/_review.json.jbuilder +1 -1
- data/app/views/spree/reviews/_form.html.erb +6 -4
- data/app/views/spree/reviews/edit.html.erb +3 -0
- data/app/views/spree/shared/_review.html.erb +6 -4
- data/bin/console +17 -0
- data/bin/rails +5 -5
- data/bin/rails-engine +13 -0
- data/bin/rails-sandbox +16 -0
- data/bin/rake +7 -0
- data/bin/sandbox +86 -0
- data/bin/setup +8 -0
- data/config/locales/de-CH.yml +1 -1
- data/config/locales/de.yml +1 -1
- data/config/locales/en-GB.yml +2 -1
- data/config/locales/en.yml +2 -1
- data/config/locales/es.yml +2 -1
- data/config/locales/fr.yml +1 -1
- data/config/locales/it.yml +1 -1
- data/config/locales/pl.yml +1 -1
- data/config/locales/pt-BR.yml +1 -1
- data/config/locales/pt.yml +1 -1
- data/config/locales/ro.yml +1 -1
- data/config/locales/sv.yml +1 -1
- data/config/locales/tr.yml +1 -1
- data/config/locales/uk.yml +1 -1
- data/config/locales/zh-CN.yml +1 -1
- data/config/locales/zh-TW.yml +1 -1
- data/config/routes.rb +3 -1
- data/db/migrate/20120123141326_recalculate_ratings.rb +1 -2
- data/db/migrate/20190613165528_add_verified_purchaser_to_reviews.rb +2 -0
- data/lib/controllers/spree/api/feedback_reviews_controller.rb +91 -0
- data/lib/controllers/spree/api/reviews_controller.rb +16 -14
- data/lib/generators/solidus_reviews/install/install_generator.rb +8 -7
- data/lib/solidus_reviews/engine.rb +26 -0
- data/lib/solidus_reviews/version.rb +5 -0
- data/lib/solidus_reviews.rb +4 -4
- data/solidus_reviews.gemspec +31 -34
- data/spec/controllers/spree/admin/feedback_reviews_controller_spec.rb +1 -1
- data/spec/controllers/spree/admin/review_settings_controller_spec.rb +3 -4
- data/spec/controllers/spree/admin/reviews_controller_spec.rb +3 -3
- data/spec/controllers/spree/api/feedback_reviews_controller_spec.rb +136 -0
- data/spec/controllers/spree/api/reviews_controller_spec.rb +27 -25
- data/spec/controllers/spree/feedback_reviews_controller_spec.rb +8 -8
- data/spec/controllers/spree/reviews_controller_spec.rb +145 -8
- data/spec/features/admin_spec.rb +6 -6
- data/spec/features/reviews_spec.rb +33 -32
- data/spec/fixtures/thinking-cat.jpg +0 -0
- data/spec/models/feedback_review_spec.rb +13 -13
- data/spec/models/product_spec.rb +13 -10
- data/spec/models/review_spec.rb +64 -41
- data/spec/models/reviews_ability_spec.rb +14 -14
- data/spec/models/reviews_configuration_spec.rb +21 -10
- data/spec/spec_helper.rb +18 -13
- data/spec/support/config.rb +7 -0
- data/spec/support/factories.rb +3 -0
- metadata +52 -213
- data/CONTRIBUTING.md +0 -28
- data/lib/spree_reviews/engine.rb +0 -22
- data/spec/controllers/spree/products_controller_spec.rb +0 -11
@@ -27,11 +27,11 @@ describe Spree::FeedbackReviewsController do
|
|
27
27
|
comment = ['Thanks for your review!', 'Cheers'].join("\n")
|
28
28
|
expect {
|
29
29
|
post :create, params: { review_id: review.id,
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
},
|
34
|
-
|
30
|
+
feedback_review: {
|
31
|
+
comment: comment,
|
32
|
+
rating: rating
|
33
|
+
},
|
34
|
+
format: :js }
|
35
35
|
expect(response.status).to eq(200)
|
36
36
|
expect(response).to render_template(:create)
|
37
37
|
}.to change(Spree::Review, :count).by(1)
|
@@ -48,7 +48,7 @@ describe Spree::FeedbackReviewsController do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'sets locale on feedback-review if required by config' do
|
51
|
-
Spree::Reviews::Config
|
51
|
+
stub_spree_preferences(Spree::Reviews::Config, track_locale: true)
|
52
52
|
post :create, params: valid_attributes
|
53
53
|
expect(assigns[:review].locale).to eq I18n.locale.to_s
|
54
54
|
end
|
@@ -58,7 +58,7 @@ describe Spree::FeedbackReviewsController do
|
|
58
58
|
|
59
59
|
expect {
|
60
60
|
post :create, params: valid_attributes
|
61
|
-
}.to raise_error
|
61
|
+
}.to raise_error(RuntimeError)
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'removes all non-numbers from ratings parameter' do
|
@@ -68,7 +68,7 @@ describe Spree::FeedbackReviewsController do
|
|
68
68
|
|
69
69
|
it 'do not create feedback-review if review doesnt exist' do
|
70
70
|
expect {
|
71
|
-
post :create, params: valid_attributes.merge!(
|
71
|
+
post :create, params: valid_attributes.merge!(review_id: nil)
|
72
72
|
}.to raise_error ActionController::UrlGenerationError
|
73
73
|
end
|
74
74
|
end
|
@@ -5,6 +5,7 @@ require 'spec_helper'
|
|
5
5
|
describe Spree::ReviewsController, type: :controller do
|
6
6
|
let(:user) { create(:user) }
|
7
7
|
let(:product) { create(:product) }
|
8
|
+
let(:review) { create(:review, :approved, product: product, user: user) }
|
8
9
|
let(:review_params) do
|
9
10
|
{ product_id: product.slug,
|
10
11
|
review: { rating: 3,
|
@@ -12,9 +13,8 @@ describe Spree::ReviewsController, type: :controller do
|
|
12
13
|
title: 'Great Product',
|
13
14
|
review: 'Some big review text..',
|
14
15
|
images: [
|
15
|
-
fixture_file_upload(File.new(
|
16
|
-
] }
|
17
|
-
}
|
16
|
+
fixture_file_upload(File.new('spec/fixtures/thinking-cat.jpg'))
|
17
|
+
] } }
|
18
18
|
end
|
19
19
|
|
20
20
|
before do
|
@@ -22,7 +22,7 @@ describe Spree::ReviewsController, type: :controller do
|
|
22
22
|
allow(controller).to receive(:spree_user_signed_in?).and_return(true)
|
23
23
|
end
|
24
24
|
|
25
|
-
|
25
|
+
describe '#index' do
|
26
26
|
context 'for a product that does not exist' do
|
27
27
|
it 'responds with a 404' do
|
28
28
|
expect {
|
@@ -43,7 +43,7 @@ describe Spree::ReviewsController, type: :controller do
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
|
46
|
+
describe '#new' do
|
47
47
|
context 'for a product that does not exist' do
|
48
48
|
it 'responds with a 404' do
|
49
49
|
expect {
|
@@ -68,7 +68,40 @@ describe Spree::ReviewsController, type: :controller do
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
|
71
|
+
describe '#edit' do
|
72
|
+
context 'for a product that does not exist' do
|
73
|
+
it 'responds with a 404' do
|
74
|
+
expect {
|
75
|
+
get :edit, params: { id: review.id, product_id: 'not_real' }
|
76
|
+
}.to raise_error(ActiveRecord::RecordNotFound)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'fail if the user is not authorized to edit a review' do
|
81
|
+
allow(controller).to receive(:authorize!).and_raise(RuntimeError)
|
82
|
+
|
83
|
+
expect {
|
84
|
+
post :edit, params: { id: review.id, product_id: product.slug }
|
85
|
+
assert_match 'ryanbig', response.body
|
86
|
+
}.to raise_error RuntimeError
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'render the edit template' do
|
90
|
+
get :edit, params: { id: review.id, product_id: product.slug }
|
91
|
+
expect(response.status).to eq(200)
|
92
|
+
expect(response).to render_template(:edit)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'doesn\'t allow another user to update a users review' do
|
96
|
+
other_user = create(:user)
|
97
|
+
allow(controller).to receive(:spree_current_user).and_return(other_user)
|
98
|
+
get :edit, params: { id: review.id, product_id: product.slug }
|
99
|
+
expect(response).not_to render_template(:edit)
|
100
|
+
expect(flash[:error]).to eq "Authorization Failure"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe '#create' do
|
72
105
|
before { allow(controller).to receive(:spree_current_user).and_return(user) }
|
73
106
|
|
74
107
|
context 'for a product that does not exist' do
|
@@ -146,7 +179,7 @@ describe Spree::ReviewsController, type: :controller do
|
|
146
179
|
|
147
180
|
it 'does not create a review' do
|
148
181
|
expect(Spree::Review.count).to eq 0
|
149
|
-
post :create, params: review_params.merge(
|
182
|
+
post :create, params: review_params.merge(review: { rating: 'not_a_number' })
|
150
183
|
expect(Spree::Review.count).to eq 0
|
151
184
|
end
|
152
185
|
end
|
@@ -154,10 +187,114 @@ describe Spree::ReviewsController, type: :controller do
|
|
154
187
|
# It always sets the locale so preference pointless
|
155
188
|
context 'when config requires locale tracking:' do
|
156
189
|
it 'sets the locale' do
|
157
|
-
Spree::Reviews::Config
|
190
|
+
stub_spree_preferences(Spree::Reviews::Config, track_locale: true)
|
158
191
|
post :create, params: review_params
|
159
192
|
expect(assigns[:review].locale).to eq I18n.locale.to_s
|
160
193
|
end
|
161
194
|
end
|
162
195
|
end
|
196
|
+
|
197
|
+
describe '#update' do
|
198
|
+
before {
|
199
|
+
allow(controller).to receive(:spree_current_user).and_return(user)
|
200
|
+
@review_params = {
|
201
|
+
product_id: product.slug,
|
202
|
+
id: review.id,
|
203
|
+
review: { title: 'Amazing Product' }
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
context 'for a product that does not exist' do
|
208
|
+
it 'responds with a 404' do
|
209
|
+
expect {
|
210
|
+
post :update, params: { id: review.id, product_id: 'not_real' }
|
211
|
+
}.to raise_error(ActiveRecord::RecordNotFound)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'updates a review' do
|
216
|
+
post :update, params: @review_params
|
217
|
+
|
218
|
+
expect(assigns[:review].title).to eq 'Amazing Product'
|
219
|
+
expect(assigns[:review].product).to eq product
|
220
|
+
expect(assigns[:review].user).to eq user
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'updates a review to be a rating only review' do
|
224
|
+
post :update, params: {
|
225
|
+
product_id: product.slug,
|
226
|
+
id: review.id,
|
227
|
+
review: { title: '', review: '', rating: 5 }
|
228
|
+
}
|
229
|
+
|
230
|
+
expect(assigns[:review].title).to eq ''
|
231
|
+
expect(assigns[:review].review).to eq ''
|
232
|
+
expect(assigns[:review].rating).to eq 5
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'updates the attached image' do
|
236
|
+
post :update, params: {
|
237
|
+
product_id: product.slug,
|
238
|
+
id: review.id,
|
239
|
+
review: {
|
240
|
+
images: [
|
241
|
+
fixture_file_upload(File.new('spec/fixtures/thinking-cat.jpg')),
|
242
|
+
]
|
243
|
+
}
|
244
|
+
}
|
245
|
+
expect(assigns[:review].images.count).to eq 1
|
246
|
+
end
|
247
|
+
|
248
|
+
it 'fails if the user is not authorized to create a review' do
|
249
|
+
allow(controller).to receive(:authorize!).and_raise(RuntimeError)
|
250
|
+
|
251
|
+
expect{
|
252
|
+
post :update, params: @review_params
|
253
|
+
}.to raise_error RuntimeError
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'flashes the notice' do
|
257
|
+
post :update, params: @review_params
|
258
|
+
|
259
|
+
expect(flash[:notice]).to eq I18n.t('spree.review_successfully_submitted')
|
260
|
+
end
|
261
|
+
|
262
|
+
it 'redirects to product page' do
|
263
|
+
post :update, params: @review_params
|
264
|
+
review.reload
|
265
|
+
review.valid?
|
266
|
+
expect(response).to redirect_to spree.product_path(product)
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'removes all non-numbers from ratings param' do
|
270
|
+
@review_params[:review][:rating] = 5
|
271
|
+
post :update, params: @review_params
|
272
|
+
expect(controller.params[:review][:rating]).to eq '5'
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'doesnt change the current spree user as reviews user' do
|
276
|
+
post :update, params: @review_params
|
277
|
+
expect(assigns[:review].user_id).to eq user.id
|
278
|
+
end
|
279
|
+
|
280
|
+
context 'with invalid params' do
|
281
|
+
it 'renders edit when review.save fails' do
|
282
|
+
expect_any_instance_of(Spree::Review).to receive(:update).and_return(false)
|
283
|
+
post :update, params: @review_params
|
284
|
+
expect(response).to render_template :edit
|
285
|
+
end
|
286
|
+
|
287
|
+
it 'does not update a review' do
|
288
|
+
original_rating = review.rating
|
289
|
+
original_title = review.title
|
290
|
+
@review_params[:review][:rating] = 'not_a_number'
|
291
|
+
@review_params[:review][:title] = true
|
292
|
+
post :update, params: @review_params
|
293
|
+
|
294
|
+
review.reload
|
295
|
+
expect(review.rating).to eq original_rating
|
296
|
+
expect(review.title).to eq original_title
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
163
300
|
end
|
data/spec/features/admin_spec.rb
CHANGED
@@ -2,21 +2,21 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
RSpec.
|
5
|
+
RSpec.describe 'Review Admin' do
|
6
6
|
stub_authorization!
|
7
7
|
|
8
|
-
|
8
|
+
let!(:review) { create(:review) }
|
9
9
|
|
10
10
|
context 'index' do
|
11
|
-
|
11
|
+
before do
|
12
12
|
visit spree.admin_reviews_path
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
it 'list reviews' do
|
16
16
|
expect(page).to have_text review.product.name
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
it 'approve reviews' do
|
20
20
|
expect(review.approved).to be false
|
21
21
|
within("tr#review_#{review.id}") do
|
22
22
|
find('.approve').click
|
@@ -24,7 +24,7 @@ RSpec.feature 'Review Admin' do
|
|
24
24
|
expect(review.reload.approved).to be true
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
it 'edit reviews' do
|
28
28
|
expect(page).to have_text review.product.name
|
29
29
|
within("tr#review_#{review.id}") do
|
30
30
|
find('.edit').click
|
@@ -2,90 +2,91 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
describe 'Reviews', js: true do
|
6
|
+
let!(:someone) { create(:user, email: 'ryan@spree.com') }
|
7
|
+
let!(:review) { create(:review, :approved, user: someone) }
|
8
|
+
let!(:unapproved_review) { create(:review, product: review.product) }
|
9
9
|
|
10
|
-
|
11
|
-
Spree::Reviews::Config
|
10
|
+
before do
|
11
|
+
stub_spree_preferences(Spree::Reviews::Config, include_unapproved_reviews: false)
|
12
12
|
end
|
13
13
|
|
14
14
|
context 'product with no review' do
|
15
|
-
|
16
|
-
|
15
|
+
let!(:product_no_reviews) { create(:product) }
|
16
|
+
|
17
|
+
it 'informs that no reviews has been written yet' do
|
17
18
|
visit spree.product_path(product_no_reviews)
|
18
19
|
expect(page).to have_text I18n.t('spree.no_reviews_available')
|
19
20
|
end
|
20
21
|
|
21
22
|
# Regression test for #103
|
22
23
|
context "shows correct number of previews" do
|
23
|
-
|
24
|
+
before do
|
24
25
|
FactoryBot.create_list :review, 3, product: product_no_reviews, approved: true
|
25
|
-
Spree::Reviews::Config
|
26
|
+
stub_spree_preferences(Spree::Reviews::Config, preview_size: 2)
|
26
27
|
end
|
27
28
|
|
28
|
-
|
29
|
+
it "displayed reviews are limited by the set preview size" do
|
29
30
|
visit spree.product_path(product_no_reviews)
|
30
|
-
expect(page.all(".review").count).to
|
31
|
+
expect(page.all(".review").count).to be(2)
|
31
32
|
end
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
36
|
context 'when anonymous user' do
|
36
|
-
|
37
|
-
Spree::Reviews::Config
|
37
|
+
before do
|
38
|
+
stub_spree_preferences(Spree::Reviews::Config, require_login: true)
|
38
39
|
end
|
39
40
|
|
40
41
|
context 'visit product with review' do
|
41
|
-
|
42
|
+
before do
|
42
43
|
visit spree.product_path(review.product)
|
43
44
|
end
|
44
45
|
|
45
|
-
|
46
|
+
it 'sees review title' do
|
46
47
|
expect(page).to have_text review.title
|
47
48
|
end
|
48
49
|
|
49
|
-
|
50
|
+
it 'can not create review' do
|
50
51
|
expect(page).not_to have_text I18n.t('spree.write_your_own_review')
|
51
52
|
end
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
55
56
|
context 'when logged in user' do
|
56
|
-
|
57
|
+
let!(:user) { create(:user) }
|
57
58
|
|
58
|
-
|
59
|
+
before do
|
59
60
|
sign_in_as! user
|
60
61
|
end
|
61
62
|
|
62
63
|
context 'visit product with review' do
|
63
|
-
|
64
|
+
before do
|
64
65
|
visit spree.product_path(review.product)
|
65
66
|
end
|
66
67
|
|
67
|
-
|
68
|
+
it 'can see review title' do
|
68
69
|
expect(page).to have_text review.title
|
69
70
|
end
|
70
71
|
|
71
72
|
context 'with unapproved content allowed' do
|
72
|
-
|
73
|
-
Spree::Reviews::Config
|
74
|
-
Spree::Reviews::Config
|
73
|
+
before do
|
74
|
+
stub_spree_preferences(Spree::Reviews::Config, include_unapproved_reviews: true)
|
75
|
+
stub_spree_preferences(Spree::Reviews::Config, display_unapproved_reviews: true)
|
75
76
|
visit spree.product_path(review.product)
|
76
77
|
end
|
77
78
|
|
78
|
-
|
79
|
+
it 'can see unapproved content when allowed' do
|
79
80
|
expect(unapproved_review.approved?).to eq(false)
|
80
81
|
expect(page).to have_text unapproved_review.title
|
81
82
|
end
|
82
83
|
end
|
83
84
|
|
84
|
-
|
85
|
+
it 'can see create new review button' do
|
85
86
|
expect(page).to have_text I18n.t('spree.write_your_own_review')
|
86
87
|
end
|
87
88
|
|
88
|
-
|
89
|
+
it 'can create new review' do
|
89
90
|
click_on I18n.t('spree.write_your_own_review')
|
90
91
|
|
91
92
|
expect(page).to have_text I18n.t('spree.leave_us_a_review_for', name: review.product.name)
|
@@ -97,7 +98,7 @@ feature 'Reviews', js: true do
|
|
97
98
|
fill_in 'review_name', with: user.email
|
98
99
|
fill_in 'review_title', with: 'Great product!'
|
99
100
|
fill_in 'review_review', with: 'Some big review text..'
|
100
|
-
attach_file 'review_images',
|
101
|
+
attach_file 'review_images', 'spec/fixtures/thinking-cat.jpg'
|
101
102
|
click_on 'Submit your review'
|
102
103
|
end
|
103
104
|
|
@@ -108,14 +109,14 @@ feature 'Reviews', js: true do
|
|
108
109
|
end
|
109
110
|
|
110
111
|
context 'visit product with review where show_identifier is false' do
|
111
|
-
|
112
|
-
|
112
|
+
let!(:user) { create(:user) }
|
113
|
+
let!(:review) { create(:review, :approved, :hide_identifier, review: 'review text', user: user) }
|
113
114
|
|
114
|
-
|
115
|
+
before do
|
115
116
|
visit spree.product_path(review.product)
|
116
117
|
end
|
117
118
|
|
118
|
-
|
119
|
+
it 'show anonymous review' do
|
119
120
|
expect(page).to have_text I18n.t('spree.anonymous')
|
120
121
|
expect(page).to have_text 'review text'
|
121
122
|
end
|
Binary file
|
@@ -13,30 +13,30 @@ describe Spree::FeedbackReview do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'does not validate with a nil review' do
|
16
|
-
expect(build(:feedback_review, review: nil)).
|
16
|
+
expect(build(:feedback_review, review: nil)).not_to be_valid
|
17
17
|
end
|
18
18
|
|
19
19
|
context 'rating' do
|
20
20
|
it 'does not validate when no rating is specified' do
|
21
|
-
expect(build(:feedback_review, rating: nil)).
|
21
|
+
expect(build(:feedback_review, rating: nil)).not_to be_valid
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'does not validate when the rating is not a number' do
|
25
|
-
expect(build(:feedback_review, rating: 'not_a_number')).
|
25
|
+
expect(build(:feedback_review, rating: 'not_a_number')).not_to be_valid
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'does not validate when the rating is a float' do
|
29
|
-
expect(build(:feedback_review, rating: 2.718)).
|
29
|
+
expect(build(:feedback_review, rating: 2.718)).not_to be_valid
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'does not validate when the rating is less than 1' do
|
33
|
-
expect(build(:feedback_review, rating: 0)).
|
34
|
-
expect(build(:feedback_review, rating: -5)).
|
33
|
+
expect(build(:feedback_review, rating: 0)).not_to be_valid
|
34
|
+
expect(build(:feedback_review, rating: -5)).not_to be_valid
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'does not validate when the rating is greater than 5' do
|
38
|
-
expect(build(:feedback_review, rating: 6)).
|
39
|
-
expect(build(:feedback_review, rating: 8)).
|
38
|
+
expect(build(:feedback_review, rating: 6)).not_to be_valid
|
39
|
+
expect(build(:feedback_review, rating: 8)).not_to be_valid
|
40
40
|
end
|
41
41
|
|
42
42
|
(1..5).each do |i|
|
@@ -54,11 +54,11 @@ describe Spree::FeedbackReview do
|
|
54
54
|
let!(:feedback_review_3) { create(:feedback_review, created_at: 5.days.ago) }
|
55
55
|
|
56
56
|
it 'properly runs most_recent_first queries' do
|
57
|
-
expect(
|
57
|
+
expect(described_class.most_recent_first.to_a).to eq([feedback_review_2, feedback_review_3, feedback_review_1])
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'defaults to most_recent_first queries' do
|
61
|
-
expect(
|
61
|
+
expect(described_class.all.to_a).to eq([feedback_review_2, feedback_review_3, feedback_review_1])
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -71,9 +71,9 @@ describe Spree::FeedbackReview do
|
|
71
71
|
let!(:fr_feedback_review_1) { create(:feedback_review, locale: 'fr', created_at: 10.days.ago) }
|
72
72
|
|
73
73
|
it 'properly runs localized queries' do
|
74
|
-
expect(
|
75
|
-
expect(
|
76
|
-
expect(
|
74
|
+
expect(described_class.localized('en').to_a).to eq([en_feedback_review_2, en_feedback_review_3, en_feedback_review_1])
|
75
|
+
expect(described_class.localized('es').to_a).to eq([es_feedback_review_1])
|
76
|
+
expect(described_class.localized('fr').to_a).to eq([fr_feedback_review_1])
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
data/spec/models/product_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe Spree::Product do
|
|
7
7
|
it { is_expected.to respond_to(:reviews) }
|
8
8
|
it { is_expected.to respond_to(:stars) }
|
9
9
|
|
10
|
-
|
10
|
+
describe '#stars' do
|
11
11
|
let(:product) { build(:product) }
|
12
12
|
|
13
13
|
it 'rounds' do
|
@@ -27,7 +27,7 @@ describe Spree::Product do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
describe '#recalculate_rating' do
|
31
31
|
let!(:product) { create(:product) }
|
32
32
|
|
33
33
|
context 'when there are approved reviews' do
|
@@ -36,11 +36,8 @@ describe Spree::Product do
|
|
36
36
|
let!(:unapproved_review_1) { create(:review, product: product, approved: false, rating: 4) }
|
37
37
|
|
38
38
|
context "including unapproved reviews" do
|
39
|
-
before
|
40
|
-
Spree::Reviews::Config
|
41
|
-
end
|
42
|
-
after(:all) do
|
43
|
-
Spree::Reviews::Config[:include_unapproved_reviews] = false
|
39
|
+
before do
|
40
|
+
stub_spree_preferences(Spree::Reviews::Config, include_unapproved_reviews: true)
|
44
41
|
end
|
45
42
|
|
46
43
|
it "updates the product average rating and ignores unapproved reviews" do
|
@@ -55,6 +52,10 @@ describe Spree::Product do
|
|
55
52
|
end
|
56
53
|
|
57
54
|
context "only approved reviews" do
|
55
|
+
before do
|
56
|
+
stub_spree_preferences(Spree::Reviews::Config, include_unapproved_reviews: false)
|
57
|
+
end
|
58
|
+
|
58
59
|
it "updates the product average rating and ignores unapproved reviews" do
|
59
60
|
product.avg_rating = 0
|
60
61
|
product.reviews_count = 0
|
@@ -70,10 +71,12 @@ describe Spree::Product do
|
|
70
71
|
context "without unapproved reviews" do
|
71
72
|
let!(:unapproved_review_1) { create(:review, product: product, approved: false, rating: 4) }
|
72
73
|
|
74
|
+
before do
|
75
|
+
stub_spree_preferences(Spree::Reviews::Config, include_unapproved_reviews: false)
|
76
|
+
end
|
77
|
+
|
73
78
|
it "updates the product average rating and ignores unapproved reviews" do
|
74
|
-
product.avg_rating
|
75
|
-
product.reviews_count = 20
|
76
|
-
product.save!
|
79
|
+
product.update_columns(avg_rating: 3, reviews_count: 20)
|
77
80
|
|
78
81
|
product.recalculate_rating
|
79
82
|
expect(product.avg_rating).to eq(0)
|