Split duration component in two
We were displaying dates in two different formats in the same component, leading to strange hacks like manually calling the `call` method or not being able to use `render_inline` in the tests. Since we're going to reuse one of these formats outside the budgets section, we're splitting the component. We're also removing the mentioned hacks.
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Admin::Budgets::DurationComponent do
|
||||
describe "#dates" do
|
||||
it "shows both dates when both are defined" do
|
||||
durable = double(
|
||||
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
|
||||
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
|
||||
)
|
||||
|
||||
dates = Admin::Budgets::DurationComponent.new(durable).dates
|
||||
|
||||
render dates
|
||||
|
||||
expect(page.text).to eq "2015-08-01 12:00 - 2016-09-30 16:29"
|
||||
expect(dates).to be_html_safe
|
||||
end
|
||||
|
||||
it "shows the start date when no end date is defined" do
|
||||
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
|
||||
render Admin::Budgets::DurationComponent.new(durable).dates
|
||||
|
||||
expect(page.text).to eq "2015-08-01 12:00 - "
|
||||
end
|
||||
|
||||
it "shows the end date when no start date is defined" do
|
||||
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
|
||||
|
||||
render Admin::Budgets::DurationComponent.new(durable).dates
|
||||
|
||||
expect(page.text).to eq "- 2016-09-30 16:29"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#duration" do
|
||||
it "describes the total duration in human language" do
|
||||
durable = double(
|
||||
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
|
||||
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
|
||||
)
|
||||
|
||||
render Admin::Budgets::DurationComponent.new(durable).duration
|
||||
|
||||
expect(page.text).to eq "about 1 year"
|
||||
end
|
||||
|
||||
it "is not defined when no end date is defined" do
|
||||
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
|
||||
|
||||
render Admin::Budgets::DurationComponent.new(durable).duration
|
||||
|
||||
expect(page).not_to be_rendered
|
||||
end
|
||||
|
||||
it "is not defined when no start date is defined" do
|
||||
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
|
||||
|
||||
render Admin::Budgets::DurationComponent.new(durable).duration
|
||||
|
||||
expect(page).not_to be_rendered
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :content
|
||||
|
||||
def render(content)
|
||||
@content = content
|
||||
end
|
||||
|
||||
def page
|
||||
Capybara::Node::Simple.new(content.to_s)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,30 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Admin::Budgets::DurationInWordsComponent do
|
||||
it "describes the total duration in human language" do
|
||||
durable = double(
|
||||
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
|
||||
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
|
||||
)
|
||||
|
||||
render_inline Admin::Budgets::DurationInWordsComponent.new(durable)
|
||||
|
||||
expect(page.text).to eq "about 1 year"
|
||||
end
|
||||
|
||||
it "is not defined when no end date is defined" do
|
||||
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
|
||||
|
||||
render_inline Admin::Budgets::DurationInWordsComponent.new(durable)
|
||||
|
||||
expect(page).not_to be_rendered
|
||||
end
|
||||
|
||||
it "is not defined when no start date is defined" do
|
||||
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
|
||||
|
||||
render_inline Admin::Budgets::DurationInWordsComponent.new(durable)
|
||||
|
||||
expect(page).not_to be_rendered
|
||||
end
|
||||
end
|
||||
32
spec/components/admin/duration_component_spec.rb
Normal file
32
spec/components/admin/duration_component_spec.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Admin::DurationComponent do
|
||||
it "shows both dates when both are defined" do
|
||||
durable = double(
|
||||
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
|
||||
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
|
||||
)
|
||||
|
||||
render_inline Admin::DurationComponent.new(durable)
|
||||
|
||||
expect(page.text).to eq "2015-08-01 12:00 - 2016-09-30 16:29"
|
||||
expect(page).to have_css "time", exact_text: "2015-08-01 12:00"
|
||||
expect(page).to have_css "time", exact_text: "2016-09-30 16:29"
|
||||
end
|
||||
|
||||
it "shows the start date when no end date is defined" do
|
||||
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
|
||||
|
||||
render_inline Admin::DurationComponent.new(durable)
|
||||
|
||||
expect(page.text).to eq "2015-08-01 12:00 - "
|
||||
end
|
||||
|
||||
it "shows the end date when no start date is defined" do
|
||||
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
|
||||
|
||||
render_inline Admin::DurationComponent.new(durable)
|
||||
|
||||
expect(page.text).to eq "- 2016-09-30 16:29"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user