|
| 1 | +RSpec.describe Member::DetailsController do |
| 2 | + render_views |
| 3 | + let(:member) { Fabricate(:member) } |
| 4 | + |
| 5 | + before do |
| 6 | + allow(controller).to receive(:current_user).and_return(member) |
| 7 | + allow_any_instance_of(Services::MailingList).to receive(:subscribe).and_return(true) |
| 8 | + end |
| 9 | + |
| 10 | + describe 'PATCH #update' do |
| 11 | + context 'with valid params' do |
| 12 | + it 'updates how_you_found_us with radio option' do |
| 13 | + patch :update, params: { |
| 14 | + id: member.id, |
| 15 | + member: { |
| 16 | + how_you_found_us: 'social_media', |
| 17 | + newsletter: 'true' |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + member.reload |
| 22 | + expect(I18n.t("member.details.edit.how_you_found_us_options.#{member.how_you_found_us}")).to eq('Social media') |
| 23 | + expect(member.how_you_found_us_other_reason).to eq(nil) |
| 24 | + expect(response).to redirect_to(step2_member_path) |
| 25 | + end |
| 26 | + |
| 27 | + it 'adds other_reason to how_you_found_us when provided' do |
| 28 | + patch :update, params: { |
| 29 | + id: member.id, |
| 30 | + member: { |
| 31 | + how_you_found_us: 'other', |
| 32 | + how_you_found_us_other_reason: 'Saw a pamphlet', |
| 33 | + newsletter: 'false' |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + member.reload |
| 38 | + expect(member.how_you_found_us).to eq('other') |
| 39 | + expect(member.how_you_found_us_other_reason).to eq('Saw a pamphlet') |
| 40 | + expect(response).to redirect_to(step2_member_path) |
| 41 | + end |
| 42 | + |
| 43 | + it 'updates how_you_found_us with only other_reason' do |
| 44 | + patch :update, params: { |
| 45 | + id: member.id, |
| 46 | + member: { |
| 47 | + how_you_found_us: 'other', |
| 48 | + how_you_found_us_other_reason: 'At a meetup', |
| 49 | + newsletter: 'true' |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + member.reload |
| 54 | + expect(member.how_you_found_us).to eq('other') |
| 55 | + expect(member.how_you_found_us_other_reason).to eq('At a meetup') |
| 56 | + expect(response).to redirect_to(step2_member_path) |
| 57 | + end |
| 58 | + |
| 59 | + it 'removes duplicates and blank entries' do |
| 60 | + patch :update, params: { |
| 61 | + id: member.id, |
| 62 | + member: { |
| 63 | + how_you_found_us: 'other', |
| 64 | + how_you_found_us_other_reason: 'From a colleague', |
| 65 | + newsletter: 'true' |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + member.reload |
| 70 | + expect(member.how_you_found_us).to eq('other') |
| 71 | + expect(member.how_you_found_us_other_reason).to eq('From a colleague') |
| 72 | + expect(response).to redirect_to(step2_member_path) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context 'when update fails (invalid data)' do |
| 77 | + it 'error raised when no how you found us selection given' do |
| 78 | + patch :update, params: { |
| 79 | + id: member.id, |
| 80 | + member: { |
| 81 | + how_you_found_us: 'other', |
| 82 | + how_you_found_us_other_reason: nil, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + expect(response.body).to include('You must select one option') |
| 87 | + end |
| 88 | + |
| 89 | + it 'error raised when both how you found us fields popoulated' do |
| 90 | + patch :update, params: { |
| 91 | + id: member.id, |
| 92 | + member: { |
| 93 | + how_you_found_us: 'from_a_friend', |
| 94 | + how_you_found_us_other_reason: 'something else', |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + expect(response.body).to include('You must select one option') |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
0 commit comments