Retrospring/spec/controllers/ajax/friend_controller_spec.rb

160 lines
4.0 KiB
Ruby
Raw Normal View History

2020-04-30 11:46:26 -07:00
# frozen_string_literal: true
require "rails_helper"
describe Ajax::FriendController, :ajax_controller, type: :controller do
describe "#create" do
let(:params) do
{
screen_name: screen_name
}
end
subject { post(:create, params: params) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when target user exists" do
let(:target_user) { FactoryBot.create(:user) }
let(:screen_name) { target_user.screen_name }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
it "creates a follow relationship" do
2021-12-31 13:19:21 -08:00
expect(user.followings.ids).not_to include(target_user.id)
expect { subject }.to(change { user.followings.count }.by(1))
expect(user.followings.ids).to include(target_user.id)
2020-04-30 11:46:26 -07:00
end
include_examples "returns the expected response"
end
context "when target user does not exist" do
let(:screen_name) { "tripmeister_eder" }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
it "does not create a follow relationship" do
2021-12-31 13:19:21 -08:00
expect { subject }.not_to(change { user.followings.count })
2020-04-30 11:46:26 -07:00
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:screen_name) { "tutenchamun" }
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
describe "#destroy" do
let(:params) do
{
screen_name: screen_name
}
end
subject { delete(:destroy, params: params) }
context "when user is signed in" do
before(:each) { sign_in(user) }
context "when target user exists" do
let(:target_user) { FactoryBot.create(:user) }
let(:screen_name) { target_user.screen_name }
before(:each) { target_user }
context "when user follows target" do
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything
}
end
before(:each) { user.follow target_user }
it "destroys a follow relationship" do
2021-12-31 13:19:21 -08:00
expect(user.followings.ids).to include(target_user.id)
expect { subject }.to(change { user.followings.count }.by(-1))
expect(user.followings.ids).not_to include(target_user.id)
2020-04-30 11:46:26 -07:00
end
include_examples "returns the expected response"
end
context "when user does not already follow target" do
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
it "does not destroy a follow relationship" do
2021-12-31 13:19:21 -08:00
expect { subject }.not_to(change { user.followings.count })
2020-04-30 11:46:26 -07:00
end
include_examples "returns the expected response"
end
end
context "when target user does not exist" do
let(:screen_name) { "tripmeister_eder" }
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
it "does not destroy a follow relationship" do
2021-12-31 13:19:21 -08:00
expect { subject }.not_to(change { user.followings.count })
2020-04-30 11:46:26 -07:00
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:screen_name) { "tutenchamun" }
let(:expected_response) do
{
"success" => false,
"status" => "fail",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
end