|
1 | 1 | RSpec.describe "Item management", type: :system do |
2 | 2 | let(:organization) { create(:organization) } |
3 | | - let(:user) { create(:user, organization: organization) } |
| 3 | + let(:user) { create(:organization_admin, organization: organization) } |
4 | 4 |
|
5 | 5 | before do |
6 | 6 | sign_in(user) |
|
39 | 39 | expect(Item.last.value_in_cents).to eq(123_456) |
40 | 40 | end |
41 | 41 |
|
42 | | - it "can update an existing item as a user" do |
43 | | - item = create(:item) |
44 | | - visit edit_item_path(item.id) |
45 | | - click_button "Save" |
| 42 | + context "update item" do |
| 43 | + let!(:item) { create(:item, organization: organization, name: "Old Name") } |
| 44 | + let(:params) do |
| 45 | + { |
| 46 | + name: "New Name", |
| 47 | + item_category_id: nil, |
| 48 | + reporting_category: "Pads", |
| 49 | + partner_key: "other", |
| 50 | + value_in_cents: 1234, |
| 51 | + package_size: 20, |
| 52 | + on_hand_minimum_quantity: 5, |
| 53 | + on_hand_recommended_quantity: 10, |
| 54 | + distribution_quantity: 2, |
| 55 | + visible_to_partners: true, |
| 56 | + active: true, |
| 57 | + additional_info: "Some additional info" |
| 58 | + } |
| 59 | + end |
| 60 | + |
| 61 | + before do |
| 62 | + visit edit_item_path(item.id) |
| 63 | + fill_in "Name", with: params[:name] |
| 64 | + select params[:reporting_category], from: "Reporting Category" |
| 65 | + fill_in "Value per item", with: params[:value_in_cents] / 100.00 |
| 66 | + fill_in "Package size", with: params[:package_size] |
| 67 | + fill_in "On hand minimum quantity", with: params[:on_hand_minimum_quantity] |
| 68 | + fill_in "On hand recommended quantity", with: params[:on_hand_recommended_quantity] |
| 69 | + fill_in "Quantity Per Individual", with: params[:distribution_quantity] |
| 70 | + fill_in "Additional Info", with: params[:additional_info] |
| 71 | + end |
| 72 | + |
| 73 | + subject { click_button "Save" } |
| 74 | + |
| 75 | + it "updates the item with valid inputs" do |
| 76 | + subject |
| 77 | + item.reload |
| 78 | + expect(page.find(".alert")).to have_content "#{item.name} updated!" |
| 79 | + expect(item.name).to eq(params[:name]) |
| 80 | + expect(item.item_category_id).to eq(params[:item_category_id]) |
| 81 | + expect(item.reporting_category).to eq(params[:reporting_category].underscore) |
| 82 | + expect(item.value_in_cents).to eq(params[:value_in_cents]) |
| 83 | + expect(item.package_size).to eq(params[:package_size]) |
| 84 | + expect(item.on_hand_minimum_quantity).to eq(params[:on_hand_minimum_quantity]) |
| 85 | + expect(item.on_hand_recommended_quantity).to eq(params[:on_hand_recommended_quantity]) |
| 86 | + expect(item.distribution_quantity).to eq(params[:distribution_quantity]) |
| 87 | + expect(item.visible_to_partners).to eq(params[:visible_to_partners]) |
| 88 | + expect(item.active).to eq(params[:active]) |
| 89 | + expect(item.additional_info).to eq(params[:additional_info]) |
| 90 | + end |
46 | 91 |
|
47 | | - expect(page.find(".alert")).to have_content "updated" |
| 92 | + context "item belongs to a kit" do |
| 93 | + let!(:kit) { create(:kit, organization: organization) } |
| 94 | + let!(:item2) { create(:item, organization: organization) } |
| 95 | + let(:kit_value_in_cents) { item.value_in_cents.to_i + item2.value_in_cents.to_i } |
| 96 | + |
| 97 | + before do |
| 98 | + item.update!(kit: kit) |
| 99 | + item2.update!(kit: kit) |
| 100 | + visit edit_item_path(item.id) |
| 101 | + fill_in "Name", with: params[:name] |
| 102 | + end |
| 103 | + |
| 104 | + it "does not allow changing reporting category" do |
| 105 | + expect(page).to have_field("Reporting Category", disabled: true) |
| 106 | + expect(page).to have_content("Kits are reported based on their contents.") |
| 107 | + subject |
| 108 | + expect(kit.value_in_cents).to eq(kit_value_in_cents) |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + context "with invalid inputs" do |
| 113 | + let(:params) do |
| 114 | + super().merge(name: "", reporting_category: "") |
| 115 | + end |
| 116 | + |
| 117 | + it "shows the error messages" do |
| 118 | + subject |
| 119 | + expect(page.find(".alert")).to have_content "Name can't be blank and Reporting category can't be blank" |
| 120 | + end |
| 121 | + end |
48 | 122 | end |
49 | 123 |
|
50 | 124 | it "can update an existing item with empty attributes as a user" do |
|
53 | 127 | fill_in "Name", with: "" |
54 | 128 | click_button "Save" |
55 | 129 |
|
56 | | - expect(page.find(".alert")).to have_content "didn't work" |
| 130 | + expect(page.find(".alert")).to have_content "Name can't be blank" |
57 | 131 | end |
58 | 132 |
|
59 | 133 | it "can make the item invisible to partners" do |
|
111 | 185 | end |
112 | 186 |
|
113 | 187 | # Consolidated these into one to reduce the setup/teardown |
114 | | - it "should display items in separate tabs", js: true do |
| 188 | + it "should display items in separate tabs", js: true, driver: :selenium_chrome do |
115 | 189 | tab_items_only_text = page.find("#items-table", visible: true).text |
116 | 190 | expect(tab_items_only_text).to have_content item_pullups.name |
117 | 191 | expect(tab_items_only_text).to have_content item_tampons.name |
|
0 commit comments