From b94b1d67bd1fb350b5df6ef3e734a43648ed824c Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 27 Sep 2018 12:56:06 +0200 Subject: [PATCH] Return default description when description empty --- .../push_notifications/creator/post.rb | 8 ++- spec/services/push_notifications/post_spec.rb | 69 +++++++++++++++++-- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/app/services/push_notifications/creator/post.rb b/app/services/push_notifications/creator/post.rb index 4579aa961..66e047d6e 100644 --- a/app/services/push_notifications/creator/post.rb +++ b/app/services/push_notifications/creator/post.rb @@ -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 diff --git a/spec/services/push_notifications/post_spec.rb b/spec/services/push_notifications/post_spec.rb index 43d2b6165..d4b312818 100644 --- a/spec/services/push_notifications/post_spec.rb +++ b/spec/services/push_notifications/post_spec.rb @@ -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 + 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