]> git.openstreetmap.org Git - rails.git/blob - test/helpers/application_helper_test.rb
Replace flash icon
[rails.git] / test / helpers / application_helper_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class ApplicationHelperTest < ActionView::TestCase
6   attr_accessor :current_user
7
8   def test_linkify
9     %w[http://example.com/test ftp://example.com/test https://example.com/test].each do |link|
10       text = "Test #{link} is <b>made</b> into a link"
11
12       html = linkify(text)
13       assert_predicate html, :html_safe?
14       assert_dom_equal "Test <a href=\"#{link}\" rel=\"nofollow\" dir=\"auto\">#{link}</a> is &lt;b&gt;made&lt;/b&gt; into a link", html
15
16       html = linkify(text.html_safe)
17       assert_predicate html, :html_safe?
18       assert_dom_equal "Test <a href=\"#{link}\" rel=\"nofollow\" dir=\"auto\">#{link}</a> is <b>made</b> into a link", html
19     end
20
21     %w[test@example.com mailto:test@example.com].each do |link|
22       text = "Test #{link} is not <b>made</b> into a link"
23
24       html = linkify(text)
25       assert_predicate html, :html_safe?
26       assert_dom_equal "Test #{link} is not &lt;b&gt;made&lt;/b&gt; into a link", html
27
28       html = linkify(text.html_safe)
29       assert_predicate html, :html_safe?
30       assert_dom_equal "Test #{link} is not <b>made</b> into a link", html
31     end
32   end
33
34   def test_rss_link_to
35     link = rss_link_to(:controller => :diary_entries, :action => :rss)
36     assert_dom_equal "<a href=\"/diary/rss\"><i class=\"bi bi-rss-fill fs-6\" aria-hidden=\"true\"></i></a>", link
37   end
38
39   def test_dir
40     assert_equal "ltr", dir
41
42     params[:dir] = "rtl"
43     assert_equal "rtl", dir
44     params.delete(:dir)
45
46     I18n.with_locale "he" do
47       assert_equal "rtl", dir
48
49       params[:dir] = "ltr"
50       assert_equal "ltr", dir
51       params.delete(:dir)
52     end
53   end
54
55   def test_friendly_date
56     date = friendly_date(Time.utc(2014, 3, 5, 18, 58, 23))
57     assert_match %r{^<time title=" *5 March 2014 at 18:58" datetime="2014-03-05T18:58:23Z">.*</time>$}, date
58
59     date = friendly_date(Time.now.utc - 1.hour)
60     assert_match %r{^<time title=".*">about 1 hour</time>$}, date
61
62     date = friendly_date(Time.now.utc - 2.days)
63     assert_match %r{^<time title=".*">2 days</time>$}, date
64
65     date = friendly_date(Time.now.utc - 3.weeks)
66     assert_match %r{^<time title=".*">21 days</time>$}, date
67
68     date = friendly_date(Time.now.utc - 4.months)
69     assert_match %r{^<time title=".*">4 months</time>$}, date
70   end
71
72   def test_friendly_date_ago
73     date = friendly_date_ago(Time.utc(2014, 3, 5, 18, 58, 23))
74     assert_match %r{^<time title=" *5 March 2014 at 18:58" datetime="2014-03-05T18:58:23Z">.*</time>$}, date
75
76     date = friendly_date_ago(Time.now.utc - 1.hour)
77     assert_match %r{^<time title=".*">about 1 hour ago</time>$}, date
78
79     date = friendly_date_ago(Time.now.utc - 2.days)
80     assert_match %r{^<time title=".*">2 days ago</time>$}, date
81
82     date = friendly_date_ago(Time.now.utc - 3.weeks)
83     assert_match %r{^<time title=".*">21 days ago</time>$}, date
84
85     date = friendly_date_ago(Time.now.utc - 4.months)
86     assert_match %r{^<time title=".*">4 months ago</time>$}, date
87   end
88 end