FIX: Serialize an empty array if no suggested topics exist (#10134)

It used to return nil, which was ambiguous (empty vs absent
result).
This commit is contained in:
Dan Ungureanu 2020-06-26 22:25:38 +03:00 committed by GitHub
parent cb898a8023
commit 4efc126635
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -7,11 +7,11 @@ module SuggestedTopicsMixin
end end
def include_related_messages? def include_related_messages?
object.next_page.nil? && object.related_messages&.topics.present? object.next_page.nil? && object.related_messages&.topics
end end
def include_suggested_topics? def include_suggested_topics?
object.next_page.nil? && object.suggested_topics&.topics.present? object.next_page.nil? && object.suggested_topics&.topics
end end
def related_messages def related_messages

View File

@ -118,6 +118,40 @@ describe TopicViewSerializer do
expect(json[:suggested_topics]).to eq(nil) expect(json[:suggested_topics]).to eq(nil)
end end
end end
describe 'with private messages' do
let!(:topic) do
Fabricate(:private_message_topic,
highest_post_number: 1,
topic_allowed_users: [
Fabricate.build(:topic_allowed_user, user: user)
]
)
end
let!(:topic2) do
Fabricate(:private_message_topic,
highest_post_number: 1,
topic_allowed_users: [
Fabricate.build(:topic_allowed_user, user: user)
]
)
end
it 'includes suggested topics' do
TopicUser.change(user, topic2.id, notification_level: TopicUser.notification_levels[:tracking])
json = serialize_topic(topic, user)
expect(json[:suggested_topics].map { |t| t[:id] }).to contain_exactly(topic2.id)
end
it 'does not include suggested topics if all PMs are read' do
TopicUser.update_last_read(user, topic2.id, 1, 1, 0)
json = serialize_topic(topic, user)
expect(json[:suggested_topics]).to eq([])
end
end
end end
describe 'when tags added to private message topics' do describe 'when tags added to private message topics' do