-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily_schedule_spec.rb
64 lines (51 loc) · 2.41 KB
/
daily_schedule_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require "spec_helper"
RSpec.describe Automations::DailySchedule do
describe "#initialize" do
it "accepts days between 0 and 6" do
@schedule = Automations::DailySchedule.new({days: [1, 2], times: [10, 22]})
expect(@schedule.days).to eq([1, 2])
end
it "does not accept days outside 0 and 6" do
expect { Automations::DailySchedule.new({days: [-1, 2], times: [10, 22]}) }.to raise_error ArgumentError
expect { Automations::DailySchedule.new({days: [1, 7], times: [10, 22]}) }.to raise_error ArgumentError
end
it "accepts times between 0 and 23" do
@schedule = Automations::DailySchedule.new({days: [1, 2], times: [10, 22]})
expect(@schedule.times).to eq([10, 22])
end
it "does not accept times outside 0 and 23" do
expect { Automations::DailySchedule.new({days: [1, 2], times: [-1, 22]}) }.to raise_error ArgumentError
expect { Automations::DailySchedule.new({days: [1, 2], times: [10, 25]}) }.to raise_error ArgumentError
end
end
describe "#call" do
it "must be supplied with a time" do
@schedule = Automations::DailySchedule.new({days: [5], times: [11]})
expect(@schedule.call(some: "data")).to be false
end
it "is ready if the day and time match" do
@schedule = Automations::DailySchedule.new({days: [5], times: [11]})
Timecop.travel Time.new(2024, 9, 6, 11, 0) do # Friday, 6th September 2024, 11:00
expect(@schedule.call(time: Time.now)).to be true
end
end
it "is ready if the day matches and the time is within the following hour" do
@schedule = Automations::DailySchedule.new({days: [5], times: [11]})
Timecop.travel Time.new(2024, 9, 6, 11, 45) do # Friday, 6th September 2024, 11:45
expect(@schedule.call(time: Time.now)).to be true
end
end
it "is not ready if the day does not match and the time matches" do
@schedule = Automations::DailySchedule.new({days: [4], times: [11]})
Timecop.travel Time.new(2024, 9, 6, 11, 0) do # Friday, 6th September 2024, 11:00
expect(@schedule.call(time: Time.now)).to be false
end
end
it "is not ready if the day matches and the time does not match" do
@schedule = Automations::DailySchedule.new({days: [5], times: [12]})
Timecop.travel Time.new(2024, 9, 6, 11, 0) do # Friday, 6th September 2024, 11:00
expect(@schedule.call(time: Time.now)).to be false
end
end
end
end