|
| 1 | +RSpec.describe "Item management", type: :system do |
| 2 | + before do |
| 3 | + sign_in(@user) |
| 4 | + end |
| 5 | + let!(:url_prefix) { "/#{@organization.to_param}" } |
| 6 | + it "can create a new item as a user" do |
| 7 | + visit url_prefix + "/items/new" |
| 8 | + item_traits = attributes_for(:item) |
| 9 | + fill_in "Name", with: item_traits[:name] |
| 10 | + select BaseItem.last.name, from: "Base Item" |
| 11 | + click_button "Save" |
| 12 | + |
| 13 | + expect(page.find(".alert")).to have_content "added" |
| 14 | + end |
| 15 | + |
| 16 | + it "can create a new item with empty attributes as a user" do |
| 17 | + visit url_prefix + "/items/new" |
| 18 | + click_button "Save" |
| 19 | + |
| 20 | + expect(page.find(".alert")).to have_content "didn't work" |
| 21 | + end |
| 22 | + |
| 23 | + it "can update an existing item as a user" do |
| 24 | + item = create(:item) |
| 25 | + visit url_prefix + "/items/#{item.id}/edit" |
| 26 | + click_button "Save" |
| 27 | + |
| 28 | + expect(page.find(".alert")).to have_content "updated" |
| 29 | + end |
| 30 | + |
| 31 | + it "can update an existing item with empty attributes as a user" do |
| 32 | + item = create(:item) |
| 33 | + visit url_prefix + "/items/#{item.id}/edit" |
| 34 | + fill_in "Name", with: "" |
| 35 | + click_button "Save" |
| 36 | + |
| 37 | + expect(page.find(".alert")).to have_content "didn't work" |
| 38 | + end |
| 39 | + |
| 40 | + it "can filter the #index by base item as a user" do |
| 41 | + Item.delete_all |
| 42 | + create(:item, base_item: BaseItem.first) |
| 43 | + create(:item, base_item: BaseItem.last) |
| 44 | + visit url_prefix + "/items" |
| 45 | + select BaseItem.first.name, from: "filters_by_base_item" |
| 46 | + click_button "Filter" |
| 47 | + within "#tbl_items" do |
| 48 | + expect(page).to have_css("tbody tr", count: 1) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + describe "destroying items" do |
| 53 | + subject { create(:item, name: "DELETEME", organization: @user.organization) } |
| 54 | + context "when an item has history" do |
| 55 | + before do |
| 56 | + create(:donation, :with_items, item: subject) |
| 57 | + end |
| 58 | + it "can be soft-deleted (deactivated) by the user" do |
| 59 | + expect do |
| 60 | + visit url_prefix + "/items" |
| 61 | + expect(page).to have_content(subject.name) |
| 62 | + within "tr[data-item-id='#{subject.id}']" do |
| 63 | + accept_confirm do |
| 64 | + click_on "Delete", match: :first |
| 65 | + end |
| 66 | + end |
| 67 | + page.find(".alert-info") |
| 68 | + end.to change { Item.unscoped.count }.by(0).and change { Item.count }.by(-1) |
| 69 | + subject.reload |
| 70 | + expect(subject).not_to be_active |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context "when an item does not have history" do |
| 75 | + it "can be fully deleted by the user" do |
| 76 | + subject |
| 77 | + expect do |
| 78 | + visit url_prefix + "/items" |
| 79 | + expect(page).to have_content(subject.name) |
| 80 | + within "tr[data-item-id='#{subject.id}']" do |
| 81 | + accept_confirm do |
| 82 | + click_on "Delete", match: :first |
| 83 | + end |
| 84 | + end |
| 85 | + page.find(".alert-info") |
| 86 | + end.to change { Item.unscoped.count }.by(-1).and change { Item.count }.by(-1) |
| 87 | + expect { subject.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + describe "Item Table Tabs >" do |
| 93 | + let(:item_pullups) { create(:item, name: "the most wonderful magical pullups that truly potty train", category: "Magic Toddlers") } |
| 94 | + let(:item_tampons) { create(:item, name: "blackbeard's rugged tampons", category: "Menstrual Products") } |
| 95 | + let(:storage_name) { "the poop catcher warehouse" } |
| 96 | + let(:storage) { create(:storage_location, :with_items, item: item_pullups, item_quantity: num_pullups_in_donation, name: storage_name) } |
| 97 | + let!(:aux_storage) { create(:storage_location, :with_items, item: item_pullups, item_quantity: num_pullups_second_donation, name: "a secret secondary location") } |
| 98 | + let(:num_pullups_in_donation) { 666 } |
| 99 | + let(:num_pullups_second_donation) { 1 } |
| 100 | + let(:num_tampons_in_donation) { 42 } |
| 101 | + let(:num_tampons_second_donation) { 17 } |
| 102 | + let(:donation_tampons) { create(:donation, :with_items, storage_location: storage, item_quantity: num_tampons_in_donation, item: item_tampons) } |
| 103 | + let(:donation_aux_tampons) { create(:donation, :with_items, storage_location: aux_storage, item_quantity: num_tampons_second_donation, item: item_tampons) } |
| 104 | + before do |
| 105 | + storage.increase_inventory(donation_tampons) |
| 106 | + aux_storage.increase_inventory(donation_aux_tampons) |
| 107 | + visit url_prefix + "/items" |
| 108 | + end |
| 109 | + # Consolidated these into one to reduce the setup/teardown |
| 110 | + it "should display items in separate tabs", js: true do |
| 111 | + tab_items_only_text = page.find("table#tbl_items", visible: true).text |
| 112 | + expect(tab_items_only_text).not_to have_content "Quantity" |
| 113 | + expect(tab_items_only_text).to have_content item_pullups.name |
| 114 | + expect(tab_items_only_text).to have_content item_tampons.name |
| 115 | + |
| 116 | + click_link "Items and Quantity" # href="#sectionB" |
| 117 | + tab_items_and_quantity_text = page.find("table#tbl_items_quantity", visible: true).text |
| 118 | + expect(tab_items_and_quantity_text).to have_content "Quantity" |
| 119 | + expect(tab_items_and_quantity_text).not_to have_content storage_name |
| 120 | + expect(tab_items_and_quantity_text).to have_content num_pullups_in_donation |
| 121 | + expect(tab_items_and_quantity_text).to have_content num_pullups_second_donation |
| 122 | + expect(tab_items_and_quantity_text).to have_content num_tampons_in_donation |
| 123 | + expect(tab_items_and_quantity_text).to have_content num_tampons_second_donation |
| 124 | + expect(tab_items_and_quantity_text).to have_content item_pullups.name |
| 125 | + expect(tab_items_and_quantity_text).to have_content item_tampons.name |
| 126 | + |
| 127 | + click_link "Items, Quantity, and Location" # href="#sectionC" |
| 128 | + tab_items_quantity_location_text = page.find("table#tbl_items_location", visible: true).text |
| 129 | + expect(tab_items_quantity_location_text).to have_content "Quantity" |
| 130 | + expect(tab_items_quantity_location_text).to have_content storage_name |
| 131 | + expect(tab_items_quantity_location_text).to have_content num_pullups_in_donation |
| 132 | + expect(tab_items_quantity_location_text).to have_content num_pullups_second_donation |
| 133 | + expect(tab_items_quantity_location_text).to have_content num_pullups_in_donation + num_pullups_second_donation |
| 134 | + expect(tab_items_quantity_location_text).to have_content num_tampons_in_donation |
| 135 | + expect(tab_items_quantity_location_text).to have_content num_tampons_second_donation |
| 136 | + expect(tab_items_quantity_location_text).to have_content num_tampons_in_donation + num_tampons_second_donation |
| 137 | + expect(tab_items_quantity_location_text).to have_content item_pullups.name |
| 138 | + expect(tab_items_quantity_location_text).to have_content item_tampons.name |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments