aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/test/mail_helper_test.rb
blob: 2002d3b6363f366445aa93814653aa9cf145a185 (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
require "abstract_unit"

class HelperMailer < ActionMailer::Base
  def use_mail_helper
    @text = "But soft! What light through yonder window breaks? It is the east, " +
            "and Juliet is the sun. Arise, fair sun, and kill the envious moon, " +
            "which is sick and pale with grief that thou, her maid, art far more " +
            "fair than she. Be not her maid, for she is envious! Her vestal " +
            "livery is but sick and green, and none but fools do wear it. Cast " +
            "it off!"

    mail_with_defaults do |format|
      format.html { render(inline: "<%= block_format @text %>") }
    end
  end

  def use_format_paragraph
    @text = "But soft! What light through yonder window breaks?"

    mail_with_defaults do |format|
      format.html { render(inline: "<%= format_paragraph @text, 15, 1 %>") }
    end
  end

  def use_format_paragraph_with_long_first_word
    @text = "Antidisestablishmentarianism is very long."

    mail_with_defaults do |format|
      format.html { render(inline: "<%= format_paragraph @text, 10, 1 %>") }
    end
  end

  def use_mailer
    mail_with_defaults do |format|
      format.html { render(inline: "<%= mailer.message.subject %>") }
    end
  end

  def use_message
    mail_with_defaults do |format|
      format.html { render(inline: "<%= message.subject %>") }
    end
  end

  def use_block_format
    @text = <<-TEXT
This is the
first     paragraph.

The second
   paragraph.

* item1 * item2
  * item3
    TEXT

    mail_with_defaults do |format|
      format.html { render(inline: "<%= block_format @text %>") }
    end
  end

  def use_cache
    mail_with_defaults do |format|
      format.html { render(inline: "<% cache(:foo) do %>Greetings from a cache helper block<% end %>") }
    end
  end

  protected

  def mail_with_defaults(&block)
    mail(to: "test@localhost", from: "tester@example.com",
          subject: "using helpers", &block)
  end
end

class MailerHelperTest < ActionMailer::TestCase
  def test_use_mail_helper
    mail = HelperMailer.use_mail_helper
    assert_match %r{  But soft!}, mail.body.encoded
    assert_match %r{east, and\r\n  Juliet}, mail.body.encoded
  end

  def test_use_mailer
    mail = HelperMailer.use_mailer
    assert_match "using helpers", mail.body.encoded
  end

  def test_use_message
    mail = HelperMailer.use_message
    assert_match "using helpers", mail.body.encoded
  end

  def test_use_format_paragraph
    mail = HelperMailer.use_format_paragraph
    assert_match " But soft! What\r\n light through\r\n yonder window\r\n breaks?", mail.body.encoded
  end

  def test_use_format_paragraph_with_long_first_word
    mail = HelperMailer.use_format_paragraph_with_long_first_word
    assert_equal " Antidisestablishmentarianism\r\n is very\r\n long.", mail.body.encoded
  end

  def test_use_block_format
    mail = HelperMailer.use_block_format
    expected = <<-TEXT
  This is the first paragraph.

  The second paragraph.

  * item1
  * item2
  * item3
    TEXT
    assert_equal expected.gsub("\n", "\r\n"), mail.body.encoded
  end

  def test_use_cache
    assert_nothing_raised do
      mail = HelperMailer.use_cache
      assert_equal "Greetings from a cache helper block", mail.body.encoded
    end
  end
end