Skip to content

Commit

Permalink
Merge pull request #3042 from alphagov/add-component-testing
Browse files Browse the repository at this point in the history
Add component testing
  • Loading branch information
andysellick authored Jan 11, 2024
2 parents 7d04b2d + 0731dbc commit 5d05814
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
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

0 comments on commit 5d05814

Please sign in to comment.