REFACTOR: improves send_reminder spec

REFACTOR: improves send_reminder spec

diff --git a/spec/jobs/regular/discourse_post_event/send_reminder_spec.rb b/spec/jobs/regular/discourse_post_event/send_reminder_spec.rb
index 7658076..c701e9c 100644
--- a/spec/jobs/regular/discourse_post_event/send_reminder_spec.rb
+++ b/spec/jobs/regular/discourse_post_event/send_reminder_spec.rb
@@ -4,19 +4,24 @@ require 'rails_helper'
 require_relative '../../../fabricators/event_fabricator'
 
 describe Jobs::DiscoursePostEventSendReminder do
+  Invitee ||= DiscoursePostEvent::Invitee
+
   let(:admin_1) { Fabricate(:user, admin: true) }
-  let(:topic_1) { Fabricate(:topic, user: admin_1) }
   let(:going_user) { Fabricate(:user) }
   let(:visited_going_user) { Fabricate(:user) }
   let(:not_going_user) { Fabricate(:user) }
   let(:going_user_unread_notification) { Fabricate(:user) }
   let(:going_user_read_notification) { Fabricate(:user) }
-  let(:post_1) { Fabricate(:post, topic: topic_1) }
+  let(:post_1) { Fabricate(:post) }
   let(:reminders) { '-5.minutes' }
-  let(:event_1) { Fabricate(:event, post: post_1, reminders: reminders) }
 
-  # ensures we start from a known state
-  def init_notifications
+  def init_invitees
+    Invitee.create_attendance!(going_user.id, event_1.id, :going)
+    Invitee.create_attendance!(not_going_user.id, event_1.id, :not_going)
+    Invitee.create_attendance!(going_user_unread_notification.id, event_1.id, :going)
+    Invitee.create_attendance!(going_user_read_notification.id, event_1.id, :going)
+    Invitee.create_attendance!(visited_going_user.id, event_1.id, :going)
+
     [
       going_user,
       not_going_user,
@@ -37,37 +42,32 @@ describe Jobs::DiscoursePostEventSendReminder do
 
   before do
     freeze_time DateTime.parse('2018-11-10 12:00')
+
     Jobs.run_immediately!
+
     SiteSetting.calendar_enabled = true
     SiteSetting.discourse_post_event_enabled = true
-
-    DiscoursePostEvent::Invitee.create_attendance!(going_user.id, event_1.id, :going)
-    DiscoursePostEvent::Invitee.create_attendance!(not_going_user.id, event_1.id, :not_going)
-    DiscoursePostEvent::Invitee.create_attendance!(going_user_unread_notification.id, event_1.id, :going)
-    DiscoursePostEvent::Invitee.create_attendance!(going_user_read_notification.id, event_1.id, :going)
-    DiscoursePostEvent::Invitee.create_attendance!(visited_going_user.id, event_1.id, :going)
   end
 
   context '#execute' do
     context 'invalid params' do
-      context 'no invitees given' do
-        it 'raises an invalid parameters errors' do
-          expect {
-            subject.execute(event_id: 1)
-          }.to raise_error(Discourse::InvalidParameters)
-
-          expect {
-            subject.execute(reminder: 'foo')
-          }.to raise_error(Discourse::InvalidParameters)
-        end
+      it 'raises an invalid parameters errors' do
+        expect {
+          subject.execute(event_id: 1)
+        }.to raise_error(Discourse::InvalidParameters)
+
+        expect {
+          subject.execute(reminder: 'foo')
+        }.to raise_error(Discourse::InvalidParameters)
       end
     end
 
-    context 'valid params' do
+    context 'public event' do
       context 'event has not started' do
+        let(:event_1) { Fabricate(:event, post: post_1, reminders: reminders, starts_at: 3.hours.from_now) }
+
         before do
-          event_1.update!(starts_at: Time.now + 1.hour)
-          init_notifications
+          init_invitees
         end
 
         it 'creates a new notification for going user' do
@@ -96,16 +96,17 @@ describe Jobs::DiscoursePostEventSendReminder do
       end
 
       context 'event has started' do
+        let(:event_1) { Fabricate(:event, post: post_1, reminders: reminders, starts_at: 3.hours.ago) }
+
         before do
-          event_1.update!(starts_at: 4.minutes.ago)
-          init_notifications
+          init_invitees
 
-          TopicUser.change(going_user, event_1.post.topic, last_visited_at: 10.minutes.ago, last_read_post_number: 1)
+          TopicUser.change(going_user, event_1.post.topic, last_visited_at: 4.hours.ago, last_read_post_number: 1)
           TopicUser.change(visited_going_user, event_1.post.topic, last_visited_at: 2.minutes.ago, last_read_post_number: 1)
         end
 
         it 'creates a new notification for going user' do
-          expect(going_user.unread_notifications).to eq(0)
+          expect(going_user.reload.unread_notifications).to eq(0)
 
           expect {
             subject.execute(event_id: event_1.id, reminder: reminders)

GitHub sha: aef07d8c

1 Like