|
| 1 | +require "test_helper" |
| 2 | +require "generators/suspenders/accessibility_generator" |
| 3 | + |
| 4 | +module Suspenders |
| 5 | + module Generators |
| 6 | + class AccessibilityGeneratorTest < Rails::Generators::TestCase |
| 7 | + include Suspenders::TestHelpers |
| 8 | + |
| 9 | + tests Suspenders::Generators::AccessibilityGenerator |
| 10 | + destination Rails.root |
| 11 | + setup :prepare_destination |
| 12 | + teardown :restore_destination |
| 13 | + |
| 14 | + test "raises if API only application" do |
| 15 | + within_api_only_app do |
| 16 | + assert_raises Suspenders::Generators::APIAppUnsupported::Error do |
| 17 | + run_generator |
| 18 | + end |
| 19 | + |
| 20 | + assert_file app_root("Gemfile") do |file| |
| 21 | + assert_no_match "capybara_accessibility_audit", file |
| 22 | + assert_no_match "capybara_accessible_selectors", file |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + test "does not raise if API configuration is commented out" do |
| 28 | + within_api_only_app do |
| 29 | + path = app_root("config/application.rb") |
| 30 | + content = File.binread(path).gsub!("config.api_only = true", "# config.api_only = true") |
| 31 | + File.binwrite(path, content) |
| 32 | + |
| 33 | + run_generator |
| 34 | + |
| 35 | + assert_file app_root("Gemfile") do |file| |
| 36 | + assert_match "capybara_accessibility_audit", file |
| 37 | + assert_match "capybara_accessible_selectors", file |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + test "adds gems to Gemfile" do |
| 43 | + expected_output = <<~RUBY |
| 44 | + group :test do |
| 45 | + gem "capybara_accessibility_audit" |
| 46 | + gem "capybara_accessible_selectors", github: "citizensadvice/capybara_accessible_selectors" |
| 47 | + end |
| 48 | + RUBY |
| 49 | + |
| 50 | + run_generator |
| 51 | + |
| 52 | + assert_file app_root("Gemfile") do |file| |
| 53 | + assert_match(expected_output, file) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + test "installs gems with Bundler" do |
| 58 | + Bundler.stubs(:with_unbundled_env).yields |
| 59 | + generator.expects(:run).with("bundle install").once |
| 60 | + |
| 61 | + capture(:stdout) do |
| 62 | + generator.add_capybara_gems |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + test "generator has a description" do |
| 67 | + description = "Installs capybara_accessibility_audit and capybara_accessible_selectors" |
| 68 | + |
| 69 | + assert_equal description, Suspenders::Generators::AccessibilityGenerator.desc |
| 70 | + end |
| 71 | + |
| 72 | + private |
| 73 | + |
| 74 | + def prepare_destination |
| 75 | + touch "Gemfile" |
| 76 | + end |
| 77 | + |
| 78 | + def restore_destination |
| 79 | + remove_file_if_exists "Gemfile" |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments