Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4655: [BUG] If you have an error on entering a partner group, the Item Categories changes to a yes/no! Obviously wrong! #4668

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/controllers/partner_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class PartnerGroupsController < ApplicationController

def new
@partner_group = current_organization.partner_groups.new
@item_categories = current_organization.item_categories
set_items_categories
end

def create
Expand All @@ -13,12 +13,14 @@ def create
redirect_to partners_path + "#nav-partner-groups", notice: "Partner group added!"
else
flash[:error] = "Something didn't work quite right -- try again?"
set_items_categories
render action: :new
end
end

def edit
@item_categories = current_organization.item_categories
@partner_group = current_organization.partner_groups.find(params[:id])
set_items_categories
end

def update
Expand All @@ -27,6 +29,7 @@ def update
redirect_to partners_path + "#nav-partner-groups", notice: "Partner group edited!"
else
flash[:error] = "Something didn't work quite right -- try again?"
set_items_categories
render action: :edit
end
end
Expand All @@ -51,4 +54,8 @@ def set_partner_group
def partner_group_params
params.require(:partner_group).permit(:name, :send_reminders, :deadline_day, :reminder_day, item_category_ids: [])
end

def set_items_categories
@item_categories = current_organization.item_categories
end
end
121 changes: 121 additions & 0 deletions spec/requests/partner_groups_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
RSpec.describe PartnerGroupsController, type: :request do
let(:organization) { create(:organization) }
let(:user) { create(:user, organization: organization) }
let!(:category_item) { create(:item_category, organization: organization, name: "Test Item") }
let!(:category_item2) { create(:item_category, organization: organization, name: "Another Test Item") }

context "While signed in" do
before do
sign_in(user)
end

describe "GET #new" do
subject { get new_partner_group_path }

it "renders the new template and assigns variables correctly" do
get new_partner_group_path

expect(response).to render_template(:new)
expect(assigns(:partner_group)).to be_a_new(PartnerGroup)
expect(assigns(:item_categories)).to eq([category_item, category_item2])
expect(response.body).to include("Test Item")
expect(response.body).to include("Another Test Item")
end
end

describe "POST #create" do
let(:attributes) { nil }

subject { post partner_groups_path, params: {partner_group: attributes} }

context "with valid attributes" do
let(:attributes) { attributes_for(:partner_group, organization_id: organization.id, name: "partner group") }

it "creates a new partner group" do
expect { subject }.to change(PartnerGroup, :count).by(1)
expect(PartnerGroup.last.name).to eq("partner group")
end

it "redirects to the partners path" do
expect(subject).to redirect_to(partners_path + "#nav-partner-groups")
end
end

context "with invalid attributes" do
let(:attributes) { {name: "existing_partner"} }
let!(:partner_group) { create(:partner_group, organization: organization, name: "existing_partner") }

it "does not create a new partner group" do
expect { subject }.not_to change(PartnerGroup, :count)
end

it "re-renders the new template with assigned values" do
expect(subject).to render_template(:new)
expect(assigns(:partner_group)).to be_a_new(PartnerGroup)
expect(assigns(:item_categories)).to eq([category_item, category_item2])
expect(subject).to have_error("Something didn't work quite right -- try again?")
expect(response.body).to include("Test Item")
expect(response.body).to include("Another Test Item")
end
end
end

describe "GET #edit" do
let(:partner_group) { create(:partner_group, organization: organization) }

subject { get edit_partner_group_path(partner_group) }

it "renders the edit template and assigns variables correctly" do
get edit_partner_group_path(partner_group)

expect(response).to render_template(:edit)
expect(assigns(:partner_group)).to eq(partner_group)
expect(assigns(:item_categories)).to eq([category_item, category_item2])
expect(response.body).to include("Test Item")
expect(response.body).to include("Another Test Item")
end
end

describe "PATCH #update" do
let(:partner_group) { create(:partner_group, organization: organization) }
let(:attributes) { nil }

subject { patch partner_group_path(partner_group), params: {partner_group: attributes} }

context "with valid attributes" do
let(:attributes) { {name: "new name"} }

it "updates the partner group" do
expect { subject }.to change { partner_group.reload.name }.from(partner_group.name).to("new name")
end

it "redirects to the partners path" do
expect(subject).to redirect_to(partners_path + "#nav-partner-groups")
end
end

context "with invalid attributes" do
let(:attributes) { {name: nil} }

it "does not update the partner group" do
expect { subject }.not_to change { partner_group.reload.name }
end

it "re-renders the edit template with assigned values" do
expect(subject).to render_template(:edit)
expect(assigns(:partner_group)).to eq(partner_group)
expect(assigns(:item_categories)).to eq([category_item, category_item2])
expect(subject).to have_error("Something didn't work quite right -- try again?")
expect(response.body).to include("Test Item")
expect(response.body).to include("Another Test Item")
end
end
end
end

context "While not signed in" do
let(:object) { create(:partner_group, organization: organization) }

include_examples "requiring authorization"
end
end
Loading