Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component testing #3042

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ group :test do
gem "i18n-coverage"
gem "minitest-reporters"
gem "mocha"
gem "shoulda-context"
gem "simplecov"
gem "timecop"
gem "webmock", require: false
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ GEM
concurrent-ruby (~> 1.0, >= 1.0.2)
sentry-ruby-core (5.3.1)
concurrent-ruby
shoulda-context (2.0.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand Down Expand Up @@ -646,6 +647,7 @@ DEPENDENCIES
rss
rubocop-govuk
sassc-rails
shoulda-context
simplecov
slimmer
sprockets-rails
Expand Down
12 changes: 0 additions & 12 deletions app/views/components/_heading.html.erb

This file was deleted.

57 changes: 57 additions & 0 deletions test/components/all_components_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "test_helper"

class AllComponentsTest < ActionController::TestCase
Dir.glob("app/views/components/*.erb").each do |filename|
template = filename.split("/").last
component_name = template.sub("_", "").sub(".html", "").sub(".erb", "").gsub("-", "_")

context component_name do
yaml_file = "#{__dir__}/../../app/views/components/docs/#{component_name}.yml"

should "is documented" do
assert File.exist?(yaml_file)
end

should "have the correct documentation" do
yaml = YAML.unsafe_load_file(yaml_file)

assert yaml["name"]
assert yaml["description"]
assert yaml["examples"]
assert yaml["accessibility_criteria"] || yaml["shared_accessibility_criteria"]
end

should "have the correct class in the ERB template" do
erb = File.read(filename)

class_name = "app-c-#{component_name.dasherize}"

assert_includes erb, class_name
end

should "have a correctly named template file" do
template_file = "#{__dir__}/../../app/views/components/_#{component_name}.html.erb"

assert File.exist?(template_file)
end

should "have a correctly named spec file" do
rspec_file = "#{__dir__}/../../test/components/#{component_name.tr('-', '_')}_test.rb"

assert File.exist?(rspec_file)
end

should "have a correctly named SCSS file" do
css_file = "#{__dir__}/../../app/assets/stylesheets/components/_#{component_name.tr('_', '-')}.scss"

assert File.exist?(css_file)
end

should "not use `html_safe`", not_applicable: component_name.in?(%w[govspeak]) do
file = File.read(filename)

assert_no_match file, "html_safe"
end
end
end
end