2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
2016-05-16 05:31:39 -04:00
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe TopicList do
|
2017-02-14 16:29:06 -05:00
|
|
|
let!(:topic) do
|
|
|
|
t = Fabricate(:topic)
|
|
|
|
t.allowed_user_ids = [t.user.id]
|
|
|
|
t
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2017-02-14 16:29:06 -05:00
|
|
|
|
2016-05-16 05:31:39 -04:00
|
|
|
let(:user) { topic.user }
|
|
|
|
let(:topic_list) { TopicList.new("liked", user, [topic]) }
|
|
|
|
|
2016-06-09 10:01:48 -04:00
|
|
|
before { TopicList.preloaded_custom_fields.clear }
|
|
|
|
|
2016-05-16 05:31:39 -04:00
|
|
|
after { TopicList.preloaded_custom_fields.clear }
|
|
|
|
|
|
|
|
describe ".preloaded_custom_fields" do
|
|
|
|
it "should return a unique set of values" do
|
|
|
|
TopicList.preloaded_custom_fields << "test"
|
|
|
|
TopicList.preloaded_custom_fields << "test"
|
|
|
|
TopicList.preloaded_custom_fields << "apple"
|
|
|
|
|
|
|
|
expect(TopicList.preloaded_custom_fields).to eq(Set.new(%w[test apple]))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "preload" do
|
2017-02-14 16:29:06 -05:00
|
|
|
it "allows preloading of data" do
|
|
|
|
preloaded_topic = false
|
2017-02-15 12:04:02 -05:00
|
|
|
preloader =
|
|
|
|
lambda do |topics, topic_list|
|
|
|
|
expect(TopicList === topic_list).to eq(true)
|
2017-02-14 16:29:06 -05:00
|
|
|
expect(topics.length).to eq(1)
|
|
|
|
preloaded_topic = true
|
|
|
|
end
|
|
|
|
|
2017-02-14 16:32:33 -05:00
|
|
|
TopicList.on_preload(&preloader)
|
2017-02-14 16:29:06 -05:00
|
|
|
|
|
|
|
topic_list.topics
|
|
|
|
expect(preloaded_topic).to eq(true)
|
|
|
|
|
2017-02-14 16:32:33 -05:00
|
|
|
TopicList.cancel_preload(&preloader)
|
2017-02-14 16:29:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-11 04:26:04 -04:00
|
|
|
describe "#load_topics" do
|
|
|
|
it "loads additional data for serialization" do
|
2021-12-28 17:19:39 -05:00
|
|
|
category_user =
|
|
|
|
CategoryUser.create!(
|
|
|
|
user: user,
|
|
|
|
category: topic.category,
|
|
|
|
notification_level: NotificationLevels.all[:regular],
|
|
|
|
)
|
2020-09-11 04:26:04 -04:00
|
|
|
|
|
|
|
topic = topic_list.load_topics.first
|
|
|
|
|
|
|
|
expect(topic.category_user_data).to eq(category_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-12 15:26:13 -05:00
|
|
|
describe "#top_tags" do
|
2016-07-07 09:17:56 -04:00
|
|
|
it "should return the right tags" do
|
2016-07-08 17:13:32 -04:00
|
|
|
tag = Fabricate(:tag, topics: [topic])
|
|
|
|
other_tag = Fabricate(:tag, topics: [topic], name: "use-anywhere")
|
2016-07-07 09:17:56 -04:00
|
|
|
output = [tag.name, other_tag.name]
|
2018-01-12 15:26:13 -05:00
|
|
|
expect(topic_list.top_tags.sort).to eq(output.sort)
|
2016-07-07 09:17:56 -04:00
|
|
|
end
|
|
|
|
|
2016-07-08 17:13:32 -04:00
|
|
|
describe "when there are tags restricted to a category" do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:category)
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:other_topic) { Fabricate(:topic) } # uncategorized
|
|
|
|
fab!(:tag) { Fabricate(:tag, topics: [topic], categories: [category], name: "category-tag") }
|
|
|
|
fab!(:other_tag) { Fabricate(:tag, topics: [topic], name: "use-anywhere") }
|
2016-07-07 09:17:56 -04:00
|
|
|
let(:topic_list) do
|
|
|
|
TopicList.new(
|
|
|
|
"latest",
|
|
|
|
topic.user,
|
|
|
|
[topic],
|
|
|
|
category: category.id,
|
|
|
|
category_id: category.id,
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2016-07-07 09:17:56 -04:00
|
|
|
|
2018-02-08 16:36:39 -05:00
|
|
|
it "should return tags used in the category" do
|
|
|
|
expect(topic_list.top_tags).to eq([tag.name, other_tag.name].sort)
|
2016-07-08 17:13:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "with no category, should return all tags" do
|
2018-01-12 15:26:13 -05:00
|
|
|
expect(TopicList.new("latest", other_topic.user, [other_topic]).top_tags.sort).to eq(
|
|
|
|
[tag.name, other_tag.name].sort,
|
|
|
|
)
|
2016-07-08 17:13:32 -04:00
|
|
|
end
|
2016-07-07 09:17:56 -04:00
|
|
|
|
2016-09-22 15:23:10 -04:00
|
|
|
it "with another category with no tags, should return no tags" do
|
2016-07-08 17:13:32 -04:00
|
|
|
other_category = Fabricate(:category)
|
|
|
|
topic3 = Fabricate(:topic, category: other_category)
|
|
|
|
list =
|
|
|
|
TopicList.new(
|
|
|
|
"latest",
|
|
|
|
topic3.user,
|
|
|
|
[topic3],
|
|
|
|
category: other_category.id,
|
|
|
|
category_id: other_category.id,
|
|
|
|
)
|
2018-01-12 15:26:13 -05:00
|
|
|
expect(list.top_tags).to be_empty
|
2016-07-07 09:17:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-12-11 07:20:48 -05:00
|
|
|
|
|
|
|
describe "#preload_key" do
|
|
|
|
let(:category) { Fabricate(:category) }
|
2021-03-11 18:06:21 -05:00
|
|
|
let(:tag) { Fabricate(:tag) }
|
2020-12-11 07:20:48 -05:00
|
|
|
|
2022-08-24 06:54:01 -04:00
|
|
|
it "returns topic_list" do
|
2020-12-11 07:20:48 -05:00
|
|
|
topic_list = TopicList.new("latest", nil, nil, category: category, category_id: category.id)
|
2022-08-24 06:54:01 -04:00
|
|
|
expect(topic_list.preload_key).to eq("topic_list")
|
2021-03-24 10:24:29 -04:00
|
|
|
end
|
2020-12-11 07:20:48 -05:00
|
|
|
end
|
2016-05-16 05:31:39 -04:00
|
|
|
end
|