8000 Rework date format specs · parse/activeadmin@1d5b4fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d5b4fd

Browse files
author
David Rodríguez
committed
Rework date format specs
1 parent fd7f8ec commit 1d5b4fd

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

spec/support/active_admin_integration_spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class MockActionView < ::ActionView::Base
5959
end
6060

6161
def with_translation(translation)
62+
# If no translations have been loaded, any later calls to `I18n.t` will
63+
# cause the full translation hash to be loaded, possibly overwritting what
64+
# we've loaded via `store_translations`. We use this hack to prevent that.
65+
# @todo Might not be necessary anymore once
66+
# https://github.com/svenfuchs/i18n/pull/353 lands.
67+
I18n.backend.send(:init_translations)
6268
I18n.backend.store_translations I18n.locale, translation
6369
yield
6470
ensure

spec/unit/pretty_format_spec.rb

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,49 @@ def method_missing(*args, &block)
1515
end
1616

1717
context "given a Date or a Time" do
18-
it "should return a localized Date or Time with long format" do
19-
t = Time.now
20-
expect(I18n).to receive(:localize).with(t, {format: :long}) { "Just Now!" }
21-
expect(pretty_format(t)).to eq "Just Now!"
18+
let(:t) { Time.utc(1985, "feb", 28, 20, 15, 1) }
19+
20+
it "formats it with the default long format" do
21+
expect(pretty_format(t)).to eq "February 28, 1985 20:15"
2222
end
2323

24-
context "actually do the formatting" do
25-
it "should actually do the formatting" do
26-
t = Time.utc(1985, "feb", 28, 20, 15, 1)
27-
expect(pretty_format(t)).to eq "February 28, 1985 20:15"
24+
it "formats it with a customized long format" do
25+
with_translation time: { formats: { long: "%B %d, %Y, %l:%M%P" } } do
26+
expect(pretty_format(t)).to eq "February 28, 1985, 8:15pm"
2827
end
28+
end
2929

30-
context "apply custom localize format" do
31-
around do |example|
32-
previous_localize_format = ActiveAdmin.application.localize_format
33-
ActiveAdmin.application.localize_format = :short
34-
example.call
35-
ActiveAdmin.application.localize_format = previous_localize_format
36-
end
37-
it "should actually do the formatting" do
38-
t = Time.utc(1985, "feb", 28, 20, 15, 1)
30+
context "with a custom localize format" do
31+
around do |example|
32+
previous_localize_format = ActiveAdmin.application.localize_format
33+
ActiveAdmin.application.localize_format = :short
34+
example.call
35+
ActiveAdmin.application.localize_format = previous_localize_format
36+
end
3937

40-
expect(pretty_format(t)).to eq "28 Feb 20:15"
41-
end
38+
it "formats it with the default custom format" do
39+
expect(pretty_format(t)).to eq "28 Feb 20:15"
4240
end
4341

44-
context "with non-English locale" do
45-
around do |example|
46-
I18n.with_locale(:es) { example.call }
42+
it "formats it with i18n custom format" do
43+
with_translation time: { formats: { short: "%-m %d %Y" } } do
44+
expect(pretty_format(t)).to eq "2 28 1985"
4745
end
48-
it "should return a localized Date or Time with long format for non-english locale" do
49-
t = Time.utc(1985, "feb", 28, 20, 15, 1)
50-
expect(pretty_format(t)).to eq "28 de febrero de 1985 20:15"
46+
end
47+
end
48+
49+
context "with non-English locale" do
50+
around do |example|
51+
I18n.with_locale(:es) { example.call }
52+
end
53+
54+
it "formats it with the default long format" do
55+
expect(pretty_format(t)).to eq "28 de febrero de 1985 20:15"
56+
end
57+
58+
it "formats it with a customized long format" do
59+
with_translation time: { formats: { long: "El %d de %B de %Y a las %H horas y %M minutos" } } do
60+
expect(pretty_format(t)).to eq "El 28 de febrero de 1985 a las 20 horas y 15 minutos"
5161
end
5262
end
5363
end

0 commit comments

Comments
 (0)
0