From ec12be830d94fe79c1ed07e7303d1ea3cdc5f314 Mon Sep 17 00:00:00 2001 From: Melissa Miller Date: Fri, 26 Jul 2019 16:33:33 -0400 Subject: [PATCH] Update simplecov config, increase coverage (#1120) --- .simplecov | 3 + spec/helpers/application_helper_spec.rb | 89 +++++++++++++++++++++++++ spec/models/user_spec.rb | 18 +++++ spec/rails_helper.rb | 2 + spec/spec_helper.rb | 2 - 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 .simplecov diff --git a/.simplecov b/.simplecov new file mode 100644 index 0000000000..1071079540 --- /dev/null +++ b/.simplecov @@ -0,0 +1,3 @@ +SimpleCov.start 'rails' do + # any custom configs like groups and filters can be here at a central place +end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 54f5811473..72a0ac42f4 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -1,6 +1,95 @@ require "rails_helper" RSpec.describe ApplicationHelper, type: :helper do + describe "dashboard_path_from_user" do + before(:each) do + allow(helper).to receive(:current_user).and_return(user) + end + + context "As a super admin" do + let(:user) { create :super_admin } + + it "links to the admin dashboard" do + expect(helper.dashboard_path_from_user).to eq "/admin/dashboard" + end + end + + context "As a user without super admin status" do + let(:user) { create :organization_admin } + + it "links to the general dashboard" do + pending("TODO - dashboard_path_from_user needs to receive user org to generate path") + org_name = user.organization.short_name + expect(helper.dashboard_path_from_user).to eq "/#{org_name}/dashboard" + end + end + end + + describe "default_title_content" do + helper do + def current_organization; end + end + + before(:each) do + allow(helper).to receive(:current_organization).and_return(organization) + end + + context "Organization exists" do + let(:organization) { create :organization } + + it "returns the organization's name" do + expect(helper.default_title_content).to eq organization.name + end + end + + context "Organization does not exist" do + let(:organization) { nil } + + it "returns a default name" do + expect(helper.default_title_content).to eq "DiaperBank" + end + end + end + + describe "active_class" do + it "Returns the controller name" do + expect(helper.active_class("foo")).to eq "test" + end + end + + describe "can_administrate?" do + let(:org_1) { @organization } + let(:org_2) { create :organization } + + before(:each) do + allow(helper).to receive(:current_user).and_return(user) + end + + context "User is org admin and part of org" do + let(:user) { create :user, organization_admin: true, organization: org_1 } + + it "can administrate" do + expect(helper.can_administrate?).to be_truthy + end + end + + context "User is org admin and not part of org" do + let(:user) { create :user, organization_admin: true, organization: org_2 } + + it "cannot administrate" do + expect(helper.can_administrate?).to be_falsy + end + end + + context "User is part of org but not org admin" do + let(:user) { create :user, organization: org_1 } + + it "cannot administrate" do + expect(helper.can_administrate?).to be_falsy + end + end + end + describe "confirm_delete_msg" do let(:item) { "Adult Briefs (Medium/Large)" } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a5c082dbc4..58c3781fde 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -29,14 +29,32 @@ # super_admin :boolean default(FALSE) # last_request_at :datetime # +require "rails_helper" RSpec.describe User, type: :model do + it "has a valid factory" do + expect(build(:user)).to be_valid + end + + context "Associations" do + it { + expect(described_class.reflect_on_association(:organization).macro) + .to eq(:belongs_to) + } + it { + expect(described_class.reflect_on_association(:feedback_messages).macro) + .to eq(:has_many) + } + end + context "Validations >" do it "requires a name" do expect(build(:user, name: nil)).not_to be_valid + expect(build(:user, name: "foo")).to be_valid end it "requires an email" do expect(build(:user, email: nil)).not_to be_valid + expect(build(:user, email: "foo@bar.com")).to be_valid end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 5d4ecce81e..f6cd0ce318 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,5 +1,6 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= "test" +require 'simplecov' require File.expand_path("../config/environment", __dir__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? @@ -100,6 +101,7 @@ def stub_addresses RSpec.configure do |config| config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::Test::ControllerHelpers, type: :view + config.include Devise::Test::ControllerHelpers, type: :helper config.include Devise::Test::IntegrationHelpers, type: :feature config.include Devise::Test::IntegrationHelpers, type: :system diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1d87a7d819..62f13ac7d0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,9 +16,7 @@ # users commonly want. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration -require 'simplecov' require 'active_support/testing/time_helpers' -SimpleCov.start 'rails' RSpec.configure do |config| config.include ActiveSupport::Testing::TimeHelpers