0
0

Combine assertions in Notification model spec (#32015)

This commit is contained in:
Matt Jankowski 2024-09-23 06:45:34 -04:00 committed by GitHub
parent 2b4bda8004
commit bbf7752256
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -252,88 +252,83 @@ RSpec.describe Notification do
] ]
end end
context 'with a preloaded target status' do context 'with a preloaded target status and a cached status' do
it 'preloads mention' do it 'preloads association records and replaces association records' do
expect(subject[0].type).to eq :mention expect(subject)
expect(subject[0].association(:mention)).to be_loaded .to contain_exactly(
expect(subject[0].mention.association(:status)).to be_loaded mention_attributes,
status_attributes,
reblog_attributes,
follow_attributes,
follow_request_attributes,
favourite_attributes,
poll_attributes
)
end end
it 'preloads status' do def mention_attributes
expect(subject[1].type).to eq :status have_attributes(
expect(subject[1].association(:status)).to be_loaded type: :mention,
target_status: eq(mention.status).and(have_loaded_association(:account)),
mention: have_loaded_association(:status)
).and(have_loaded_association(:mention))
end end
it 'preloads reblog' do def status_attributes
expect(subject[2].type).to eq :reblog have_attributes(
expect(subject[2].association(:status)).to be_loaded type: :status,
expect(subject[2].status.association(:reblog)).to be_loaded target_status: eq(status).and(have_loaded_association(:account))
).and(have_loaded_association(:status))
end end
it 'preloads follow as nil' do def reblog_attributes
expect(subject[3].type).to eq :follow have_attributes(
expect(subject[3].target_status).to be_nil type: :reblog,
status: have_loaded_association(:reblog),
target_status: eq(reblog.reblog).and(have_loaded_association(:account))
).and(have_loaded_association(:status))
end end
it 'preloads follow_request as nill' do def follow_attributes
expect(subject[4].type).to eq :follow_request have_attributes(
expect(subject[4].target_status).to be_nil type: :follow,
target_status: be_nil
)
end end
it 'preloads favourite' do def follow_request_attributes
expect(subject[5].type).to eq :favourite have_attributes(
expect(subject[5].association(:favourite)).to be_loaded type: :follow_request,
expect(subject[5].favourite.association(:status)).to be_loaded target_status: be_nil
)
end end
it 'preloads poll' do def favourite_attributes
expect(subject[6].type).to eq :poll have_attributes(
expect(subject[6].association(:poll)).to be_loaded type: :favourite,
expect(subject[6].poll.association(:status)).to be_loaded favourite: have_loaded_association(:status),
end target_status: eq(favourite.status).and(have_loaded_association(:account))
end ).and(have_loaded_association(:favourite))
context 'with a cached status' do
it 'replaces mention' do
expect(subject[0].type).to eq :mention
expect(subject[0].target_status.association(:account)).to be_loaded
expect(subject[0].target_status).to eq mention.status
end end
it 'replaces status' do def poll_attributes
expect(subject[1].type).to eq :status have_attributes(
expect(subject[1].target_status.association(:account)).to be_loaded type: :poll,
expect(subject[1].target_status).to eq status poll: have_loaded_association(:status),
end target_status: eq(poll.status).and(have_loaded_association(:account))
).and(have_loaded_association(:poll))
it 'replaces reblog' do
expect(subject[2].type).to eq :reblog
expect(subject[2].target_status.association(:account)).to be_loaded
expect(subject[2].target_status).to eq reblog.reblog
end
it 'replaces follow' do
expect(subject[3].type).to eq :follow
expect(subject[3].target_status).to be_nil
end
it 'replaces follow_request' do
expect(subject[4].type).to eq :follow_request
expect(subject[4].target_status).to be_nil
end
it 'replaces favourite' do
expect(subject[5].type).to eq :favourite
expect(subject[5].target_status.association(:account)).to be_loaded
expect(subject[5].target_status).to eq favourite.status
end
it 'replaces poll' do
expect(subject[6].type).to eq :poll
expect(subject[6].target_status.association(:account)).to be_loaded
expect(subject[6].target_status).to eq poll.status
end end
end end
end end
end end
end end
RSpec::Matchers.define :have_loaded_association do |association|
match do |record|
record.association(association).loaded?
end
failure_message do |record|
"expected #{record} to have loaded association #{association} but it did not."
end
end