11require " ./spec_helper"
2+ require " ./support/email_with_sendgrid_features"
23
34describe Carbon ::SendGridAdapter do
45 {% if flag?(" with-integration" ) % }
@@ -17,10 +18,10 @@ describe Carbon::SendGridAdapter do
1718 {% end % }
1819
1920 describe " errors" do
20- it " raises SendGridInvalidTemplateError if no template is defined in params " do
21+ it " raises SendGridInvalidTemplateError if no template is defined" do
2122 expect_raises(Carbon ::SendGridInvalidTemplateError ) do
2223 email = FakeEmail .new
23- Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).params
24+ Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
2425 end
2526 end
2627 end
@@ -111,24 +112,24 @@ describe Carbon::SendGridAdapter do
111112 end
112113
113114 it " sets the content" do
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 [
115+ sendgrid_options_for (text_body: " text" )[" content" ].should eq JSON .parse( [{" type" => " text/plain" , " value" => " text" }].to_json)
116+ sendgrid_options_for (html_body: " html" )[" content" ].should eq JSON .parse( [{" type" => " text/html" , " value" => " html" }].to_json)
117+ sendgrid_options_for (text_body: " text" , html_body: " html" )[" content" ].should eq JSON .parse( [
117118 {" type" => " text/plain" , " value" => " text" },
118119 {" type" => " text/html" , " value" => " html" },
119- ]
120+ ].to_json)
120121 end
121122
122123 it " allows for a custom template_id" do
123124 custom_email = CustomTemplateEmail .new
124- params = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).params
125+ options = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).sendgrid_options
125126
126- params [" template_id" ].should eq(" welcome-abc-123" )
127+ options [" template_id" ].should eq(JSON :: Any .new( " welcome-abc-123" ) )
127128
128129 normal_email = FakeEmail .new(text_body: " 0" )
129- params = Carbon ::SendGridAdapter ::Email .new(normal_email, api_key: " fake_key" ).params
130+ options = Carbon ::SendGridAdapter ::Email .new(normal_email, api_key: " fake_key" ).sendgrid_options
130131
131- params .has_key?(" template_id" ).should eq(false )
132+ options .has_key?(" template_id" ).should eq(false )
132133 end
133134
134135 it " allows for custom template data" do
@@ -145,14 +146,14 @@ describe Carbon::SendGridAdapter do
145146
146147 it " passes over asm data on how to handle unsubscribes" do
147148 custom_email = CustomTemplateEmail .new
148- params = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).params
149+ options = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).sendgrid_options
149150
150- params[ " personalizations " ].as( Array ).first[ " dynamic_template_data " ].should_not eq( nil )
151+ options[ " asm " ].should eq( JSON .parse({ " group_id " => 1234 , " groups_to_display " => [ 1234 ]}.to_json) )
151152
152153 email = FakeEmail .new(text_body: " 0" )
153- params = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).params
154+ options = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
154155
155- params[ " personalizations " ].as( Array ).first .has_key?(" asm" ).should eq(false )
156+ options .has_key?(" asm" ).should eq(false )
156157 end
157158
158159 it " handles attachments" do
@@ -163,6 +164,65 @@ describe Carbon::SendGridAdapter do
163164 attachments.first[" filename" ].should eq(" contract.pdf" )
164165 Base64 .decode_string(attachments.first[" content" ].to_s).should eq(" Sign here" )
165166 end
167+
168+ describe " sendgrid specific features" do
169+ it " includes categories in sendgrid_options when defined" do
170+ email = EmailWithSendGridFeatures .new(text_body: " 0" )
171+ options = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
172+
173+ options[" categories" ].should eq(JSON .parse([" welcome" , " onboarding" , " transactional" ].to_json))
174+ end
175+
176+ it " does not include categories when not defined" do
177+ email = FakeEmail .new(text_body: " 0" )
178+ options = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
179+
180+ options.has_key?(" categories" ).should eq(false )
181+ end
182+
183+ it " includes send_at in sendgrid_options when defined" do
184+ email = EmailWithSendGridFeatures .new(text_body: " 0" )
185+ options = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
186+
187+ options[" send_at" ].should eq(JSON ::Any .new(1704067200 _i64 ))
188+ end
189+
190+ it " does not include send_at when not defined" do
191+ email = FakeEmail .new(text_body: " 0" )
192+ options = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
193+
194+ options.has_key?(" send_at" ).should eq(false )
195+ end
196+
197+ it " includes asm in sendgrid_options when defined" do
198+ custom_email = CustomTemplateEmail .new
199+ options = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).sendgrid_options
200+
201+ options[" asm" ].should eq(JSON .parse({" group_id" => 1234 , " groups_to_display" => [1234 ]}.to_json))
202+ end
203+
204+ it " does not include asm when not defined" do
205+ email = FakeEmail .new(text_body: " 0" )
206+ options = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
207+
208+ options.has_key?(" asm" ).should eq(false )
209+ end
210+
211+ it " includes template_id in sendgrid_options when defined" do
212+ custom_email = CustomTemplateEmail .new
213+ options = Carbon ::SendGridAdapter ::Email .new(custom_email, api_key: " fake_key" ).sendgrid_options
214+
215+ options[" template_id" ].should eq(JSON ::Any .new(" welcome-abc-123" ))
216+ end
217+
218+ it " includes content in sendgrid_options when template_id not defined" do
219+ email = FakeEmail .new(text_body: " hello" )
220+ options = Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
221+
222+ options[" content" ].should eq(JSON .parse([{" type" => " text/plain" , " value" => " hello" }].to_json))
223+ options.has_key?(" template_id" ).should eq(false )
224+ end
225+ end
166226 end
167227end
168228
@@ -171,6 +231,11 @@ private def params_for(**email_attrs)
171231 Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).params
172232end
173233
234+ private def sendgrid_options_for (** email_attrs)
235+ email = FakeEmail .new(** email_attrs)
236+ Carbon ::SendGridAdapter ::Email .new(email, api_key: " fake_key" ).sendgrid_options
237+ end
238+
174239private def send_email_to_send_grid (** email_attrs)
175240 api_key = ENV .fetch(" SEND_GRID_API_KEY" )
176241 email = FakeEmail .new(** email_attrs)
0 commit comments