docbrown/spec/forms/creator_settings_form_spec.rb
Daniel Uber 1d26485c36
Clear changes to settings after checking form saves them (#15746)
* Clear the cached community name before loading new page

When the creator settings form had been run before the create new page
spec, the community name would be changed from the default
"DEV(local)" to "Climbing Life". Since the application helper can
memoize this value - and the database will not - ensure these are in
sync by clearing the settings cache before requesting the page (which
will be prefilled based on the setting).

This "fix" should probably be moved into the test introducing the
change, rather than the one impacted by it.

* Move clearing the settings cache closer to the change

Rather than clearing the cache where we were impacted, clear
immediately after the save spec completes.
2021-12-14 08:39:14 -06:00

71 lines
2.7 KiB
Ruby

require "rails_helper"
RSpec.describe CreatorSettingsForm, type: :model do
describe "validations" do
it { is_expected.to validate_presence_of(:community_name) }
it { is_expected.to validate_presence_of(:primary_brand_color_hex) }
end
describe "attributes" do
it "has the correct attribute names" do
expect(described_class.attribute_names).to eq(%w[checked_code_of_conduct checked_terms_and_conditions
community_name invite_only_mode logo
primary_brand_color_hex public])
end
it "sets default values", :aggregate_failures do
attributes = described_class.new.attributes
expect(attributes["checked_code_of_conduct"]).to be(false)
expect(attributes["checked_terms_and_conditions"]).to be(false)
end
end
describe "initializer" do
it "updates the values when we pass an attribute as a param", :aggregate_failures do
attributes = described_class.new(
primary_brand_color_hex: "#0a0a0a",
invite_only_mode: false,
).attributes
expect(attributes["primary_brand_color_hex"]).to eq("#0a0a0a")
expect(attributes["invite_only_mode"]).to eq(false)
end
end
describe "#save" do
let(:current_user) { create(:user) }
after do
# prevent changes here from leaking into other tests
Settings::Community.clear_cache
Settings::UserExperience.clear_cache
Settings::Authentication.clear_cache
end
# rubocop:disable RSpec/ExampleLength
it "saves the updated attributes to the correct Settings values", :aggregate_failures do
# NOTE: override the profile migration hack from rails_helper.rb
# TODO: remove this once we remove it in rails_helper.rb
allow(Settings::UserExperience).to receive(:public).and_call_original
creator_settings_form = described_class.new(
checked_code_of_conduct: true,
checked_terms_and_conditions: true,
community_name: "Climbing Life",
invite_only_mode: false,
public: false,
primary_brand_color_hex: "#a81adb",
)
expect(creator_settings_form.valid?).to be(true)
creator_settings_form.save
expect(creator_settings_form.success).to be(true)
expect(Settings::Community.community_name).to eq("Climbing Life")
expect(Settings::UserExperience.primary_brand_color_hex).to eq("#a81adb")
expect(Settings::UserExperience.public).to eq(false)
expect(Settings::Authentication.invite_only_mode).to eq(false)
expect(current_user.checked_code_of_conduct).to eq(true)
expect(current_user.checked_terms_and_conditions).to eq(true)
end
# rubocop:enable RSpec/ExampleLength
end
end