|
16 | 16 | before { create(:vendor) } |
17 | 17 |
|
18 | 18 | context "html" do |
19 | | - let!(:first_vendor) { create(:vendor, business_name: "Abc", organization: organization) } |
| 19 | + let!(:no_purchases_vendor) { create(:vendor, business_name: "Abc", organization: organization) } |
| 20 | + let(:purchase_vendor) { create(:vendor, business_name: "Xyz", organization: organization) } |
20 | 21 | let!(:deactivated_vendor) { create(:vendor, business_name: "Deactivated", organization: organization, active: false) } |
21 | 22 |
|
22 | 23 | let(:response_format) { 'html' } |
23 | 24 |
|
24 | 25 | it { is_expected.to be_successful } |
25 | 26 |
|
| 27 | + before do |
| 28 | + create(:purchase, :with_items, vendor: purchase_vendor) |
| 29 | + end |
| 30 | + |
26 | 31 | it "should have only activated vendor names" do |
27 | 32 | subject |
28 | | - expect(response.body).to include(first_vendor.business_name) |
| 33 | + expect(response.body).to include(no_purchases_vendor.business_name) |
29 | 34 | expect(response.body).not_to include(deactivated_vendor.business_name) |
30 | 35 | end |
31 | 36 |
|
32 | | - it "should have a deactivate button for each active vendor" do |
33 | | - expect(subject.body.scan("Deactivate").count).to eq(2) |
| 37 | + it "should have a delete button for no_purchases_vendor and a deactivate button for purchase_vendor" do |
| 38 | + subject |
| 39 | + parsed_body = Nokogiri::HTML(response.body) |
| 40 | + no_purchases_vendor_row = parsed_body.css("tr").find { |row| row.text.include?(no_purchases_vendor.business_name) } |
| 41 | + purchase_vendor_row = parsed_body.css("tr").find { |row| row.text.include?(purchase_vendor.business_name) } |
| 42 | + |
| 43 | + expect(no_purchases_vendor_row.at_css("a", text: "Delete")).to be_present |
| 44 | + expect(purchase_vendor_row.at_css("a", text: "Deactivate")).to be_present |
34 | 45 | end |
35 | 46 | end |
36 | 47 |
|
|
115 | 126 | end |
116 | 127 |
|
117 | 128 | describe "DELETE #destroy" do |
118 | | - subject { delete vendor_path(id: create(:vendor)) } |
119 | | - it "does not have a route for this" do |
120 | | - subject |
121 | | - expect(response.code).to eq('404') |
| 129 | + let!(:vendor) { create(:vendor, organization: organization) } |
| 130 | + |
| 131 | + subject { delete vendor_path(id: vendor.id) } |
| 132 | + |
| 133 | + context 'when vendor does not have purchase items' do |
| 134 | + it 'shoud delete the vendor' do |
| 135 | + expect { subject }.to change(Vendor, :count) |
| 136 | + expect(response).to redirect_to(vendors_path) |
| 137 | + follow_redirect! |
| 138 | + expect(response.body).to include("#{vendor.business_name} has been removed.") |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context 'when vendor has purchase items' do |
| 143 | + before do |
| 144 | + create(:purchase, :with_items, vendor: vendor) |
| 145 | + end |
| 146 | + |
| 147 | + it 'shoud not delete the vendor' do |
| 148 | + expect { subject }.not_to change(Vendor, :count) |
| 149 | + expect(response).to have_error(/ could not be removed/) |
| 150 | + end |
122 | 151 | end |
123 | 152 | end |
124 | 153 |
|
|
0 commit comments