DEV: Add comments in flags specs

Followup to https://github.com/discourse/discourse/pull/29325.

This patch adds comments to tell why we need to destroy created flags in
specs once the examples have run.
This commit is contained in:
Loïc Guitaut 2024-10-22 10:07:24 +02:00 committed by Loïc Guitaut
parent f31f8cc15f
commit d991378218
5 changed files with 26 additions and 16 deletions

View File

@ -40,9 +40,11 @@ RSpec.describe(Flags::CreateFlag) do
context "when name is not unique" do context "when name is not unique" do
let!(:flag) { Fabricate(:flag, name:) } let!(:flag) { Fabricate(:flag, name:) }
it { is_expected.to fail_a_policy(:unique_name) } # DO NOT REMOVE: flags have side effects and their state will leak to
# other examples otherwise.
after { flag.destroy! } after { flag.destroy! }
it { is_expected.to fail_a_policy(:unique_name) }
end end
context "when everything's ok" do context "when everything's ok" do
@ -56,6 +58,8 @@ RSpec.describe(Flags::CreateFlag) do
) )
end end
# DO NOT REMOVE: flags have side effects and their state will leak to
# other examples otherwise.
after { flag.destroy! } after { flag.destroy! }
it { is_expected.to run_successfully } it { is_expected.to run_successfully }

View File

@ -4,35 +4,35 @@ RSpec.describe(Flags::DestroyFlag) do
subject(:result) { described_class.call(**params, **dependencies) } subject(:result) { described_class.call(**params, **dependencies) }
fab!(:current_user) { Fabricate(:admin) } fab!(:current_user) { Fabricate(:admin) }
fab!(:flag)
let(:flag) { Fabricate(:flag) }
let(:params) { { id: flag_id } } let(:params) { { id: flag_id } }
let(:dependencies) { { guardian: current_user.guardian } } let(:dependencies) { { guardian: current_user.guardian } }
let(:flag_id) { flag.id } let(:flag_id) { flag.id }
# DO NOT REMOVE: flags have side effects and their state will leak to
# other examples otherwise.
after { flag.destroy! }
context "when model is not found" do context "when model is not found" do
after { flag.destroy! }
let(:flag_id) { 0 } let(:flag_id) { 0 }
it { is_expected.to fail_to_find_a_model(:flag) } it { is_expected.to fail_to_find_a_model(:flag) }
end end
context "when the flag is a system one" do context "when the flag is a system one" do
after { flag.destroy! }
let(:flag) { Flag.first } let(:flag) { Flag.first }
it { is_expected.to fail_a_policy(:not_system) } it { is_expected.to fail_a_policy(:not_system) }
end end
context "when the flag has been used" do context "when the flag has been used" do
after { flag.destroy! }
let!(:post_action) { Fabricate(:post_action, post_action_type_id: flag.id) } let!(:post_action) { Fabricate(:post_action, post_action_type_id: flag.id) }
it { is_expected.to fail_a_policy(:not_used) } it { is_expected.to fail_a_policy(:not_used) }
end end
context "when user is not allowed to perform the action" do context "when user is not allowed to perform the action" do
after { flag.destroy! }
fab!(:current_user) { Fabricate(:user) } fab!(:current_user) { Fabricate(:user) }
it { is_expected.to fail_a_policy(:invalid_access) } it { is_expected.to fail_a_policy(:invalid_access) }

View File

@ -42,9 +42,9 @@ RSpec.describe(Flags::ReorderFlag) do
end end
context "when everything's ok" do context "when everything's ok" do
after do # DO NOT REMOVE: flags have side effects and their state will leak to
described_class.call(flag_id: flag.id, guardian: current_user.guardian, direction: "down") # other examples otherwise.
end after { described_class.call(**params.merge(direction: "down"), **dependencies) }
it { is_expected.to run_successfully } it { is_expected.to run_successfully }

View File

@ -9,12 +9,16 @@ RSpec.describe(Flags::ToggleFlag) do
subject(:result) { described_class.call(**params, **dependencies) } subject(:result) { described_class.call(**params, **dependencies) }
fab!(:current_user) { Fabricate(:admin) } fab!(:current_user) { Fabricate(:admin) }
fab!(:flag)
let(:flag) { Fabricate(:flag) }
let(:flag_id) { flag.id } let(:flag_id) { flag.id }
let(:params) { { flag_id: flag_id } } let(:params) { { flag_id: flag_id } }
let(:dependencies) { { guardian: current_user.guardian } } let(:dependencies) { { guardian: current_user.guardian } }
# DO NOT REMOVE: flags have side effects and their state will leak to
# other examples otherwise.
after { flag.destroy! }
context "when user is not allowed to perform the action" do context "when user is not allowed to perform the action" do
fab!(:current_user) { Fabricate(:user) } fab!(:current_user) { Fabricate(:user) }
@ -34,8 +38,6 @@ RSpec.describe(Flags::ToggleFlag) do
end end
context "when everything's ok" do context "when everything's ok" do
after { flag.reload.update!(enabled: true) }
it { is_expected.to run_successfully } it { is_expected.to run_successfully }
it "toggles the flag" do it "toggles the flag" do

View File

@ -14,8 +14,8 @@ RSpec.describe(Flags::UpdateFlag) do
subject(:result) { described_class.call(**params, **dependencies) } subject(:result) { described_class.call(**params, **dependencies) }
fab!(:current_user) { Fabricate(:admin) } fab!(:current_user) { Fabricate(:admin) }
fab!(:flag)
let(:flag) { Fabricate(:flag) }
let(:params) do let(:params) do
{ {
id: flag_id, id: flag_id,
@ -36,6 +36,8 @@ RSpec.describe(Flags::UpdateFlag) do
let(:enabled) { false } let(:enabled) { false }
let(:auto_action_type) { true } let(:auto_action_type) { true }
# DO NOT REMOVE: flags have side effects and their state will leak to
# other examples otherwise.
after { flag.destroy! } after { flag.destroy! }
context "when contract is invalid" do context "when contract is invalid" do
@ -71,9 +73,11 @@ RSpec.describe(Flags::UpdateFlag) do
context "when title is not unique" do context "when title is not unique" do
let!(:flag_2) { Fabricate(:flag, name:) } let!(:flag_2) { Fabricate(:flag, name:) }
it { is_expected.to fail_a_policy(:unique_name) } # DO NOT REMOVE: flags have side effects and their state will leak to
# other examples otherwise.
after { flag_2.destroy! } after { flag_2.destroy! }
it { is_expected.to fail_a_policy(:unique_name) }
end end
context "when everything's ok" do context "when everything's ok" do