From b7b2adde4402cad7556d8b1efbe3920ffcbf170d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Tue, 5 May 2020 20:17:29 +0200 Subject: [PATCH] Add specs for ThemeHelper --- spec/helpers/theme_helper_spec.rb | 108 ++++++++++++++++++++++++++++++ spec/rails_helper.rb | 1 + 2 files changed, 109 insertions(+) create mode 100644 spec/helpers/theme_helper_spec.rb diff --git a/spec/helpers/theme_helper_spec.rb b/spec/helpers/theme_helper_spec.rb new file mode 100644 index 00000000..69f2b588 --- /dev/null +++ b/spec/helpers/theme_helper_spec.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ThemeHelper, :type => :helper do + describe "#get_hex_color_from_theme_value" do + it "returns the proper hex value from the theme value" do + expect(helper.get_hex_color_from_theme_value(16777215)).to eq("ffffff") + end + end + + describe "#get_decimal_triplet_from_hex" do + it "returns the proper decimal triplet from a hex value" do + expect(helper.get_decimal_triplet_from_hex("5e35b1")).to eq("94, 53, 177") + end + end + + describe "#get_active_theme" do + context "when user is a guest" do + context "when target page doesn't have a theme" do + it "returns no theme" do + expect(helper.get_active_theme).to be_nil + end + end + + context "when target page has a theme" do + before(:each) do + @user = FactoryBot.create(:user) + @user.theme = Theme.new + @user.save! + end + + it "returns a theme" do + expect(helper.get_active_theme).to be_a(Theme) + end + end + end + + context "when user is signed in" do + let(:user) { FactoryBot.create(:user) } + + before(:each) { sign_in(user) } + + context "when user has no theme" do + context "when target page has no theme" do + it "returns no theme" do + expect(helper.get_active_theme).to be_nil + end + end + + context "when target page has a theme" do + let(:theme) { Theme.new } + + before(:each) do + @user = FactoryBot.create(:user) + @user.theme = theme + @user.save! + end + + it "returns a theme" do + expect(helper.get_active_theme).to be(theme) + end + end + end + + context "when user has a theme" do + let(:theme) { Theme.new } + + before(:each) do + user.theme = theme + user.show_foreign_themes = true + user.save! + end + + context "when target page has no theme" do + it "returns the theme of the current user" do + expect(helper.get_active_theme).to eq(theme) + end + end + + context "when target page has a theme" do + let(:user_theme) { Theme.new } + + before(:each) do + @user = FactoryBot.create(:user) + @user.theme = user_theme + @user.save! + end + + it "returns the theme of the current page" do + expect(helper.get_active_theme).to eq(user_theme) + end + + context "when user doesn't allow foreign themes" do + before(:each) do + user.show_foreign_themes = false + user.save! + end + + it "should return the users theme" do + expect(helper.get_active_theme).to eq(theme) + end + end + end + end + end + end +end \ No newline at end of file diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 742cffa8..e2f13e87 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -77,6 +77,7 @@ RSpec.configure do |config| # config.filter_gems_from_backtrace("gem name") config.include Devise::Test::ControllerHelpers, type: :controller + config.include Devise::Test::ControllerHelpers, type: :helper end Dir[Rails.root.join "spec", "shared_examples", "*.rb"].sort.each { |f| require f }