Skip to content

Commit ec12be8

Browse files
holytoastrarmahillo
authored andcommitted
Update simplecov config, increase coverage (rubyforgood#1120)
1 parent e5203fe commit ec12be8

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

.simplecov

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SimpleCov.start 'rails' do
2+
# any custom configs like groups and filters can be here at a central place
3+
end

spec/helpers/application_helper_spec.rb

+89
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,95 @@
11
require "rails_helper"
22

33
RSpec.describe ApplicationHelper, type: :helper do
4+
describe "dashboard_path_from_user" do
5+
before(:each) do
6+
allow(helper).to receive(:current_user).and_return(user)
7+
end
8+
9+
context "As a super admin" do
10+
let(:user) { create :super_admin }
11+
12+
it "links to the admin dashboard" do
13+
expect(helper.dashboard_path_from_user).to eq "/admin/dashboard"
14+
end
15+
end
16+
17+
context "As a user without super admin status" do
18+
let(:user) { create :organization_admin }
19+
20+
it "links to the general dashboard" do
21+
pending("TODO - dashboard_path_from_user needs to receive user org to generate path")
22+
org_name = user.organization.short_name
23+
expect(helper.dashboard_path_from_user).to eq "/#{org_name}/dashboard"
24+
end
25+
end
26+
end
27+
28+
describe "default_title_content" do
29+
helper do
30+
def current_organization; end
31+
end
32+
33+
before(:each) do
34+
allow(helper).to receive(:current_organization).and_return(organization)
35+
end
36+
37+
context "Organization exists" do
38+
let(:organization) { create :organization }
39+
40+
it "returns the organization's name" do
41+
expect(helper.default_title_content).to eq organization.name
42+
end
43+
end
44+
45+
context "Organization does not exist" do
46+
let(:organization) { nil }
47+
48+
it "returns a default name" do
49+
expect(helper.default_title_content).to eq "DiaperBank"
50+
end
51+
end
52+
end
53+
54+
describe "active_class" do
55+
it "Returns the controller name" do
56+
expect(helper.active_class("foo")).to eq "test"
57+
end
58+
end
59+
60+
describe "can_administrate?" do
61+
let(:org_1) { @organization }
62+
let(:org_2) { create :organization }
63+
64+
before(:each) do
65+
allow(helper).to receive(:current_user).and_return(user)
66+
end
67+
68+
context "User is org admin and part of org" do
69+
let(:user) { create :user, organization_admin: true, organization: org_1 }
70+
71+
it "can administrate" do
72+
expect(helper.can_administrate?).to be_truthy
73+
end
74+
end
75+
76+
context "User is org admin and not part of org" do
77+
let(:user) { create :user, organization_admin: true, organization: org_2 }
78+
79+
it "cannot administrate" do
80+
expect(helper.can_administrate?).to be_falsy
81+
end
82+
end
83+
84+
context "User is part of org but not org admin" do
85+
let(:user) { create :user, organization: org_1 }
86+
87+
it "cannot administrate" do
88+
expect(helper.can_administrate?).to be_falsy
89+
end
90+
end
91+
end
92+
493
describe "confirm_delete_msg" do
594
let(:item) { "Adult Briefs (Medium/Large)" }
695

spec/models/user_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,32 @@
2929
# super_admin :boolean default(FALSE)
3030
# last_request_at :datetime
3131
#
32+
require "rails_helper"
3233

3334
RSpec.describe User, type: :model do
35+
it "has a valid factory" do
36+
expect(build(:user)).to be_valid
37+
end
38+
39+
context "Associations" do
40+
it {
41+
expect(described_class.reflect_on_association(:organization).macro)
42+
.to eq(:belongs_to)
43+
}
44+
it {
45+
expect(described_class.reflect_on_association(:feedback_messages).macro)
46+
.to eq(:has_many)
47+
}
48+
end
49+
3450
context "Validations >" do
3551
it "requires a name" do
3652
expect(build(:user, name: nil)).not_to be_valid
53+
expect(build(:user, name: "foo")).to be_valid
3754
end
3855
it "requires an email" do
3956
expect(build(:user, email: nil)).not_to be_valid
57+
expect(build(:user, email: "foo@bar.com")).to be_valid
4058
end
4159
end
4260

spec/rails_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file is copied to spec/ when you run 'rails generate rspec:install'
22
ENV["RAILS_ENV"] ||= "test"
3+
require 'simplecov'
34
require File.expand_path("../config/environment", __dir__)
45
# Prevent database truncation if the environment is production
56
abort("The Rails environment is running in production mode!") if Rails.env.production?
@@ -100,6 +101,7 @@ def stub_addresses
100101
RSpec.configure do |config|
101102
config.include Devise::Test::ControllerHelpers, type: :controller
102103
config.include Devise::Test::ControllerHelpers, type: :view
104+
config.include Devise::Test::ControllerHelpers, type: :helper
103105
config.include Devise::Test::IntegrationHelpers, type: :feature
104106
config.include Devise::Test::IntegrationHelpers, type: :system
105107

spec/spec_helper.rb

-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
# users commonly want.
1717
#
1818
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19-
require 'simplecov'
2019
require 'active_support/testing/time_helpers'
21-
SimpleCov.start 'rails'
2220

2321
RSpec.configure do |config|
2422
config.include ActiveSupport::Testing::TimeHelpers

0 commit comments

Comments
 (0)