|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Lesson::CreateBatch, type: :unit do |
| 6 | + let(:school) { create(:school) } |
| 7 | + let(:teacher) { create(:teacher, school:) } |
| 8 | + |
| 9 | + let(:lessons_params) do |
| 10 | + [ |
| 11 | + { |
| 12 | + name: 'Test Lesson', |
| 13 | + user_id: teacher.id, |
| 14 | + school_id: school.id, |
| 15 | + origin_identifier: 'test-lesson-identifier-one', |
| 16 | + project_attributes: { |
| 17 | + name: 'Hello world project', |
| 18 | + project_type: Project::Types::PYTHON, |
| 19 | + components: [ |
| 20 | + { name: 'main.py', extension: 'py', content: 'print("Hello, world!")' } |
| 21 | + ] |
| 22 | + } |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'Test Lesson 2', |
| 26 | + user_id: teacher.id, |
| 27 | + school_id: school.id, |
| 28 | + origin_identifier: 'test-lesson-identifier-two', |
| 29 | + project_attributes: { |
| 30 | + name: 'Hello world project', |
| 31 | + project_type: Project::Types::PYTHON, |
| 32 | + components: [ |
| 33 | + { name: 'main.py', extension: 'py', content: 'print("Hello, world!")' } |
| 34 | + ] |
| 35 | + } |
| 36 | + } |
| 37 | + ] |
| 38 | + end |
| 39 | + |
| 40 | + context 'with a teacher' do |
| 41 | + let(:result) { described_class.call(lessons_params:) } |
| 42 | + |
| 43 | + before do |
| 44 | + allow(User).to receive(:from_userinfo).with(ids: teacher.id).and_return([teacher]) |
| 45 | + end |
| 46 | + |
| 47 | + it 'returns a successful operation response for the first lesson' do |
| 48 | + expect(result.first.success?).to be(true) |
| 49 | + end |
| 50 | + |
| 51 | + it 'returns a successful operation response for the second lesson' do |
| 52 | + expect(result.second.success?).to be(true) |
| 53 | + end |
| 54 | + |
| 55 | + it 'creates multiple lessons' do |
| 56 | + expect { described_class.call(lessons_params:) }.to change(Lesson, :count).by(2) |
| 57 | + end |
| 58 | + |
| 59 | + it 'does not pass origin_identifier to lesson creation' do |
| 60 | + received_params = [] |
| 61 | + allow(Lesson::Create).to receive(:call).and_wrap_original do |method, lesson_params:| |
| 62 | + received_params << lesson_params |
| 63 | + method.call(lesson_params:) |
| 64 | + end |
| 65 | + |
| 66 | + described_class.call(lessons_params:) |
| 67 | + |
| 68 | + expect(received_params).to all(satisfy { |params| !params.key?(:origin_identifier) }) |
| 69 | + end |
| 70 | + |
| 71 | + it 'appends the origin_identifier to the first created lesson' do |
| 72 | + expect(result.first[:origin_identifier]).to eq('test-lesson-identifier-one') |
| 73 | + end |
| 74 | + |
| 75 | + it 'appends the origin_identifier to the second created lesson' do |
| 76 | + expect(result.second[:origin_identifier]).to eq('test-lesson-identifier-two') |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments