discourse/spec/models/sidebar_section_link_spec.rb
Daniel Waterworth 6e161d3e75
DEV: Allow fab! without block (#24314)
The most common thing that we do with fab! is:

    fab!(:thing) { Fabricate(:thing) }

This commit adds a shorthand for this which is just simply:

    fab!(:thing)

i.e. If you omit the block, then, by default, you'll get a `Fabricate`d object using the fabricator of the same name.
2023-11-09 16:47:59 -06:00

75 lines
2.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe SidebarSectionLink do
fab!(:user)
describe "Validations" do
it "is not valid when linkable already exists for the current user" do
category_sidebar_section_link = Fabricate(:category_sidebar_section_link, user: user)
sidebar_section_link =
SidebarSectionLink.new(user: user, linkable: category_sidebar_section_link.linkable)
expect(sidebar_section_link.valid?).to eq(false)
expect(sidebar_section_link.errors.details[:user_id][0][:error]).to eq(:taken)
end
describe "#linkable_type" do
it "is not valid when linkable_type is not supported" do
sidebar_section_link =
SidebarSectionLink.new(user: user, linkable_id: 1, linkable_type: "sometype")
expect(sidebar_section_link.valid?).to eq(false)
expect(sidebar_section_link.errors[:linkable_type]).to eq(
[
I18n.t(
"activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid",
),
],
)
end
it "is not valid when linkable_type is Tag and SiteSetting.tagging_enabled is false" do
SiteSetting.tagging_enabled = false
sidebar_section_link =
SidebarSectionLink.new(user: user, linkable_id: 1, linkable_type: "Tag")
expect(sidebar_section_link.valid?).to eq(false)
expect(sidebar_section_link.errors[:linkable_type]).to eq(
[
I18n.t(
"activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid",
),
],
)
end
end
end
it "uses section user for links belonging to private sections" do
private_section = Fabricate(:sidebar_section, public: false)
sidebar_section_link =
Fabricate(
:sidebar_section_link,
sidebar_section: private_section,
linkable_id: 1,
linkable_type: "Tag",
)
expect(sidebar_section_link.user_id).to eq(private_section.user_id)
end
it "uses system user for links belonging to public sections" do
public_section = Fabricate(:sidebar_section, public: true)
sidebar_section_link =
Fabricate(
:sidebar_section_link,
sidebar_section: public_section,
linkable_id: 1,
linkable_type: "Tag",
)
expect(sidebar_section_link.user_id).to eq(Discourse.system_user.id)
end
end