Skip to content

Commit 0731dbc

Browse files
committed
Add components test
- checks for component files and their correct naming conventions - 'context' requires `shoulda-context` gem for minitest (copying from `frontend`)
1 parent 8142d53 commit 0731dbc

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ group :test do
4343
gem "i18n-coverage"
4444
gem "minitest-reporters"
4545
gem "mocha"
46+
gem "shoulda-context"
4647
gem "simplecov"
4748
gem "timecop"
4849
gem "webmock", require: false

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ GEM
567567
concurrent-ruby (~> 1.0, >= 1.0.2)
568568
sentry-ruby-core (5.3.1)
569569
concurrent-ruby
570+
shoulda-context (2.0.0)
570571
simplecov (0.22.0)
571572
docile (~> 1.1)
572573
simplecov-html (~> 0.11)
@@ -646,6 +647,7 @@ DEPENDENCIES
646647
rss
647648
rubocop-govuk
648649
sassc-rails
650+
shoulda-context
649651
simplecov
650652
slimmer
651653
sprockets-rails
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require "test_helper"
2+
3+
class AllComponentsTest < ActionController::TestCase
4+
Dir.glob("app/views/components/*.erb").each do |filename|
5+
template = filename.split("/").last
6+
component_name = template.sub("_", "").sub(".html", "").sub(".erb", "").gsub("-", "_")
7+
8+
context component_name do
9+
yaml_file = "#{__dir__}/../../app/views/components/docs/#{component_name}.yml"
10+
11+
should "is documented" do
12+
assert File.exist?(yaml_file)
13+
end
14+
15+
should "have the correct documentation" do
16+
yaml = YAML.unsafe_load_file(yaml_file)
17+
18+
assert yaml["name"]
19+
assert yaml["description"]
20+
assert yaml["examples"]
21+
assert yaml["accessibility_criteria"] || yaml["shared_accessibility_criteria"]
22+
end
23+
24+
should "have the correct class in the ERB template" do
25+
erb = File.read(filename)
26+
27+
class_name = "app-c-#{component_name.dasherize}"
28+
29+
assert_includes erb, class_name
30+
end
31+
32+
should "have a correctly named template file" do
33+
template_file = "#{__dir__}/../../app/views/components/_#{component_name}.html.erb"
34+
35+
assert File.exist?(template_file)
36+
end
37+
38+
should "have a correctly named spec file" do
39+
rspec_file = "#{__dir__}/../../test/components/#{component_name.tr('-', '_')}_test.rb"
40+
41+
assert File.exist?(rspec_file)
42+
end
43+
44+
should "have a correctly named SCSS file" do
45+
css_file = "#{__dir__}/../../app/assets/stylesheets/components/_#{component_name.tr('_', '-')}.scss"
46+
47+
assert File.exist?(css_file)
48+
end
49+
50+
should "not use `html_safe`", not_applicable: component_name.in?(%w[govspeak]) do
51+
file = File.read(filename)
52+
53+
assert_no_match file, "html_safe"
54+
end
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)