Skip to content

Commit f12ad32

Browse files
Moved definition of safe_add/subtract_days so they can be used in other tests, used them to fix test failing intermitently on the 14th
1 parent 83f0ad7 commit f12ad32

5 files changed

Lines changed: 31 additions & 29 deletions

File tree

app/models/organization.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def reminder_schedule_is_empty_or_valid?
269269
end
270270
if reminder_schedule.by_month_or_week == "day_of_month" && reminder_schedule.day_of_month.to_i == deadline_day.to_i
271271
errors.add(:day_of_month, "Reminder day must not be the same as deadline day")
272-
end
272+
end
273273
end
274274
end
275275

app/models/partner_group.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def reminder_schedule_is_empty_or_valid?
5454
end
5555
if deadline_on_reminder_date?
5656
errors.add(:day_of_month, "Reminder day must not be the same as deadline day")
57-
end
57+
end
5858
end
5959
end
6060

@@ -64,7 +64,7 @@ def reminder_schedule_present?
6464
end
6565
if deadline_on_reminder_date?
6666
errors.add(:day_of_month, "Reminder day must not be the same as deadline day")
67-
end
67+
end
6868
end
6969

7070
def deadline_on_reminder_date?

spec/support/deadline_day_fields_shared_example.rb

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
# The reminder day (the #{form_prefix}_reminder_schedule_service_day_of_month field ) has to be less than or equal to 28.
2+
# These functions are implemented to calculate dates prior or after a given date that do not fall on a
3+
# date with a day greater than 28.
4+
# It is recommended to use these functions to calculate, from now, inputs for the reminder day and deadline day fields if
5+
# your test cares about the text created by the deadline_day_controller.js controller as there isn't an easy way to spoof
6+
# the current time in the test browser and different behavior could occur if the test is run on different days.
7+
def safe_add_days(date, num)
8+
result = date + num.days
9+
if result.day > 28
10+
result = result.change({day: 1 + num})
11+
result += 1.month
12+
end
13+
result
14+
end
15+
16+
def safe_subtract_days(date, num)
17+
result = date - num.days
18+
if result.day > 28
19+
result = result.change({day: 28 - num})
20+
result -= 1.month
21+
end
22+
result
23+
end
24+
125
RSpec.shared_examples_for "deadline and reminder form" do |form_prefix, save_button, post_form_submit|
226
it "can set a reminder on a day of the month" do
327
choose "Day of Month"
@@ -34,7 +58,6 @@
3458
expect(page).to_not have_content("Your next reminder date is")
3559
expect(page).to_not have_content("The deadline on your next reminder email will be")
3660

37-
3861
fill_in "#{form_prefix}_reminder_schedule_service_day_of_month", with: "-1"
3962
expect(page).to have_content("Reminder day must be between 1 and 28")
4063
fill_in "#{form_prefix}_reminder_schedule_service_day_of_month", with: "20"
@@ -52,27 +75,6 @@
5275
end
5376

5477
describe "reported reminder and deadline dates" do
55-
# The reminder day (the #{form_prefix}_reminder_schedule_service_day_of_month field ) has to be less than or equal to 28.
56-
# These functions are implemented to calculate dates prior or after @now that do not fall on a
57-
# date with a day greater than 28.
58-
def safe_add_days(date, num)
59-
result = date + num.days
60-
if result.day > 28
61-
result = result.change({day: 1 + num})
62-
result += 1.month
63-
end
64-
result
65-
end
66-
67-
def safe_subtract_days(date, num)
68-
result = date - num.days
69-
if result.day > 28
70-
result = result.change({day: 28 - num})
71-
result -= 1.month
72-
end
73-
result
74-
end
75-
7678
context "when the reminder is a day of the month" do
7779
before do
7880
choose "Day of Month"

spec/system/organization_system_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def post_form_submit
7575

7676
it "the deadline day form's reminder and deadline dates are consistent with the dates calculated by the FetchPartnersToRemindNowService and DeadlineService" do
7777
choose "Day of Month"
78-
fill_in "organization_reminder_schedule_service_day_of_month", with: 14
79-
fill_in "Deadline day in reminder email", with: 21
78+
fill_in "organization_reminder_schedule_service_day_of_month", with: safe_add_days(Time.zone.now, 1).day
79+
fill_in "Deadline day in reminder email", with: safe_add_days(Time.zone.now, 2).day
8080

8181
reminder_text = find('small[data-deadline-day-target="reminderText"]').text
8282
reminder_text.slice!("Your next reminder date is ")

spec/system/partner_system_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,8 @@
721721

722722
it "the deadline day form's reminder and deadline dates are consistent with the dates calculated by the FetchPartnersToRemindNowService and DeadlineService" do
723723
choose "Day of Month"
724-
fill_in "partner_group_reminder_schedule_service_day_of_month", with: 14
725-
fill_in "Deadline day in reminder email", with: 21
724+
fill_in "partner_group_reminder_schedule_service_day_of_month", with: safe_add_days(Time.zone.now, 1).day
725+
fill_in "Deadline day in reminder email", with: safe_add_days(Time.zone.now, 2).day
726726

727727
reminder_text = find('small[data-deadline-day-target="reminderText"]').text
728728
reminder_text.slice!("Your next reminder date is ")

0 commit comments

Comments
 (0)