mastodon/spec/models/collection_spec.rb
David Roetzel 5a7a4fff11
Some checks are pending
Check i18n / check-i18n (push) Waiting to run
Chromatic / Check for relevant changes (push) Waiting to run
Chromatic / Run Chromatic (push) Blocked by required conditions
CodeQL / Analyze (actions) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (ruby) (push) Waiting to run
Check formatting / lint (push) Waiting to run
CSS Linting / lint (push) Waiting to run
JavaScript Linting / lint (push) Waiting to run
Ruby Linting / lint (push) Waiting to run
JavaScript Testing / test (push) Waiting to run
Historical data migration test / test (14-alpine) (push) Waiting to run
Historical data migration test / test (15-alpine) (push) Waiting to run
Historical data migration test / test (16-alpine) (push) Waiting to run
Historical data migration test / test (17-alpine) (push) Waiting to run
Ruby Testing / build (production) (push) Waiting to run
Ruby Testing / build (test) (push) Waiting to run
Ruby Testing / test (.ruby-version) (push) Blocked by required conditions
Ruby Testing / test (3.2) (push) Blocked by required conditions
Ruby Testing / test (3.3) (push) Blocked by required conditions
Ruby Testing / ImageMagick tests (.ruby-version) (push) Blocked by required conditions
Ruby Testing / ImageMagick tests (3.2) (push) Blocked by required conditions
Ruby Testing / ImageMagick tests (3.3) (push) Blocked by required conditions
Ruby Testing / End to End testing (.ruby-version) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.2) (push) Blocked by required conditions
Ruby Testing / End to End testing (3.3) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.10.2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.2, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.13) (push) Blocked by required conditions
First draft of Collection update API (#37110)
2025-12-04 10:00:21 +00:00

130 lines
3.4 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Collection do
describe 'Validations' do
subject { Fabricate.build :collection }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:description) }
it { is_expected.to_not allow_value(nil).for(:local) }
it { is_expected.to_not allow_value(nil).for(:sensitive) }
it { is_expected.to_not allow_value(nil).for(:discoverable) }
context 'when collection is remote' do
subject { Fabricate.build :collection, local: false }
it { is_expected.to validate_presence_of(:uri) }
it { is_expected.to validate_presence_of(:original_number_of_items) }
end
context 'when using a hashtag as category' do
subject { Fabricate.build(:collection, tag:) }
context 'when hashtag is usable' do
let(:tag) { Fabricate.build(:tag) }
it { is_expected.to be_valid }
end
context 'when hashtag is not usable' do
let(:tag) { Fabricate.build(:tag, usable: false) }
it { is_expected.to_not be_valid }
end
end
context 'when there are more items than allowed' do
subject { Fabricate.build(:collection, collection_items:) }
let(:collection_items) { Fabricate.build_times(described_class::MAX_ITEMS + 1, :collection_item, collection: nil) }
it { is_expected.to_not be_valid }
end
end
describe '#item_for' do
subject { Fabricate(:collection) }
let!(:items) { Fabricate.times(2, :collection_item, collection: subject) }
context 'when given no account' do
it 'returns all items' do
expect(subject.items_for).to match_array(items)
end
end
context 'when given an account' do
let(:account) { Fabricate(:account) }
before do
account.block!(items.first.account)
end
it 'does not return items blocked by this account' do
expect(subject.items_for(account)).to contain_exactly(items.last)
end
end
end
describe '#tag_name=' do
context 'when the collection is new and has no tag' do
subject { Fabricate.build(:collection) }
context 'when the tag exists' do
let!(:tag) { Fabricate.create(:tag, name: 'people') }
it 'correctly assigns the existing tag' do
subject.tag_name = '#people'
expect(subject.tag).to eq tag
end
end
context 'when the tag does not exist' do
it 'creates and assigns a new tag' do
expect do
subject.tag_name = '#people'
end.to change(Tag, :count).by(1)
expect(subject.tag).to be_present
expect(subject.tag.name).to eq 'people'
end
end
end
context 'when the collection is persisted and has a tag' do
subject { Fabricate(:collection, tag:) }
let!(:tag) { Fabricate(:tag, name: 'people') }
context 'when the new tag is the same' do
it 'does not change the object' do
subject.tag_name = '#People'
expect(subject.tag).to eq tag
expect(subject).to_not be_changed
end
end
context 'when the new tag is different' do
it 'creates and assigns a new tag' do
expect do
subject.tag_name = '#bots'
end.to change(Tag, :count).by(1)
expect(subject.tag).to be_present
expect(subject.tag.name).to eq 'bots'
expect(subject).to be_changed
end
end
end
end
end