33require "spec_helper"
44
55class ExampleMailer < Supermail ::Rails ::Base
6- def initialize ( to :, from :, subject :, body :)
6+ def initialize ( to :, from :, subject :, body :, cc : [ ] , bcc : [ ] )
77 @to = to
88 @from = from
99 @subject = subject
1010 @body = body
11+ @cc = cc
12+ @bcc = bcc
1113 end
1214
13- attr_reader :to , :from , :subject , :body
15+ attr_reader :to , :from , :subject , :body , :cc , :bcc
1416end
1517
1618RSpec . describe ExampleMailer do
1719 let ( :email ) { described_class . new (
1820 to : "user@example.com" ,
1921 from : "support@example.com" ,
2022 subject : "Hello" ,
21- body : "Hi there" )
23+ body : "Hi there" ,
24+ cc : [ "cc@example.com" ] ,
25+ bcc : [ "bcc@example.com" ] )
2226 }
2327 let ( :message ) { email . message }
2428 subject { message }
@@ -44,127 +48,14 @@ def initialize(to:, from:, subject:, body:)
4448 end
4549
4650 describe "#mailto" do
47- let ( :full_mailer_class ) do
48- Class . new ( Supermail ::Rails ::Base ) do
49- def initialize ( to :, from :, subject :, body :, cc :, bcc :)
50- @to = to
51- @from = from
52- @subject = subject
53- @body = body
54- @cc = cc
55- @bcc = bcc
56- end
57-
58- attr_reader :to , :from , :subject , :body , :cc , :bcc
59- end
60- end
61-
62- let ( :full_mailer ) do
63- full_mailer_class . new (
64- to : "recipient@example.com" ,
65- from : "sender@example.com" ,
66- subject : "Test Subject" ,
67- body : "Test body content" ,
68- cc : [ "cc@example.com" ] ,
69- bcc : [ "bcc@example.com" ]
70- )
71- end
72-
7351 it "passes all mail fields to the mailto URL" do
74- result = full_mailer . mailto
75- expect ( result ) . to start_with ( "mailto:recipient @example.com?" )
76- expect ( result ) . to include ( "from=sender %40example.com" )
77- expect ( result ) . to include ( "subject=Test%20Subject " )
78- expect ( result ) . to include ( "body=Test%20body%20content " )
52+ result = email . mailto
53+ expect ( result ) . to start_with ( "mailto:user @example.com?" )
54+ expect ( result ) . to include ( "from=support %40example.com" )
55+ expect ( result ) . to include ( "subject=Hello " )
56+ expect ( result ) . to include ( "body=Hi%20there " )
7957 expect ( result ) . to include ( "cc=%5B%22cc%40example.com%22%5D" )
8058 expect ( result ) . to include ( "bcc=%5B%22bcc%40example.com%22%5D" )
8159 end
8260 end
83-
84- describe "with minimal configuration" do
85- let ( :minimal_mailer_class ) do
86- Class . new ( Supermail ::Rails ::Base ) do
87- def to = "test@example.com"
88- def from = nil
89- def subject = nil
90- def body = ""
91- def cc = [ ]
92- def bcc = [ ]
93- end
94- end
95-
96- let ( :minimal_mailer ) { minimal_mailer_class . new }
97-
98- it "generates a simple mailto URL when optional parameters are nil or empty" do
99- result = minimal_mailer . mailto
100- expect ( result ) . to eq ( "mailto:test@example.com?body=" )
101- end
102- end
103-
104- describe "with special characters" do
105- let ( :special_mailer_class ) do
106- Class . new ( Supermail ::Rails ::Base ) do
107- def to = "test+tag@example.com"
108- def from = "sender@example.com"
109- def subject = "Hello & Welcome! 🎉"
110- def body = "Line 1\n Line 2\n \n Best regards"
111- def cc = [ ]
112- def bcc = [ ]
113- end
114- end
115-
116- let ( :special_mailer ) { special_mailer_class . new }
117-
118- it "properly escapes special characters and unicode" do
119- result = special_mailer . mailto
120- expect ( result ) . to include ( "mailto:test+tag@example.com?" )
121- expect ( result ) . to include ( "subject=Hello%20%26%20Welcome%21" )
122- expect ( result ) . to include ( "body=Line%201%0ALine%202%0A%0ABest%20regards" )
123- end
124- end
125-
126- describe "with empty parameters" do
127- let ( :empty_mailer_class ) do
128- Class . new ( Supermail ::Rails ::Base ) do
129- def to = "test@example.com"
130- def from = ""
131- def subject = ""
132- def body = ""
133- def cc = [ ]
134- def bcc = [ ]
135- end
136- end
137-
138- let ( :empty_mailer ) { empty_mailer_class . new }
139-
140- it "includes empty string parameters but excludes empty arrays in the mailto URL" do
141- result = empty_mailer . mailto
142- expect ( result ) . to include ( "from=" )
143- expect ( result ) . to include ( "subject=" )
144- expect ( result ) . to include ( "body=" )
145- expect ( result ) . not_to include ( "cc=" )
146- expect ( result ) . not_to include ( "bcc=" )
147- end
148- end
149-
150- describe "with mixed array and string recipients" do
151- let ( :mixed_mailer_class ) do
152- Class . new ( Supermail ::Rails ::Base ) do
153- def to = "primary@example.com"
154- def from = "sender@example.com"
155- def subject = "Mixed Recipients"
156- def body = "Test message"
157- def cc = "single-cc@example.com"
158- def bcc = [ "bcc1@example.com" , "bcc2@example.com" ]
159- end
160- end
161-
162- let ( :mixed_mailer ) { mixed_mailer_class . new }
163-
164- it "handles both string and array recipients correctly" do
165- result = mixed_mailer . mailto
166- expect ( result ) . to include ( "cc=single-cc%40example.com" )
167- expect ( result ) . to include ( "bcc=%5B%22bcc1%40example.com%22%2C%20%22bcc2%40example.com%22%5D" )
168- end
169- end
17061end
0 commit comments