Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Return a default description when it is empty #437

Merged
merged 1 commit into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/services/push_notifications/creator/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ def title
end

def body
event.post.description&.truncate(20) || 'No description'
description = event.post.description

if description.blank?
'No description'
else
description.truncate(20)
end
end

def data
Expand Down
69 changes: 65 additions & 4 deletions spec/services/push_notifications/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,71 @@
end

describe '#create!' do
it 'creates as many PushNotification resources as needed' do
expect {
creator.create!
}.to change{PushNotification.count}.by(1)
context 'integration' do
it 'creates as many PushNotification resources as needed' do
expect { creator.create! }.to change { PushNotification.count }.by(1)
end
Copy link
Collaborator Author

@sauloperez sauloperez Sep 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing only this was very naive IMO.

end

context 'unit' do
let(:post) do
Fabricate(
:post,
organization: organization,
user: user,
description: description
)
end

before { allow(PushNotification).to receive(:create!) }

context 'when the post description is empty' do
let(:description) { '' }

it 'creates a PushNotification with a default body' do
creator.create!

expect(PushNotification)
.to have_received(:create!)
.with(include(body: 'No description'))
end
end

context 'when the post description is nil' do
let(:description) { nil }

it 'creates a PushNotification with a default body' do
creator.create!

expect(PushNotification)
.to have_received(:create!)
.with(include(body: 'No description'))
end
end

context 'when the post description is shorter than 20 chars' do
let(:description) { 'description' }

it 'creates a PushNotification with the post body' do
creator.create!

expect(PushNotification)
.to have_received(:create!)
.with(include(body: post.description))
end
end

context 'when the post description is larger than 20 chars' do
let(:description) { 'this is a very long description' }

it 'creates a PushNotification with the post body truncated' do
creator.create!

expect(PushNotification)
.to have_received(:create!)
.with(include(body: post.description.truncate(20)))
end
end
end
end
end