discourse/spec/services/nested_topic/toggle_spec.rb
Jarek Radosz 3832cc5c42
DEV: Fix a shoulda-matchers warning (#39429)
that matcher wasn't useful (rails ensures that values are casted to true
or false)

```
************************************************************************
Warning from shoulda-matchers:

You are using `validate_inclusion_of` to assert that a boolean column
allows boolean values and disallows non-boolean ones. Be aware that it
is not possible to fully test this, as boolean columns will
automatically convert non-boolean values to boolean ones. Hence, you
should consider removing this test.
************************************************************************
```
2026-04-22 11:26:07 +02:00

80 lines
2.1 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe NestedTopic::Toggle do
describe described_class::Contract, type: :model do
it { is_expected.to validate_presence_of(:topic_id) }
end
describe ".call" do
subject(:result) { described_class.call(params:, **dependencies) }
fab!(:admin)
fab!(:topic)
let(:params) { { topic_id: topic.id, enabled: } }
let(:dependencies) { { guardian: admin.guardian } }
let(:enabled) { true }
context "when contract is invalid" do
let(:params) { { topic_id: nil, enabled: true } }
it { is_expected.to fail_a_contract }
end
context "when enabled is nil" do
let(:params) { { topic_id: topic.id, enabled: nil } }
it { is_expected.to fail_a_contract }
end
context "when topic is not found" do
let(:params) { { topic_id: 0, enabled: true } }
it { is_expected.to fail_to_find_a_model(:topic) }
end
context "when user is not staff" do
fab!(:admin, :user)
it { is_expected.to fail_a_policy(:staff_can_edit) }
end
context "when enabling nested view" do
let(:enabled) { true }
it { is_expected.to run_successfully }
it "creates a nested topic record" do
expect { result }.to change { NestedTopic.where(topic: topic).count }.from(0).to(1)
end
context "when nested topic already exists" do
before { Fabricate(:nested_topic, topic: topic) }
it { is_expected.to run_successfully }
it "does not create a duplicate record" do
expect { result }.not_to change { NestedTopic.count }
end
end
end
context "when disabling nested view" do
let(:enabled) { false }
before { Fabricate(:nested_topic, topic: topic) }
it { is_expected.to run_successfully }
it "destroys the nested topic record" do
expect { result }.to change { NestedTopic.where(topic: topic).count }.from(1).to(0)
end
end
context "when disabling nested view without existing record" do
let(:enabled) { false }
it { is_expected.to run_successfully }
end
end
end