@@ -16,32 +16,42 @@ describe Carbon::SendGridAdapter do
1616 end
1717 {% end % }
1818
19+ describe " errors" do
20+ it " raises SendGridInvalidTemplateError if no template is defined in params" do
21+ expect_raises(Carbon ::SendGridInvalidTemplateError ) do
22+ email = FakeEmail .new
23+ Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).params
24+ end
25+ end
26+ end
27+
1928 describe " params" do
2029 it " is not sandboxed by default" do
21- params_for[:mail_settings ][:sandbox_mode ][:enable ].should be_false
30+ settings = params_for(text_body: " 0" )[" mail_settings" ].as(NamedTuple )
31+ settings[:sandbox_mode ][:enable ].should be_false
2232 end
2333
2434 it " handles headers" do
2535 headers = {" Header1" => " value1" , " Header2" => " value2" }
26- params = params_for(headers: headers)
36+ params = params_for(headers: headers, text_body: " 0 " )
2737
28- params[: headers ].should eq headers
38+ params[" headers" ].should eq headers
2939 end
3040
3141 it " sets extracts reply-to header" do
3242 headers
= {
" reply-to" =>
" [email protected] " ,
" Header" =>
" value" }
33- params = params_for(headers: headers)
43+ params = params_for(headers: headers, text_body: " 0 " )
3444
35- params[: headers ].should eq({" Header" => " value" })
36- params[
: reply_to].should eq({
email: " [email protected] " })
45+ params[" headers" ].should eq({" Header" => " value" })
46+ params[
" reply_to" ].should eq({
" email" => " [email protected] " })
3747 end
3848
3949 it " sets extracts reply-to header regardless of case" do
4050 headers
= {
" Reply-To" =>
" [email protected] " ,
" Header" =>
" value" }
41- params = params_for(headers: headers)
51+ params = params_for(headers: headers, text_body: " 0 " )
4252
43- params[: headers ].should eq({" Header" => " value" })
44- params[
: reply_to].should eq({
email: " [email protected] " })
53+ params[" headers" ].should eq({" Header" => " value" })
54+ params[
" reply_to" ].should eq({
" email" => " [email protected] " })
4555 end
4656
4757 it " sets personalizations" do
@@ -55,58 +65,83 @@ describe Carbon::SendGridAdapter do
5565 recipient_params = params_for(
5666 to: [to_without_name, to_with_name],
5767 cc: [cc_without_name, cc_with_name],
58- bcc: [bcc_without_name, bcc_with_name]
59- )[:personalizations ].first
68+ bcc: [bcc_without_name, bcc_with_name],
69+ text_body: " 0"
70+ )[" personalizations" ].as(Array ).first
6071
61- recipient_params[:to ].should eq(
72+ recipient_params[" to " ].should eq(
6273 [
63- {
name: nil , email: " [email protected] " },
64- {
name: " Jimmy" ,
email: " [email protected] " },
74+ 75+ {
" name" => " Jimmy" ,
" email" => " [email protected] " },
6576 ]
6677 )
67- recipient_params[:cc ].should eq(
78+ recipient_params[" cc " ].should eq(
6879 [
69- {
name: nil , email: " [email protected] " },
70- {
name: " Kim" ,
email: " [email protected] " },
80+ 81+ {
" name" => " Kim" ,
" email" => " [email protected] " },
7182 ]
7283 )
73- recipient_params[: bcc ].should eq(
84+ recipient_params[" bcc" ].should eq(
7485 [
75- {
name: nil , email: " [email protected] " },
76- {
name: " James" ,
email: " [email protected] " },
86+ 87+ {
" name" => " James" ,
" email" => " [email protected] " },
7788 ]
7889 )
7990 end
8091
8192 it " removes empty recipients from personalizations" do
8293 to_without_name
= Carbon ::
Address .new(
" [email protected] " )
8394
84- recipient_params = params_for(to: [to_without_name])[ : personalizations] .first
95+ recipient_params = params_for(to: [to_without_name], text_body: " 0 " )[ " personalizations" ].as( Array ) .first
8596
86- recipient_params.keys.should eq [:to ]
87- recipient_params[
:to ].should eq [{
name: nil , email: " [email protected] " }]
97+ recipient_params.keys.should eq [" to " ]
98+ recipient_params[
" to " ].should eq [{
" email" => " [email protected] " }]
8899 end
89100
90101 it " sets the subject" do
91- params_for(subject: " My subject" )[ : subject ].should eq " My subject"
102+ params_for(subject: " My subject" , text_body: " 0 " )[ " subject" ].should eq " My subject"
92103 end
93104
94105 it " sets the from address" do
95106 address
= Carbon ::
Address .new(
" [email protected] " )
96- params_for(
from: address
)[ : from].should eq({
email: " [email protected] " }
.to_h )
107+ params_for(
from: address
, text_body: " 0 " )[ " from" ].should eq({
" email" => " [email protected] " })
97108
98109 address
= Carbon ::
Address .new(
" Sally" ,
" [email protected] " )
99- params_for(
from: address
)[ : from].should eq({
name: " Sally" ,
email: " [email protected] " }
.to_h )
110+ params_for(
from: address
, text_body: " 0 " )[ " from" ].should eq({
" name" => " Sally" ,
" email" => " [email protected] " })
100111 end
101112
102113 it " sets the content" do
103- params_for(text_body: " text" )[: content ].should eq [{type: " text/plain" , value: " text" }]
104- params_for(html_body: " html" )[: content ].should eq [{type: " text/html" , value: " html" }]
105- params_for(text_body: " text" , html_body: " html" )[: content ].should eq [
106- {type: " text/plain" , value: " text" },
107- {type: " text/html" , value: " html" },
114+ params_for(text_body: " text" )[" content" ].should eq [{" type" => " text/plain" , " value" => " text" }]
115+ params_for(html_body: " html" )[" content" ].should eq [{" type" => " text/html" , " value" => " html" }]
116+ params_for(text_body: " text" , html_body: " html" )[" content" ].should eq [
117+ {" type" => " text/plain" , " value" => " text" },
118+ {" type" => " text/html" , " value" => " html" },
108119 ]
109120 end
121+
122+ it " allows for a custom template_id" do
123+ custom_email = CustomTemplateEmail .new
124+ params = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).params
125+
126+ params[" template_id" ].should eq(" welcome-abc-123" )
127+
128+ normal_email = FakeEmail .new(text_body: " 0" )
129+ params = Carbon ::SendGridAdapter ::Email .new(normal_email, api_key: " fake_key" ).params
130+
131+ params.has_key?(" template_id" ).should eq(false )
132+ end
133+
134+ it " allows for custom template data" do
135+ custom_email = CustomTemplateEmail .new
136+ params = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).params
137+
138+ params[" personalizations" ].as(Array ).first[" dynamic_template_data" ].should_not eq(nil )
139+
140+ normal_email = FakeEmail .new(text_body: " 0" )
141+ params = Carbon ::SendGridAdapter ::Email .new(normal_email, api_key: " fake_key" ).params
142+
143+ params[" personalizations" ].as(Array ).first.has_key?(" dynamic_template_data" ).should eq(false )
144+ end
110145 end
111146end
112147
0 commit comments