add spec for Ajax::SmileController

This commit is contained in:
Georg Gadinger 2020-04-30 19:04:20 +02:00
parent dd5f718f31
commit fcdb640b9a
3 changed files with 316 additions and 0 deletions

View file

@ -0,0 +1,300 @@
# coding: utf-8
# frozen_string_literal: true
require "rails_helper"
describe Ajax::SmileController, type: :controller do
shared_examples "returns the expected response" do
it "returns the expected response" do
expect(JSON.parse(subject.body)).to match(expected_response)
end
end
let(:user) { FactoryBot.create(:user) }
describe "#create" do
let(:params) do
{
id: answer_id
}.compact
end
let(:answer) { FactoryBot.create(:answer, user: user) }
subject { post(:create, params: params) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when answer exists" do
let(:answer_id) { answer.id }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
it "creates a smile to the answer" do
expect { subject }.to(change { Smile.count }.by(1))
expect(answer.reload.smiles.ids).to include(Smile.last.id)
end
include_examples "returns the expected response"
end
context "when answer does not exist" do
let(:answer_id) { "nein!" }
let(:expected_response) do
{
"success" => false,
"status" => anything,
"message" => anything
}
end
it "does not create a smile" do
expect { subject }.not_to(change { Smile.count })
end
include_examples "returns the expected response"
end
context "when some parameters are missing" do
let(:answer_id) { nil }
let(:expected_response) do
{
"success" => false,
"status" => "parameter_error",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:answer_id) { answer.id }
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
describe "#destroy" do
let(:answer) { FactoryBot.create(:answer, user: user) }
let(:smile) { FactoryBot.create(:smile, user: user, answer: answer) }
let(:answer_id) { answer.id }
let(:params) do
{
id: answer_id
}
end
subject { delete(:destroy, params: params) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when the smile exists" do
# ensure we already have it in the db
before(:each) { smile }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
it "deletes the smile" do
expect { subject }.to(change { Smile.count }.by(-1))
end
include_examples "returns the expected response"
end
context "when the smile does not exist" do
let(:answer_id) { "sonic_the_hedgehog" }
let(:expected_response) do
{
"success" => false,
"status" => anything,
"message" => anything
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
describe "#create_comment" do
let(:params) do
{
id: comment_id
}.compact
end
let(:answer) { FactoryBot.create(:answer, user: user) }
let(:comment) { FactoryBot.create(:comment, user: user, answer: answer) }
subject { post(:create_comment, params: params) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when comment exists" do
let(:comment_id) { comment.id }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
it "creates a smile to the comment" do
expect { subject }.to(change { CommentSmile.count }.by(1))
expect(comment.reload.smiles.ids).to include(CommentSmile.last.id)
end
include_examples "returns the expected response"
end
context "when comment does not exist" do
let(:comment_id) { "nein!" }
let(:expected_response) do
{
"success" => false,
"status" => anything,
"message" => anything
}
end
it "does not create a smile" do
expect { subject }.not_to(change { CommentSmile.count })
end
include_examples "returns the expected response"
end
context "when some parameters are missing" do
let(:comment_id) { nil }
let(:expected_response) do
{
"success" => false,
"status" => "parameter_error",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:comment_id) { comment.id }
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
describe "#destroy_comment" do
let(:answer) { FactoryBot.create(:answer, user: user) }
let(:comment) { FactoryBot.create(:comment, user: user, answer: answer) }
let(:comment_smile) { FactoryBot.create(:comment_smile, user: user, comment: comment) }
let(:comment_id) { comment.id }
let(:params) do
{
id: comment_id
}
end
subject { delete(:destroy_comment, params: params) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when the smile exists" do
# ensure we already have it in the db
before(:each) { comment_smile }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
it "deletes the smile" do
expect { subject }.to(change { CommentSmile.count }.by(-1))
end
include_examples "returns the expected response"
end
context "when the smile does not exist" do
let(:answer_id) { "sonic_the_hedgehog" }
let(:expected_response) do
{
"success" => false,
"status" => anything,
"message" => anything
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :comment_smile do
user { FactoryBot.build(:user) }
comment { FactoryBot.build(:comment) }
end
end

8
spec/factories/smile.rb Normal file
View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :smile do
user { FactoryBot.build(:user) }
answer { FactoryBot.build(:answer) }
end
end