Skip to content

Commit 3c581c4

Browse files
authored
Spec/converstaion mailer (#918)
- Updates the message factory to include generated body text, and the association to a conversation. - Adds multiple expect blocks to the conversation_mailer_spec.rb to test the rendered headers and body content. - Fixes the conversation_mailer_preview.rb to properly render a dynamically generated message notification email.
2 parents 16522f0 + d8fd038 commit 3c581c4

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# frozen_string_literal: true
22

33
FactoryBot.define do
4-
factory :message do
5-
content { 'MyText' }
4+
factory('better-together/message',
5+
class: 'BetterTogether::Message',
6+
aliases: %i[better_together_message message]) do
7+
content { Faker::Lorem.paragraph }
8+
association :sender, factory: :person
9+
association :conversation
610
end
711
end

spec/mailers/better_together/conversation_mailer_spec.rb

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,40 @@
33
require 'rails_helper'
44

55
module BetterTogether
6-
RSpec.describe ConversationMailer, type: :mailer do
7-
# describe 'new_message_notification' do
8-
# let(:mail) { ConversationMailer.new_message_notification }
6+
RSpec.describe ConversationMailer, type: :mailer do # rubocop:todo Metrics/BlockLength
7+
describe 'new_message_notification' do # rubocop:todo Metrics/BlockLength
8+
let!(:host_platform) { create(:platform, :host) }
9+
let(:sender) { create(:user) }
10+
let(:recipient) { create(:user) }
11+
let(:conversation) { create(:conversation, creator: sender.person) }
12+
let(:message) { create(:message, conversation: conversation, sender: sender.person) }
913

10-
# it 'renders the headers' do
11-
# expect(mail.subject).to eq('New message notification')
12-
# expect(mail.to).to eq(['[email protected]'])
13-
# expect(mail.from).to eq(['[email protected]'])
14-
# end
14+
let(:mail) do
15+
ConversationMailer.with(message: message, recipient: recipient.person)
16+
.new_message_notification
17+
end
1518

16-
# it 'renders the body' do
17-
# expect(mail.body.encoded).to match('Hi')
18-
# end
19-
# end
19+
it 'renders the headers' do
20+
expect(mail.subject).to eq("[#{host_platform.name}] New message in conversation \"#{conversation.title}\"")
21+
expect(mail.to).to eq([recipient.email])
22+
expect(mail.from).to eq(['[email protected]'])
23+
end
24+
25+
it 'renders the body' do
26+
expect(mail.body.encoded).to have_content("Hello #{recipient.person.name}")
27+
expect(mail.body.encoded).to have_content("#{sender.person.name}:")
28+
expect(mail.body.encoded).to have_content(message.content.to_plain_text)
29+
end
30+
31+
it 'sends a message notification email' do
32+
expect { mail.deliver_now }
33+
.to change { ActionMailer::Base.deliveries.count }.by(1)
34+
end
35+
36+
it 'sends the message notification to the correct email address' do
37+
mail.deliver_now
38+
expect(ActionMailer::Base.deliveries.last.to).to include(recipient.email)
39+
end
40+
end
2041
end
2142
end
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
# frozen_string_literal: true
22

33
module BetterTogether
4-
# Preview all emails at http://localhost:3000/rails/mailers/better_together/conversation_mailer_mailer
4+
# Preview all emails at http://localhost:3000/rails/mailers/better_together/conversation_mailer
55
class ConversationMailerPreview < ActionMailer::Preview
6-
# Preview this email at http://localhost:3000/rails/mailers/better_together/conversation_mailer_mailer/new_message_notification
6+
include FactoryBot::Syntax::Methods
7+
include BetterTogether::ApplicationHelper
8+
9+
# Preview this email at http://localhost:3000/rails/mailers/better_together/conversation_mailer/new_message_notification
710
def new_message_notification
8-
ConversationMailer.new_message_notification
11+
host_platform || create(:platform)
12+
sender = create(:user)
13+
recipient = create(:user)
14+
conversation = create(:conversation, creator: sender.person)
15+
message = create(:message, conversation: conversation, sender: sender.person)
16+
17+
ConversationMailer.with(message: message, recipient: recipient.person)
18+
.new_message_notification
919
end
1020
end
1121
end

0 commit comments

Comments
 (0)