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

Superadmin Partners View (4682) & Category Dropdown Error (4674) #4703

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion spec/controllers/admin/partners_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
let(:super_admin) { create(:super_admin) }

# Ensure partners are created before the test
let!(:partner1) { create(:partner, name: "alpha", organization: organization) }
let!(:partner2) { create(:partner, name: "Bravo", organization: organization) }
let!(:partner1) { create(:partner, name: "alpha", organization: organization) }
let!(:partner3) { create(:partner, name: "Zeus", organization: organization) }

let(:default_params) do
Expand Down
10 changes: 0 additions & 10 deletions spec/controllers/items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,6 @@

expect(response).to have_error
end

it "should load and assign item categories when rendering new" do
# Create some item categories for the organization
create_list(:item_category, 3, organization: organization)

post :create, params: bad_params

expect(response).to render_template(:new)
expect(assigns(:item_categories)).to eq(organization.item_categories.order('name ASC'))
end
end
end

Expand Down
34 changes: 34 additions & 0 deletions spec/requests/items_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@

describe "CREATE #create" do
let!(:existing_item) { create(:item, organization: organization, name: "Really Good Item") }
let!(:item_category) { create(:item_category, organization: organization, name: "Test Category") }

describe "with an already existing item name" do
let(:item_params) do
Expand All @@ -134,6 +135,39 @@
end
end

describe "with invalid parameters" do
let(:invalid_item_params) do
{
item: {
name: "", # Invalid name
partner_key: create(:base_item).partner_key,
value_in_cents: -100, # Invalid value
package_size: nil,
distribution_quantity: nil
}
}
end

it "loads and displays the item categories when rendering new" do
# Create some item categories for the organization
item_categories = create_list(:item_category, 3, organization: organization)

# Attempt to create an item with invalid parameters
post items_path, params: invalid_item_params

# Expect to render the new template
expect(response).to render_template(:new)

# Ensure the item categories are assigned in the controller
expect(assigns(:item_categories)).to eq(organization.item_categories.order('name ASC'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I much prefer giving the categories explicit values and testing against those values rather than pulling them from the DB. You end up basically just copying the code you're testing into the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm having trouble understanding exactly what you mean here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean doing something like

let(:categories) do 
  [
    FactoryBot.create(:item_category, name: 'Apples'),  # include whatever other IDs need to be passed in
    FactoryBot.create(:item_category, name: 'Bananas')
  ] 
end
# ...
expect(assigns(:item_categories)).to eq(categories))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bdeanhardt -- do you need further guidance on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cielf! Past 2 weeks have been chaos, but I'm back! I'm taking a look today :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So sorry about the delay! How does it look now?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to specify an order in the spec. 

To be a bit more directive, here's what I had in mind:

let!(:category1) { FactoryBot.create(:item_category, name: 'Bananas', organization: organization) }
let!(:category2) { FactoryBot.create(:item_category, name: 'Apples' organization: organization) }
let!(:category3) { FactoryBot.create(:item_category, name: 'Umbrella', organization: organization) }

# ...

expect(assigns(:item_categories)).to eq([category2, category1, category3])


# Verify the categories are included in the response body
item_categories.each do |category|
expect(response.body).to include("<option value=\"#{category.id}\">#{category.name}</option>")
end
end
end

describe 'GET #index' do
let(:storage_location) { create(:storage_location, organization: organization) }
let!(:item) { create(:item, organization: organization, name: "ACTIVEITEM") }
Expand Down
Loading