spec/unit/pretty_format_spec.rb in activeadmin-1.0.0.pre5 vs spec/unit/pretty_format_spec.rb in activeadmin-1.0.0
- old
+ new
@@ -6,60 +6,64 @@
def method_missing(*args, &block)
mock_action_view.send *args, &block
end
- {String: 'hello', Fixnum: 23, Float: 5.67, Bignum: 10**30, Symbol: :foo,
- 'Arbre::Element' => Arbre::Element.new.br(:foo)
- }.each do |klass, obj|
- it "should call `to_s` on #{klass}s" do
- expect(obj).to be_a klass.to_s.constantize # safeguard for Bignum
+ ['hello', 23, 5.67, 10**30, :foo, Arbre::Element.new.br(:foo)].each do |obj|
+ it "should call `to_s` on #{obj.class}s" do
expect(pretty_format(obj)).to eq obj.to_s
end
end
- context "given a Date or a Time" do
- it "should return a localized Date or Time with long format" do
- t = Time.now
- expect(self).to receive(:localize).with(t, {format: :long}) { "Just Now!" }
- expect(pretty_format(t)).to eq "Just Now!"
+ shared_examples_for 'a time-ish object' do |t|
+ it "formats it with the default long format" do
+ expect(pretty_format(t)).to eq "February 28, 1985 20:15"
end
- context "actually do the formatting" do
- it "should actually do the formatting" do
- t = Time.utc(1985,"feb",28,20,15,1)
- expect(pretty_format(t)).to eq "February 28, 1985 20:15"
+ it "formats it with a customized long format" do
+ with_translation time: { formats: { long: "%B %d, %Y, %l:%M%P" } } do
+ expect(pretty_format(t)).to eq "February 28, 1985, 8:15pm"
end
+ end
- context "apply custom localize format" do
- around do |example|
- previous_localize_format = ActiveAdmin.application.localize_format
- ActiveAdmin.application.localize_format = :short
- example.call
- ActiveAdmin.application.localize_format = previous_localize_format
- end
- it "should actually do the formatting" do
- t = Time.utc(1985, "feb", 28, 20, 15, 1)
+ context "with a custom localize format" do
+ around do |example|
+ previous_localize_format = ActiveAdmin.application.localize_format
+ ActiveAdmin.application.localize_format = :short
+ example.call
+ ActiveAdmin.application.localize_format = previous_localize_format
+ end
- expect(pretty_format(t)).to eq "28 Feb 20:15"
+ it "formats it with the default custom format" do
+ expect(pretty_format(t)).to eq "28 Feb 20:15"
+ end
+
+ it "formats it with i18n custom format" do
+ with_translation time: { formats: { short: "%-m %d %Y" } } do
+ expect(pretty_format(t)).to eq "2 28 1985"
end
end
+ end
- context "with non-English locale" do
- before do
- @previous_locale = I18n.locale.to_s
- I18n.locale = "es"
+ context "with non-English locale" do
+ around do |example|
+ I18n.with_locale(:es) { example.call }
+ end
+
+ it "formats it with the default long format" do
+ expect(pretty_format(t)).to eq "28 de febrero de 1985 20:15"
+ end
+
+ it "formats it with a customized long format" do
+ with_translation time: { formats: { long: "El %d de %B de %Y a las %H horas y %M minutos" } } do
+ expect(pretty_format(t)).to eq "El 28 de febrero de 1985 a las 20 horas y 15 minutos"
end
- after do
- I18n.locale = @previous_locale
- end
- it "should return a localized Date or Time with long format for non-english locale" do
- t = Time.utc(1985,"feb",28,20,15,1)
- expect(pretty_format(t)).to eq "28 de febrero de 1985 20:15"
- end
end
end
end
+
+ it_behaves_like 'a time-ish object', Time.utc(1985, "feb", 28, 20, 15, 1)
+ it_behaves_like 'a time-ish object', DateTime.new(1985, 2, 28, 20, 15, 1)
context "given an ActiveRecord object" do
it "should delegate to auto_link" do
post = Post.new
expect(self).to receive(:auto_link).with(post) { "model name" }