Skip to content

Commit

Permalink
fix the request-spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaryanpal committed Sep 19, 2024
1 parent 2880826 commit 2e6f4a3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
11 changes: 7 additions & 4 deletions app/controllers/partner_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ def update
end

def destroy
@partner_group.destroy

respond_to do |format|
format.html { redirect_to partners_path + "#nav-partner-groups", notice: "Partner Group was successfully deleted." }
if @partner_group.partners.any?
redirect_to partners_path + "#nav-partner-groups", alert: "Partner Group cannot be deleted."
else
@partner_group.destroy
respond_to do |format|
format.html { redirect_to partners_path + "#nav-partner-groups", notice: "Partner Group was successfully deleted." }
end
end
end

Expand Down
43 changes: 31 additions & 12 deletions spec/requests/partner_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,40 @@
end

describe "DELETE #destroy" do
it "destroys the requested partner_group" do
partner_group
expect {
delete partner_group_path(partner_group)
}.to change(PartnerGroup, :count).by(-1)
end
context "when partner group has no partners" do
let!(:partner_group) { create(:partner_group) }
before { get partners_path + "#nav-partner-groups" }
it "destroys the partner group" do
within "#nav-partner-groups" do
expect(response.body).to have_link("Delete")
end
expect {
delete partner_group_path(partner_group)
}.to change(PartnerGroup, :count).by(-1)

it "redirects to partners path with anchor" do
delete partner_group_path(partner_group)
expect(response).to redirect_to(partners_path + "#nav-partner-groups")
expect(flash[:notice]).to eq("Partner Group was successfully deleted.")
expect(response).to redirect_to(partners_path + "#nav-partner-groups")
end
end

it "sets a success notice" do
delete partner_group_path(partner_group)
expect(flash[:notice]).to eq("Partner Group was successfully deleted.")
context "when partner group has partners" do
let!(:partner_group) { create(:partner_group) }

before do
create(:partner, partner_group: partner_group)
get partners_path + "#nav-partner-groups"
end
it "does not destroy the partner group" do
within "#nav-partner-groups" do
expect(reponse.body).not_to have_link("Delete")
end
expect {
delete partner_group_path(partner_group)
}.not_to change(PartnerGroup, :count)

expect(flash[:alert]).to eq("Partner Group cannot be deleted.")
expect(response).to redirect_to(partners_path + "#nav-partner-groups")
end
end
end
end

0 comments on commit 2e6f4a3

Please sign in to comment.