2022-12-01 09:32:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe SidebarSectionLinksUpdater do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2022-12-01 09:32:35 +08:00
|
|
|
fab!(:user2) { Fabricate(:user) }
|
|
|
|
|
|
|
|
describe ".update_category_section_links" do
|
|
|
|
fab!(:public_category) { Fabricate(:category) }
|
|
|
|
fab!(:public_category2) { Fabricate(:category) }
|
|
|
|
|
|
|
|
fab!(:user_category_section_link) do
|
|
|
|
Fabricate(:category_sidebar_section_link, linkable: public_category, user: user)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2023-07-27 10:52:33 +08:00
|
|
|
|
2022-12-01 09:32:35 +08:00
|
|
|
fab!(:user2_category_section_link) do
|
|
|
|
Fabricate(:category_sidebar_section_link, linkable: public_category, user: user2)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2022-12-01 09:32:35 +08:00
|
|
|
|
|
|
|
it "deletes all sidebar category section links when category ids provided is blank" do
|
|
|
|
described_class.update_category_section_links(user, category_ids: [])
|
|
|
|
|
|
|
|
expect(SidebarSectionLink.exists?(linkable: public_category, user: user)).to eq(false)
|
|
|
|
expect(SidebarSectionLink.exists?(linkable: public_category, user: user2)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2023-07-27 10:52:33 +08:00
|
|
|
it "updates user's sidebar category section link records to given category ids" do
|
2022-12-01 09:32:35 +08:00
|
|
|
expect(
|
|
|
|
SidebarSectionLink.where(linkable_type: "Category", user: user).pluck(:linkable_id),
|
|
|
|
).to contain_exactly(public_category.id)
|
|
|
|
|
|
|
|
described_class.update_category_section_links(
|
|
|
|
user,
|
2023-07-27 10:52:33 +08:00
|
|
|
category_ids: [public_category.id, public_category2.id],
|
2022-12-01 09:32:35 +08:00
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
|
|
|
expect(
|
2022-12-01 09:32:35 +08:00
|
|
|
SidebarSectionLink.where(linkable_type: "Category", user: user).pluck(:linkable_id),
|
2023-07-27 10:52:33 +08:00
|
|
|
).to contain_exactly(public_category.id, public_category2.id)
|
2022-12-01 09:32:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".update_tag_section_links" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:tag)
|
2022-12-01 09:32:35 +08:00
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
fab!(:user_tag_section_link) { Fabricate(:tag_sidebar_section_link, linkable: tag, user: user) }
|
2023-07-27 10:52:33 +08:00
|
|
|
|
2022-12-01 09:32:35 +08:00
|
|
|
fab!(:user2_tag_section_link) do
|
|
|
|
Fabricate(:tag_sidebar_section_link, linkable: tag, user: user2)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2022-12-01 09:32:35 +08:00
|
|
|
|
|
|
|
it "deletes all sidebar tag section links when tag names provided is blank" do
|
2023-07-27 10:52:33 +08:00
|
|
|
described_class.update_tag_section_links(user, tag_ids: [])
|
2022-12-01 09:32:35 +08:00
|
|
|
|
|
|
|
expect(SidebarSectionLink.exists?(linkable: tag, user: user)).to eq(false)
|
|
|
|
expect(SidebarSectionLink.exists?(linkable: tag, user: user2)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2023-07-27 10:52:33 +08:00
|
|
|
it "updates user's sidebar tag section link records to given tag names" do
|
2022-12-01 09:32:35 +08:00
|
|
|
expect(
|
|
|
|
SidebarSectionLink.where(linkable_type: "Tag", user: user).pluck(:linkable_id),
|
|
|
|
).to contain_exactly(tag.id)
|
|
|
|
|
2023-07-27 10:52:33 +08:00
|
|
|
described_class.update_tag_section_links(user, tag_ids: [tag.id, tag2.id])
|
2022-12-01 09:32:35 +08:00
|
|
|
|
|
|
|
expect(
|
|
|
|
SidebarSectionLink.where(linkable_type: "Tag", user: user).pluck(:linkable_id),
|
2023-07-27 10:52:33 +08:00
|
|
|
).to contain_exactly(tag.id, tag2.id)
|
2022-12-01 09:32:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|