|
| 1 | +RSpec.describe Member::DetailsController, type: :controller do |
| 2 | + render_views |
| 3 | + let(:member) { Fabricate(:member) } |
| 4 | + |
| 5 | + before do |
| 6 | + allow(controller).to receive(:current_user).and_return(member) |
| 7 | + end |
| 8 | + |
| 9 | + describe 'PATCH #update' do |
| 10 | + context 'with valid params' do |
| 11 | + it 'updates how_you_found_us with checkbox options' do |
| 12 | + patch :update, params: { |
| 13 | + id: member.id, |
| 14 | + member: { |
| 15 | + how_you_found_us: ['Social Media', 'From a friend'], |
| 16 | + newsletter: 'true' |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + member.reload |
| 21 | + expect(member.how_you_found_us).to contain_exactly('Social Media', 'From a friend') |
| 22 | + expect(response).to redirect_to(step2_member_path) |
| 23 | + end |
| 24 | + |
| 25 | + it 'adds other_reason to how_you_found_us when provided' do |
| 26 | + patch :update, params: { |
| 27 | + id: member.id, |
| 28 | + member: { |
| 29 | + how_you_found_us: ['Search engine (Google etc.)'], |
| 30 | + how_you_found_us_other_reason: 'Saw a pamphlet', |
| 31 | + newsletter: 'false' |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + member.reload |
| 36 | + expect(member.how_you_found_us).to contain_exactly('Search engine (Google etc.)', 'Saw a pamphlet') |
| 37 | + expect(response).to redirect_to(step2_member_path) |
| 38 | + end |
| 39 | + |
| 40 | + it 'updates how_you_found_us with only other_reason' do |
| 41 | + patch :update, params: { |
| 42 | + id: member.id, |
| 43 | + member: { |
| 44 | + how_you_found_us: [], |
| 45 | + how_you_found_us_other_reason: 'At a meetup', |
| 46 | + newsletter: 'true' |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + member.reload |
| 51 | + expect(member.how_you_found_us).to eq(['At a meetup']) |
| 52 | + expect(response).to redirect_to(step2_member_path) |
| 53 | + end |
| 54 | + |
| 55 | + it 'removes duplicates and blank entries' do |
| 56 | + patch :update, params: { |
| 57 | + id: member.id, |
| 58 | + member: { |
| 59 | + how_you_found_us: ['From a friend', '', 'From a friend'], |
| 60 | + how_you_found_us_other_reason: 'From a friend', |
| 61 | + newsletter: 'true' |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + member.reload |
| 66 | + expect(member.how_you_found_us).to eq(['From a friend']) |
| 67 | + expect(response).to redirect_to(step2_member_path) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context 'when update fails (invalid data)' do |
| 72 | + it 'renders the edit template' do |
| 73 | + patch :update, params: { |
| 74 | + id: member.id, |
| 75 | + member: { |
| 76 | + how_you_found_us: [] |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + expect(response.body).to include('You must select at least one option') |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments