|
| 1 | +require "test_helper" |
| 2 | +require "generators/suspenders/inline_svg_generator" |
| 3 | + |
| 4 | +module Suspenders |
| 5 | + module Generators |
| 6 | + class InlinveSvgGeneratorTest < Rails::Generators::TestCase |
| 7 | + include Suspenders::TestHelpers |
| 8 | + |
| 9 | + tests Suspenders::Generators::InlineSvgGenerator |
| 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 "inline_svg", file |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + test "does not raise if API configuration is commented out" do |
| 27 | + within_api_only_app do |
| 28 | + path = app_root("config/application.rb") |
| 29 | + content = File.binread(path).gsub!("config.api_only = true", "# config.api_only = true") |
| 30 | + File.binwrite(path, content) |
| 31 | + |
| 32 | + run_generator |
| 33 | + |
| 34 | + assert_file app_root("Gemfile") do |file| |
| 35 | + assert_match "inline_svg", file |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + test "adds gems to Gemfile" do |
| 41 | + expected_output = <<~RUBY |
| 42 | + gem "inline_svg" |
| 43 | + RUBY |
| 44 | + |
| 45 | + run_generator |
| 46 | + |
| 47 | + assert_file app_root("Gemfile") do |file| |
| 48 | + assert_match(expected_output, file) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + test "installs gems with Bundler" do |
| 53 | + Bundler.stubs(:with_unbundled_env).yields |
| 54 | + generator.expects(:run).with("bundle install").once |
| 55 | + |
| 56 | + capture(:stdout) do |
| 57 | + generator.add_inline_svg_gem |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + test "generator has a description" do |
| 62 | + description = "Render SVG images inline, as a potential performance improvement for the viewer." |
| 63 | + |
| 64 | + assert_equal description, Suspenders::Generators::InlineSvgGenerator.desc |
| 65 | + end |
| 66 | + |
| 67 | + test "configures raising an error when an SVG file is not found" do |
| 68 | + expected_configuration = <<~RUBY |
| 69 | + InlineSvg.configure do |config| |
| 70 | + config.raise_on_file_not_found = true |
| 71 | + end |
| 72 | + RUBY |
| 73 | + |
| 74 | + run_generator |
| 75 | + |
| 76 | + assert_file app_root("config/initializers/inline_svg.rb") do |file| |
| 77 | + assert_match(expected_configuration, file) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + private |
| 82 | + |
| 83 | + def prepare_destination |
| 84 | + touch "Gemfile" |
| 85 | + end |
| 86 | + |
| 87 | + def restore_destination |
| 88 | + remove_file_if_exists "Gemfile" |
| 89 | + remove_file_if_exists "config/initializers/inline_svg.rb" |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments