|
1 | 1 | RSpec.describe "DonationSites", type: :request do |
2 | 2 | let(:organization) { create(:organization) } |
3 | 3 | let(:user) { create(:user, organization: organization) } |
| 4 | + let(:params) { {} } |
4 | 5 |
|
5 | 6 | describe "while signed in" do |
6 | 7 | before do |
7 | 8 | sign_in(user) |
8 | 9 | end |
9 | 10 |
|
10 | 11 | describe "GET #index" do |
| 12 | + let!(:active_donation_site) { create(:donation_site, organization: organization, name: "An Active Site") } |
| 13 | + let!(:inactive_donation_site) { create(:donation_site, organization: organization, active: false, name: "An Inactive Site") } |
| 14 | + |
11 | 15 | subject do |
12 | | - get donation_sites_path(format: response_format) |
| 16 | + get donation_sites_path(**params) |
13 | 17 | response |
14 | 18 | end |
15 | 19 |
|
16 | | - before do |
17 | | - create(:donation_site) |
| 20 | + it { is_expected.to be_successful } |
| 21 | + |
| 22 | + it "should show active donation sites with deactivate buttons" do |
| 23 | + get donation_sites_path |
| 24 | + page = Nokogiri::HTML(response.body) |
| 25 | + expect(response.body).to include("An Active Site") |
| 26 | + expect(response.body).not_to include("An Inactive Site") |
| 27 | + button1 = page.css(".btn[href='/donation_sites/#{active_donation_site.id}/deactivate']") |
| 28 | + expect(button1.text.strip).to eq("Deactivate") |
| 29 | + expect(button1.attr('class')).not_to match(/disabled/) |
18 | 30 | end |
19 | 31 |
|
20 | | - context "html" do |
21 | | - let(:response_format) { 'html' } |
| 32 | + context "with include donation sites checkbox selected" do |
| 33 | + let(:params) { { include_inactive_donation_sites: "1" } } |
22 | 34 |
|
23 | | - it { is_expected.to be_successful } |
| 35 | + it "should show active donation sites with deactivate buttons" do |
| 36 | + get donation_sites_path(params) |
| 37 | + page = Nokogiri::HTML(response.body) |
| 38 | + expect(response.body).to include("An Active Site") |
| 39 | + expect(response.body).to include("An Inactive Site") |
| 40 | + |
| 41 | + # Active donation site should have deactivate button |
| 42 | + button1 = page.css(".btn[href='/donation_sites/#{active_donation_site.id}/deactivate']") |
| 43 | + expect(button1.text.strip).to eq("Deactivate") |
| 44 | + expect(button1.attr('class')).not_to match(/disabled/) |
| 45 | + |
| 46 | + # Inactive donation site should have reactivate button |
| 47 | + button2 = page.css(".btn[href='/donation_sites/#{inactive_donation_site.id}/reactivate']") |
| 48 | + expect(button2.text.strip).to eq("Restore") |
| 49 | + expect(button2.attr('class')).not_to match(/disabled/) |
| 50 | + end |
24 | 51 | end |
25 | 52 |
|
26 | 53 | context "csv" do |
27 | | - let(:response_format) { 'csv' } |
| 54 | + subject do |
| 55 | + get donation_sites_path(format: :csv) |
| 56 | + response |
| 57 | + end |
| 58 | + |
| 59 | + let(:expected_headers) { ["Name", "Address", "Contact Name", "Email", "Phone"] } |
28 | 60 |
|
29 | 61 | it { is_expected.to be_successful } |
30 | | - end |
31 | | - end |
32 | | - describe 'GET #index' do |
33 | | - let!(:active_donation_site) { create(:donation_site, organization: organization, name: "An Active Site") } |
34 | | - let!(:inactive_donation_site) { create(:donation_site, organization: organization, active: false, name: "An Inactive Site") } |
35 | 62 |
|
36 | | - it "should show all/only active donation sites with deactivate buttons" do |
37 | | - get donation_sites_path |
38 | | - page = Nokogiri::HTML(response.body) |
39 | | - expect(response.body).to include("An Active Site") |
40 | | - expect(response.body).not_to include("An Inactive Site") |
41 | | - button1 = page.css(".btn[href='/donation_sites/#{active_donation_site.id}/deactivate']") |
42 | | - expect(button1.text.strip).to eq("Deactivate") |
43 | | - expect(button1.attr('class')).not_to match(/disabled/) |
| 63 | + it "has the expected csv headers" do |
| 64 | + subject |
| 65 | + |
| 66 | + csv = CSV.parse(response.body) |
| 67 | + expect(csv[0]).to eq(expected_headers) |
| 68 | + end |
| 69 | + |
| 70 | + it "only includes active donation sites by default" do |
| 71 | + subject |
| 72 | + |
| 73 | + csv = CSV.parse(response.body) |
| 74 | + |
| 75 | + # 1 row of headers + 1 row for active site |
| 76 | + expect(csv.length).to eq(2) |
| 77 | + |
| 78 | + # Expect active site to be present by name |
| 79 | + expect(csv[1][0]).to eq("An Active Site") |
| 80 | + end |
| 81 | + |
| 82 | + context "with include inactive donation sites selected" do |
| 83 | + subject do |
| 84 | + get donation_sites_path(include_inactive_donation_sites: "1", format: :csv) |
| 85 | + response |
| 86 | + end |
| 87 | + |
| 88 | + it "includes active and inactive donation sites" do |
| 89 | + subject |
| 90 | + |
| 91 | + csv = CSV.parse(response.body) |
| 92 | + |
| 93 | + # 1 row of headers + 1 row for active site + 1 row for inactive site |
| 94 | + expect(csv.length).to eq(3) |
| 95 | + |
| 96 | + csv_rows = csv[1..] |
| 97 | + donation_site_names = csv_rows.map(&:first) |
| 98 | + |
| 99 | + expect(donation_site_names).to include("An Active Site") |
| 100 | + expect(donation_site_names).to include("An Inactive Site") |
| 101 | + end |
| 102 | + end |
44 | 103 | end |
45 | 104 | end |
46 | 105 |
|
47 | | - describe 'DELETE #deactivate' do |
| 106 | + describe 'PUT #deactivate' do |
48 | 107 | it 'should be able to deactivate an item' do |
49 | 108 | donation_site = create(:donation_site, organization: organization, active: true, name: "to be deactivated") |
50 | 109 | params = { id: donation_site.id } |
51 | 110 |
|
52 | | - expect { delete deactivate_donation_site_path(params) }.to change { donation_site.reload.active }.from(true).to(false) |
| 111 | + expect { put deactivate_donation_site_path(params) }.to change { donation_site.reload.active }.from(true).to(false) |
| 112 | + expect(response).to redirect_to(donation_sites_path) |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + describe 'PUT #reactivate' do |
| 117 | + it 'should be able to reactivate an item' do |
| 118 | + donation_site = create(:donation_site, organization:, active: false, name: "to be reactivated") |
| 119 | + params = { id: donation_site.id } |
| 120 | + |
| 121 | + expect { put reactivate_donation_site_path(params) }.to change { donation_site.reload.active }.from(false).to(true) |
53 | 122 | expect(response).to redirect_to(donation_sites_path) |
54 | 123 | end |
55 | 124 | end |
|
0 commit comments