2016-05-16 04:31:39 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe TopicList do
|
2017-02-14 15:29:06 -06:00
|
|
|
let!(:topic) {
|
|
|
|
t = Fabricate(:topic)
|
|
|
|
t.allowed_user_ids = [t.user.id]
|
|
|
|
t
|
|
|
|
}
|
|
|
|
|
2016-05-16 04:31:39 -05:00
|
|
|
let(:user) { topic.user }
|
|
|
|
let(:topic_list) { TopicList.new("liked", user, [topic]) }
|
|
|
|
|
2016-06-09 09:01:48 -05:00
|
|
|
before do
|
|
|
|
TopicList.preloaded_custom_fields.clear
|
|
|
|
end
|
|
|
|
|
2016-05-16 04:31:39 -05:00
|
|
|
after do
|
|
|
|
TopicList.preloaded_custom_fields.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-02-14 15:29:06 -06:00
|
|
|
context "preload" do
|
|
|
|
it "allows preloading of data" do
|
|
|
|
preloaded_topic = false
|
|
|
|
preloader = lambda do |topics|
|
|
|
|
expect(topics.length).to eq(1)
|
|
|
|
preloaded_topic = true
|
|
|
|
end
|
|
|
|
|
2017-02-14 15:32:33 -06:00
|
|
|
TopicList.on_preload(&preloader)
|
2017-02-14 15:29:06 -06:00
|
|
|
|
|
|
|
topic_list.topics
|
|
|
|
expect(preloaded_topic).to eq(true)
|
|
|
|
|
2017-02-14 15:32:33 -06:00
|
|
|
TopicList.cancel_preload(&preloader)
|
2017-02-14 15:29:06 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-16 04:31:39 -05:00
|
|
|
context "DiscourseTagging enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should add tags to preloaded custom fields" do
|
2016-12-09 15:08:17 -06:00
|
|
|
expect(topic_list.preloaded_custom_fields).to include(DiscourseTagging::TAGS_FIELD_NAME)
|
2016-05-16 04:31:39 -05:00
|
|
|
end
|
|
|
|
end
|
2016-07-07 08:17:56 -05:00
|
|
|
|
|
|
|
describe '#tags' do
|
|
|
|
it 'should return the right tags' do
|
2016-07-08 16:13:32 -05:00
|
|
|
tag = Fabricate(:tag, topics: [topic])
|
|
|
|
other_tag = Fabricate(:tag, topics: [topic], name: "use-anywhere")
|
2016-07-07 08:17:56 -05:00
|
|
|
output = [tag.name, other_tag.name]
|
|
|
|
expect(topic_list.tags.sort).to eq(output.sort)
|
|
|
|
end
|
|
|
|
|
2016-07-08 16:13:32 -05:00
|
|
|
describe 'when there are tags restricted to a category' do
|
|
|
|
let!(:category) { Fabricate(:category) }
|
|
|
|
let!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
let!(:other_topic) { Fabricate(:topic) } # uncategorized
|
|
|
|
let!(:tag) { Fabricate(:tag, topics: [topic], categories: [category], name: "category-tag") }
|
|
|
|
let!(:other_tag) { Fabricate(:tag, topics: [topic], name: "use-anywhere") }
|
2016-07-07 08:17:56 -05:00
|
|
|
let(:topic_list) { TopicList.new('latest', topic.user, [topic], { category: category.id, category_id: category.id }) }
|
|
|
|
|
|
|
|
it 'should only return tags allowed in the category' do
|
2016-07-08 16:13:32 -05:00
|
|
|
expect(topic_list.tags).to eq([tag.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "with no category, should return all tags" do
|
|
|
|
expect(TopicList.new('latest', other_topic.user, [other_topic]).tags.sort).to eq([tag.name, other_tag.name].sort)
|
|
|
|
end
|
2016-07-07 08:17:56 -05:00
|
|
|
|
2016-09-22 14:23:10 -05:00
|
|
|
it "with another category with no tags, should return no tags" do
|
2016-07-08 16:13:32 -05: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 })
|
2016-09-22 14:23:10 -05:00
|
|
|
expect(list.tags).to be_empty
|
2016-07-07 08:17:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-16 04:31:39 -05:00
|
|
|
end
|