|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe InvitationManager do |
| 4 | + subject(:manager) { InvitationManager.new } |
| 5 | + |
| 6 | + let(:chapter) { Fabricate(:chapter) } |
| 7 | + let(:member_in_both_groups) { Fabricate(:member, accepted_toc_at: Time.zone.now) } |
| 8 | + |
| 9 | + describe '#chapter_students' do |
| 10 | + context 'when a member has multiple subscriptions to the same group type' do |
| 11 | + before do |
| 12 | + students_group1 = Fabricate(:group, name: 'Students', chapter: chapter) |
| 13 | + students_group2 = Fabricate(:group, name: 'Students', chapter: chapter) |
| 14 | + students_group1.members << member_in_both_groups |
| 15 | + students_group2.members << member_in_both_groups |
| 16 | + end |
| 17 | + |
| 18 | + it 'returns unique members' do |
| 19 | + result = manager.send(:chapter_students, chapter) |
| 20 | + |
| 21 | + expect(result.count).to eq(1) |
| 22 | + expect(result).to contain_exactly(member_in_both_groups) |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe '#chapter_coaches' do |
| 28 | + context 'when a member has multiple subscriptions to the same group type' do |
| 29 | + before do |
| 30 | + coaches_group1 = Fabricate(:group, name: 'Coaches', chapter: chapter) |
| 31 | + coaches_group2 = Fabricate(:group, name: 'Coaches', chapter: chapter) |
| 32 | + coaches_group1.members << member_in_both_groups |
| 33 | + coaches_group2.members << member_in_both_groups |
| 34 | + end |
| 35 | + |
| 36 | + it 'returns unique members' do |
| 37 | + result = manager.send(:chapter_coaches, chapter) |
| 38 | + |
| 39 | + expect(result.count).to eq(1) |
| 40 | + expect(result).to contain_exactly(member_in_both_groups) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + describe 'sending invitations to members in both students and coaches groups' do |
| 46 | + let(:workshop) { Fabricate(:workshop, chapter: chapter) } |
| 47 | + let(:students_group) { Fabricate(:group, name: 'Students', chapter: chapter) } |
| 48 | + let(:coaches_group) { Fabricate(:group, name: 'Coaches', chapter: chapter) } |
| 49 | + |
| 50 | + before do |
| 51 | + students_group.members << member_in_both_groups |
| 52 | + coaches_group.members << member_in_both_groups |
| 53 | + end |
| 54 | + |
| 55 | + it 'sends one invitation per role when audience is everyone' do |
| 56 | + expect(WorkshopInvitation).to receive(:find_or_create_by) |
| 57 | + .with(workshop: workshop, member: member_in_both_groups, role: 'Student') |
| 58 | + .and_call_original |
| 59 | + .once |
| 60 | + |
| 61 | + expect(WorkshopInvitation).to receive(:find_or_create_by) |
| 62 | + .with(workshop: workshop, member: member_in_both_groups, role: 'Coach') |
| 63 | + .and_call_original |
| 64 | + .once |
| 65 | + |
| 66 | + manager.send_workshop_emails(workshop, 'everyone') |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments