|
| 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