spec/controllers/feedback_controller_spec.rb in europeana-feedback-button-0.0.5 vs spec/controllers/feedback_controller_spec.rb in europeana-feedback-button-0.0.6
- old
+ new
@@ -1,40 +1,103 @@
# frozen_string_literal: true
+
RSpec.describe Europeana::FeedbackButton::FeedbackController do
def main_app
Rails.application.class.routes.url_helpers
end
routes { Europeana::FeedbackButton::Engine.routes }
describe 'POST create' do
- let(:params) { { locale: 'en', type: 'comment', text: 'This is good, five words!', url: main_app.root_path(locale: 'en'), format: :json } }
- subject { post :create, params }
+ subject { -> { post :create, params: params } }
- context 'with recipient configured' do
- before do
- Rails.application.config.x.feedback_mail_to = 'feedback@example.com'
+ let(:valid_params) do
+ { type: 'comment', text: 'This is good, five words!',
+ page: main_app.root_path, privacy_policy: '1', format: :json }
+ end
+ let(:json) { JSON.parse(response.body) }
+
+ before do
+ Europeana::FeedbackButton.mail_to = mail_to
+ end
+
+ shared_examples 'feedback creation failure' do
+ it 'should not queue an email job' do
+ expect { subject.call }.to_not change(ActionMailer::Base.deliveries, :length)
end
+ it 'should return JSON' do
+ subject.call
+ expect(response.content_type).to eq('application/json')
+ end
+
+ it 'should include "success": "false"' do
+ subject.call
+ expect(json['success']).to be(false)
+ end
+
+ it 'should include "message"' do
+ subject.call
+ expect(json['message']).to be_present
+ end
+ end
+
+ shared_examples 'feedback creation success' do
it 'should queue an email job' do
- expect { subject }.to change(ActionMailer::Base.deliveries, :length)
+ expect { subject.call }.to change(ActionMailer::Base.deliveries, :length).by(1)
end
- # it '' do
- # message_delivery = instance_double(ActionMailer::MessageDelivery)
- # expect(ServiceMailer).to receive(:new_user).with(@user).and_return(message_delivery)
- # expect(message_delivery).to receive(:deliver_later)
- # subject
- # expect { subject }.to change { Delayed::Job.where("handler LIKE '%FeedbackMailer%'").count }.by(1)
- # end
+
+ it 'should have response status code 200' do
+ subject.call
+ expect(response.status).to eq(200)
+ end
+
+ it 'should return JSON' do
+ subject.call
+ expect(response.content_type).to eq('application/json')
+ end
+
+ it 'should include "success": "true"' do
+ subject.call
+ expect(json['success']).to be(true)
+ end
end
- context 'without recipient configured' do
- before do
- Rails.application.config.x.feedback_mail_to = nil
+ context 'with recipient configured' do
+ let(:mail_to) { 'feedback@example.com' }
+
+ context 'with valid params' do
+ let(:params) { valid_params }
+
+ it_behaves_like 'feedback creation success'
end
- it 'should not queue an email job' do
- expect { subject }.to_not change(ActionMailer::Base.deliveries, :length)
+ context 'with invalid params' do
+ let(:params) { valid_params.except(:text) }
+
+ it_behaves_like 'feedback creation failure'
+
+ it 'should have response status code 400' do
+ subject.call
+ expect(response.status).to eq(400)
+ end
+
+ it 'includes error messages' do
+ subject.call
+ expect(json['errors']['text']).to be_present
+ end
+ end
+ end
+
+ context 'without recipient configured' do
+ let(:mail_to) { nil }
+ let(:params) { valid_params }
+
+ it_behaves_like 'feedback creation failure'
+
+ it 'should have response status code 500' do
+ subject.call
+ expect(response.status).to eq(500)
end
end
end
end