require 'rails_helper' module Mks module Rate RSpec.describe ServiceBundleService do before(:each) do @service = ServiceBundleService.new end describe '#add_services' do it 'adds new services' do bundle = create(:service_bundle) s1 = create(:chargeable_service) s2 = create(:chargeable_service) s3 = create(:chargeable_service) expect(bundle.chargeable_services.count).to eq 0 @service.add_services(bundle, [s1.id, s2.id, s3.id]) bundle.reload expect(bundle.chargeable_services.count).to eq 3 end it 'removes non selected services' do bundle = create(:service_bundle) s1 = create(:chargeable_service) s2 = create(:chargeable_service) s3 = create(:chargeable_service) bundle.chargeable_services << [s1, s2, s3] expect(bundle.chargeable_services.count).to eq 3 @service.add_services(bundle, [s3.id]) bundle.reload expect(bundle.chargeable_services.count).to eq 1 expect(bundle.chargeable_services[0].id).to eq s3.id end it 'adds newly selected and removes non selected' do bundle = create(:service_bundle) s1 = create(:chargeable_service) s2 = create(:chargeable_service) s3 = create(:chargeable_service) s4 = create(:chargeable_service) bundle.chargeable_services << [s1, s2] expect(bundle.chargeable_services.count).to eq 2 @service.add_services(bundle, [s2.id, s3.id, s4.id]) bundle.reload expect(bundle.chargeable_services.count).to eq 3 expect(bundle.chargeable_services).not_to include s1 end end end end end