# Copyright (C) 2024-2025 LibreWeddingPlanner contributors # frozen_string_literal: true require 'rails_helper' module Tables RSpec.describe Distribution do describe '#save!' do around do |example| ActsAsTenant.with_tenant(create(:wedding)) do example.run end end let(:people) { create_list(:guest, 2, status: :invited) } let(:distribution) do described_class.new(min_per_table: 5, max_per_table: 10).tap{|d| d.random_distribution(people)} end context 'when tables_arrangement_id is nil' do it { expect { distribution.save! }.to change { TablesArrangement.count }.by(1) } it { expect { distribution.save! }.to change { Seat.count }.by(2) } end context 'when tables_arrangement_id is set' do before do existing_arrangement = TablesArrangement.create! existing_arrangement.seats.create!(guest: people.first, table_number: 1) distribution.tables_arrangement_id = existing_arrangement.id end it { expect { distribution.save! }.not_to change { TablesArrangement.count } } it { expect { distribution.save! }.to change { Seat.count }.by(1) } end end describe '#random_distribution' do subject(:distribution) { described_class.new(min_per_table: 5, max_per_table: 10) } context 'when there are fewer people than the minimum per table' do it 'creates one table' do distribution.random_distribution([1, 2, 3, 4]) expect(distribution.tables.count).to eq(1) end end context 'when there are more people than the maximum per table' do it 'creates multiple tables' do distribution.random_distribution([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) expect(distribution.tables.count).to be > 1 end end end end end