aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/mailer_generator_test.rb
blob: 3d1cf87daedef0790752ab35911a9f18ab5adcee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require 'generators/generators_test_helper'
require 'rails/generators/mailer/mailer_generator'

class MailerGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  arguments %w(notifier foo bar)

  def test_mailer_skeleton_is_created
    run_generator
    assert_file "app/mailers/notifier.rb" do |mailer|
      assert_match(/class Notifier < ApplicationMailer/, mailer)
      assert_no_match(/default from: "from@example.com"/, mailer)
      assert_no_match(/layout :mailer_notifier/, mailer)
    end
  end

  def test_application_mailer_skeleton_is_created
    run_generator
    assert_file "app/mailers/application_mailer.rb" do |mailer|
      assert_match(/class ApplicationMailer < ActionMailer::Base/, mailer)
      assert_match(/default from: "from@example.com"/, mailer)
      assert_match(/layout 'mailer'/, mailer)
    end
  end

  def test_mailer_with_i18n_helper
    run_generator
    assert_file "app/mailers/notifier.rb" do |mailer|
      assert_match(/en\.notifier\.foo\.subject/, mailer)
      assert_match(/en\.notifier\.bar\.subject/, mailer)
    end
  end

  def test_check_class_collision
    Object.send :const_set, :Notifier, Class.new
    content = capture(:stderr){ run_generator }
    assert_match(/The name 'Notifier' is either already used in your application or reserved/, content)
  ensure
    Object.send :remove_const, :Notifier
  end

  def test_invokes_default_test_framework
    run_generator
    assert_file "test/mailers/notifier_test.rb" do |test|
      assert_match(/class NotifierTest < ActionMailer::TestCase/, test)
      assert_match(/test "foo"/, test)
      assert_match(/test "bar"/, test)
    end
    assert_file "test/mailers/previews/notifier_preview.rb" do |preview|
      assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/notifier/, preview)
      assert_match(/class NotifierPreview < ActionMailer::Preview/, preview)
      assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier\/foo/, preview)
      assert_instance_method :foo, preview do |foo|
        assert_match(/Notifier.foo/, foo)
      end
      assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier\/bar/, preview)
      assert_instance_method :bar, preview do |bar|
        assert_match(/Notifier.bar/, bar)
      end
    end
  end

  def test_check_test_class_collision
    Object.send :const_set, :NotifierTest, Class.new
    content = capture(:stderr){ run_generator }
    assert_match(/The name 'NotifierTest' is either already used in your application or reserved/, content)
  ensure
    Object.send :remove_const, :NotifierTest
  end

  def test_check_preview_class_collision
    Object.send :const_set, :NotifierPreview, Class.new
    content = capture(:stderr){ run_generator }
    assert_match(/The name 'NotifierPreview' is either already used in your application or reserved/, content)
  ensure
    Object.send :remove_const, :NotifierPreview
  end

  def test_invokes_default_text_template_engine
    run_generator
    assert_file "app/views/notifier/foo.text.erb" do |view|
      assert_match(%r(\sapp/views/notifier/foo\.text\.erb), view)
      assert_match(/<%= @greeting %>/, view)
    end

    assert_file "app/views/notifier/bar.text.erb" do |view|
      assert_match(%r(\sapp/views/notifier/bar\.text\.erb), view)
      assert_match(/<%= @greeting %>/, view)
    end

    assert_file "app/views/layouts/mailer.text.erb" do |view|
      assert_match(/<%= yield %>/, view)
    end
  end

  def test_invokes_default_html_template_engine
    run_generator
    assert_file "app/views/notifier/foo.html.erb" do |view|
      assert_match(%r(\sapp/views/notifier/foo\.html\.erb), view)
      assert_match(/<%= @greeting %>/, view)
    end

    assert_file "app/views/notifier/bar.html.erb" do |view|
      assert_match(%r(\sapp/views/notifier/bar\.html\.erb), view)
      assert_match(/<%= @greeting %>/, view)
    end

    assert_file "app/views/layouts/mailer.html.erb" do |view|
      assert_match(%r{<html>\n  <body>\n    <%= yield %>\n  </body>\n</html>}, view)
    end
  end

  def test_invokes_default_template_engine_even_with_no_action
    run_generator ["notifier"]
    assert_file "app/views/notifier"
    assert_file "app/views/layouts/mailer.text.erb"
    assert_file "app/views/layouts/mailer.html.erb"
  end

  def test_logs_if_the_template_engine_cannot_be_found
    content = run_generator ["notifier", "foo", "bar", "--template-engine=haml"]
    assert_match(/haml \[not found\]/, content)
  end

  def test_mailer_with_namedspaced_mailer
    run_generator ["Farm::Animal", "moos"]
    assert_file "app/mailers/farm/animal.rb" do |mailer|
      assert_match(/class Farm::Animal < ApplicationMailer/, mailer)
      assert_match(/en\.farm\.animal\.moos\.subject/, mailer)
    end
    assert_file "test/mailers/previews/farm/animal_preview.rb" do |preview|
      assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal/, preview)
      assert_match(/class Farm::AnimalPreview < ActionMailer::Preview/, preview)
      assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal\/moos/, preview)
    end
    assert_file "app/views/farm/animal/moos.text.erb"
    assert_file "app/views/farm/animal/moos.html.erb"
  end

  def test_actions_are_turned_into_methods
    run_generator

    assert_file "app/mailers/notifier.rb" do |mailer|
      assert_instance_method :foo, mailer do |foo|
        assert_match(/mail to: "to@example.org"/, foo)
        assert_match(/@greeting = "Hi"/, foo)
      end

      assert_instance_method :bar, mailer do |bar|
        assert_match(/mail to: "to@example.org"/, bar)
        assert_match(/@greeting = "Hi"/, bar)
      end
    end
  end

  def test_mailer_on_revoke
    run_generator
    run_generator ["notifier"], behavior: :revoke

    assert_no_file "app/mailers/notifier.rb"
    assert_no_file "app/views/notifier/foo.text.erb"
    assert_no_file "app/views/notifier/bar.text.erb"
    assert_no_file "app/views/notifier/foo.html.erb"
    assert_no_file "app/views/notifier/bar.html.erb"

    assert_file "app/mailers/application_mailer.rb"
    assert_file "app/views/layouts/mailer.text.erb"
    assert_file "app/views/layouts/mailer.html.erb"
  end
end