From a9cfa3fccca45abb3200c6e828e17317e47fd758 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 24 Jun 2022 22:50:05 +0200 Subject: [PATCH] Add tests for toggling unmask --- Gemfile | 1 + Gemfile.lock | 3 ++ .../controllers/moderation_controller_spec.rb | 34 +++++++++++++++++++ spec/rails_helper.rb | 7 ++++ 4 files changed, 45 insertions(+) create mode 100644 spec/controllers/moderation_controller_spec.rb diff --git a/Gemfile b/Gemfile index 041d91f0..5f2dbfb1 100644 --- a/Gemfile +++ b/Gemfile @@ -95,6 +95,7 @@ group :development, :test do gem "rspec-sidekiq", "~> 3.0", require: false gem "rubocop", "~> 1.22", ">= 1.22.1" gem "rubocop-rails", "~> 2.13", ">= 2.13.1" + gem "shoulda-matchers", "~> 5.0" gem "simplecov", require: false gem "simplecov-cobertura", require: false gem "simplecov-json", require: false diff --git a/Gemfile.lock b/Gemfile.lock index 30af70f3..7acac0da 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -515,6 +515,8 @@ GEM sentry-ruby-core (~> 5.2.1) sidekiq (>= 3.0) shellany (0.0.1) + shoulda-matchers (5.1.0) + activesupport (>= 5.2.0) sidekiq (5.2.10) connection_pool (~> 2.2, >= 2.2.2) rack (~> 2.0) @@ -659,6 +661,7 @@ DEPENDENCIES sentry-rails sentry-ruby sentry-sidekiq + shoulda-matchers (~> 5.0) sidekiq (< 6) simplecov simplecov-cobertura diff --git a/spec/controllers/moderation_controller_spec.rb b/spec/controllers/moderation_controller_spec.rb new file mode 100644 index 00000000..15391661 --- /dev/null +++ b/spec/controllers/moderation_controller_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ModerationController, type: :controller do + describe "#toggle_unmask" do + let(:user) { FactoryBot.create(:user, roles: [:moderator]) } + + before do + sign_in(user) + post :toggle_unmask, session: { moderation_view: moderation_view } + end + + context "when moderation view flag is true" do + let(:moderation_view) { true } + + it { is_expected.to set_session[:moderation_view].to(false) } + + it { is_expected.to redirect_to(root_path) } + end + + context "when moderation view flag is false" do + let(:moderation_view) { false } + + it { is_expected.to set_session[:moderation_view].to(true) } + end + + context "when moderation view flag is not set" do + let(:moderation_view) { nil } + + it { is_expected.to set_session[:moderation_view].to(true) } + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index e2f13e87..e0552d11 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -80,4 +80,11 @@ RSpec.configure do |config| config.include Devise::Test::ControllerHelpers, type: :helper end +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end +end + Dir[Rails.root.join "spec", "shared_examples", "*.rb"].sort.each { |f| require f }