|
5 | 5 | RSpec.describe ClassMember::Create, type: :unit do |
6 | 6 | let!(:school_class) { create(:school_class, teacher_id: teacher.id, school:) } |
7 | 7 | let(:school) { create(:school) } |
8 | | - let(:student) { create(:student, school:) } |
| 8 | + let(:students) { create_list(:student, 3, school:) } |
9 | 9 | let(:teacher) { create(:teacher, school:) } |
10 | 10 |
|
11 | | - let(:class_member_params) do |
12 | | - { student_id: student.id } |
13 | | - end |
| 11 | + let(:student_ids) { students.map(&:id) } |
14 | 12 |
|
15 | 13 | it 'returns a successful operation response' do |
16 | | - response = described_class.call(school_class:, class_member_params:) |
| 14 | + response = described_class.call(school_class:, students:) |
17 | 15 | expect(response.success?).to be(true) |
18 | 16 | end |
19 | 17 |
|
20 | 18 | it 'creates a school class' do |
21 | | - expect { described_class.call(school_class:, class_member_params:) }.to change(ClassMember, :count).by(1) |
| 19 | + expect { described_class.call(school_class:, students:) }.to change(ClassMember, :count).by(3) |
| 20 | + end |
| 21 | + |
| 22 | + it 'returns a class members JSON array' do |
| 23 | + response = described_class.call(school_class:, students:) |
| 24 | + expect(response[:class_members].size).to eq(3) |
22 | 25 | end |
23 | 26 |
|
24 | | - it 'returns the class member in the operation response' do |
25 | | - response = described_class.call(school_class:, class_member_params:) |
26 | | - expect(response[:class_member]).to be_a(ClassMember) |
| 27 | + it 'returns class members in the operation response' do |
| 28 | + response = described_class.call(school_class:, students:) |
| 29 | + expect(response[:class_members]).to all(be_a(ClassMember)) |
27 | 30 | end |
28 | 31 |
|
29 | 32 | it 'assigns the school_class' do |
30 | | - response = described_class.call(school_class:, class_member_params:) |
31 | | - expect(response[:class_member].school_class).to eq(school_class) |
| 33 | + response = described_class.call(school_class:, students:) |
| 34 | + expect(response[:class_members]).to all(have_attributes(school_class:)) |
32 | 35 | end |
33 | 36 |
|
34 | 37 | it 'assigns the student_id' do |
35 | | - response = described_class.call(school_class:, class_member_params:) |
36 | | - expect(response[:class_member].student_id).to eq(student.id) |
| 38 | + response = described_class.call(school_class:, students:) |
| 39 | + expect(response[:class_members].map(&:student_id)).to match_array(student_ids) |
37 | 40 | end |
38 | 41 |
|
39 | | - context 'when creation fails' do |
40 | | - let(:class_member_params) { {} } |
41 | | - |
| 42 | + context 'when creations fail' do |
42 | 43 | before do |
43 | 44 | allow(Sentry).to receive(:capture_exception) |
44 | 45 | end |
45 | 46 |
|
46 | | - it 'does not create a class member' do |
47 | | - expect { described_class.call(school_class:, class_member_params:) }.not_to change(ClassMember, :count) |
48 | | - end |
| 47 | + context 'with malformed students' do |
| 48 | + let(:students) { nil } |
49 | 49 |
|
50 | | - it 'returns a failed operation response' do |
51 | | - response = described_class.call(school_class:, class_member_params:) |
52 | | - expect(response.failure?).to be(true) |
53 | | - end |
| 50 | + it 'does not create a class member' do |
| 51 | + expect { described_class.call(school_class:, students:) }.not_to change(ClassMember, :count) |
| 52 | + end |
54 | 53 |
|
55 | | - it 'returns the error message in the operation response' do |
56 | | - response = described_class.call(school_class:, class_member_params:) |
57 | | - expect(response[:error]).to match(/Error creating class member/) |
| 54 | + it 'returns a failed operation response' do |
| 55 | + response = described_class.call(school_class:, students:) |
| 56 | + expect(response.failure?).to be(true) |
| 57 | + end |
| 58 | + |
| 59 | + it 'returns the error message in the operation response' do |
| 60 | + response = described_class.call(school_class:, students:) |
| 61 | + expect(response[:error]).to match(/No valid students provided/) |
| 62 | + end |
| 63 | + |
| 64 | + it 'sent the exception to Sentry' do |
| 65 | + described_class.call(school_class:, students:) |
| 66 | + expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError)) |
| 67 | + end |
58 | 68 | end |
59 | 69 |
|
60 | | - it 'sent the exception to Sentry' do |
61 | | - described_class.call(school_class:, class_member_params:) |
62 | | - expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError)) |
| 70 | + context 'with a student from a different school' do |
| 71 | + let(:different_school) { create(:school) } |
| 72 | + let(:different_school_student) { create(:student, school: different_school) } |
| 73 | + |
| 74 | + context 'with non existent students' do |
| 75 | + let(:students) { [different_school_student] } |
| 76 | + |
| 77 | + it 'does not create a class member' do |
| 78 | + expect { described_class.call(school_class:, students:) }.not_to change(ClassMember, :count) |
| 79 | + end |
| 80 | + |
| 81 | + it 'returns a successful operation response' do |
| 82 | + response = described_class.call(school_class:, students:) |
| 83 | + expect(response.success?).to be(true) |
| 84 | + end |
| 85 | + |
| 86 | + it 'returns an empty class members array' do |
| 87 | + response = described_class.call(school_class:, students:) |
| 88 | + expect(response[:class_members]).to eq([]) |
| 89 | + end |
| 90 | + |
| 91 | + it 'returns the error messages in the operation response' do |
| 92 | + response = described_class.call(school_class:, students:) |
| 93 | + expect(response[:errors][different_school_student.id]).to include("Error creating class member for student_id #{different_school_student.id}: Student '#{different_school_student.id}' does not have the 'school-student' role for organisation '#{school.id}'") |
| 94 | + end |
| 95 | + |
| 96 | + it 'sent the exception to Sentry' do |
| 97 | + described_class.call(school_class:, students:) |
| 98 | + expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError)) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + context 'when one creation fails' do |
| 103 | + let(:new_students) { students + [different_school_student] } |
| 104 | + |
| 105 | + it 'returns a successful operation response' do |
| 106 | + response = described_class.call(school_class:, students: new_students) |
| 107 | + expect(response.success?).to be(true) |
| 108 | + end |
| 109 | + |
| 110 | + it 'returns a class members JSON array' do |
| 111 | + response = described_class.call(school_class:, students: new_students) |
| 112 | + expect(response[:class_members].size).to eq(3) |
| 113 | + end |
| 114 | + |
| 115 | + it 'returns class members in the operation response' do |
| 116 | + response = described_class.call(school_class:, students: new_students) |
| 117 | + expect(response[:class_members]).to all(be_a(ClassMember)) |
| 118 | + end |
| 119 | + |
| 120 | + it 'assigns the school_class' do |
| 121 | + response = described_class.call(school_class:, students: new_students) |
| 122 | + expect(response[:class_members]).to all(have_attributes(school_class:)) |
| 123 | + end |
| 124 | + |
| 125 | + it 'assigns the successful students' do |
| 126 | + response = described_class.call(school_class:, students: new_students) |
| 127 | + expect(response[:class_members].map(&:student_id)).to match_array(student_ids) |
| 128 | + end |
| 129 | + |
| 130 | + it 'returns the error messages in the operation response' do |
| 131 | + response = described_class.call(school_class:, students: new_students) |
| 132 | + expect(response[:errors][different_school_student.id]).to eq("Error creating class member for student_id #{different_school_student.id}: Student '#{different_school_student.id}' does not have the 'school-student' role for organisation '#{school.id}'") |
| 133 | + end |
| 134 | + |
| 135 | + it 'sent the exception to Sentry' do |
| 136 | + described_class.call(school_class:, students: new_students) |
| 137 | + expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError)) |
| 138 | + end |
| 139 | + end |
63 | 140 | end |
64 | 141 | end |
65 | 142 | end |
0 commit comments