aboutsummaryrefslogtreecommitdiffstats
path: root/spec/helpers/link_to_helper_spec.rb
blob: f7be9eab073f01920f8d5aa57108b9cd23f1bcac (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe LinkToHelper do

    include LinkToHelper

    describe 'when creating a url for a request' do

        before do
            @mock_request = mock_model(InfoRequest, :url_title => 'test_title')
        end

        it 'should return a path like /request/test_title' do
            request_path(@mock_request).should == '/request/test_title'
        end

        it 'should return a path including any extra parameters passed' do
            request_path(@mock_request, {:update_status => 1}).should == '/request/test_title?update_status=1'
        end

    end

    describe 'when linking to new incoming messages' do

        before do
            @info_request = mock_model(InfoRequest, :id => 123, :url_title => 'test_title')
            @incoming_message = mock_model(IncomingMessage, :id => 32, :info_request => @info_request)
        end

        context 'for external links' do

            it 'generates the url to the info request of the message' do
                incoming_message_url(@incoming_message).should include('http://test.host/request/test_title')
            end

            it 'includes an anchor to the new message' do
                incoming_message_url(@incoming_message).should include('#incoming-32')
            end

            it 'includes does not cache by default' do
                incoming_message_url(@incoming_message).should_not include('nocache=incoming-32')
            end

            it 'includes a cache busting parameter if set' do
                incoming_message_url(@incoming_message, :cachebust => true).should include('nocache=incoming-32')
            end

        end

        context 'for internal links' do

            it 'generates the incoming_message_url with the path only' do
                expected = '/request/test_title#incoming-32'
                incoming_message_path(@incoming_message).should == expected
            end

        end

    end

    describe 'when linking to new outgoing messages' do

        before do
            @info_request = mock_model(InfoRequest, :id => 123, :url_title => 'test_title')
            @outgoing_message = mock_model(OutgoingMessage, :id => 32, :info_request => @info_request)
        end

        context 'for external links' do

            it 'generates the url to the info request of the message' do
                outgoing_message_url(@outgoing_message).should include('http://test.host/request/test_title')
            end

            it 'includes an anchor to the new message' do
                outgoing_message_url(@outgoing_message).should include('#outgoing-32')
            end

            it 'includes does not cache by default' do
                outgoing_message_url(@outgoing_message).should_not include('nocache=outgoing-32')
            end

            it 'includes a cache busting parameter if set' do
                outgoing_message_url(@outgoing_message, :cachebust => true).should include('nocache=outgoing-32')
            end

        end

        context 'for internal links' do

            it 'generates the outgoing_message_url with the path only' do
                expected = '/request/test_title#outgoing-32'
                outgoing_message_path(@outgoing_message).should == expected
            end

        end

    end

    describe 'when displaying a user link for a request' do

        context "for external requests" do
            before do
                @info_request = mock_model(InfoRequest, :external_user_name => nil,
                                                       :is_external? => true)
            end

            it 'should return the text "Anonymous user" with a link to the privacy help pages when there is no external username' do
                request_user_link(@info_request).should == '<a href="/help/privacy#anonymous">Anonymous user</a>'
            end

            it 'should return a link with an alternative text if requested' do
                request_user_link(@info_request, 'other text').should == '<a href="/help/privacy#anonymous">other text</a>'
            end

            it 'should display an absolute link if requested' do
                request_user_link_absolute(@info_request).should == '<a href="http://test.host/help/privacy#anonymous">Anonymous user</a>'
            end
        end

        context "for normal requests" do

            before do
                @info_request = FactoryGirl.build(:info_request)
            end

            it 'should display a relative link by default' do
                request_user_link(@info_request).should == '<a href="/user/example_user">Example User</a>'
            end

            it 'should display an absolute link if requested' do
                request_user_link_absolute(@info_request).should == '<a href="http://test.host/user/example_user">Example User</a>'
            end

        end

    end

    describe 'when displaying a user admin link for a request' do

        it 'should return the text "An anonymous user (external)" in the case where there is no external username' do
            info_request = mock_model(InfoRequest, :external_user_name => nil,
                                                   :is_external? => true)
            user_admin_link_for_request(info_request).should == 'Anonymous user (external)'
        end

    end

    describe 'simple_date' do

        it 'formats a date in html by default' do
            time = Time.utc(2012, 11, 07, 21, 30, 26)
            self.should_receive(:simple_date_html).with(time)
            simple_date(time)
        end

        it 'formats a date in the specified format' do
            time = Time.utc(2012, 11, 07, 21, 30, 26)
            self.should_receive(:simple_date_text).with(time)
            simple_date(time, :format => :text)
        end

        it 'raises an argument error if given an unrecognized format' do
            time = Time.utc(2012, 11, 07, 21, 30, 26)
            expect { simple_date(time, :format => :unknown) }.to raise_error(ArgumentError)
        end

    end

    describe 'simple_date_html' do

        it 'formats a date in a time tag' do
            Time.use_zone('London') do
                time = Time.utc(2012, 11, 07, 21, 30, 26)
                expected = "<time datetime=\"2012-11-07T21:30:26+00:00\" title=\"2012-11-07 21:30:26 +0000\">November 07, 2012</time>"
                simple_date_html(time).should == expected
            end
        end

    end

    describe 'simple_date_text' do

        it 'should respect time zones' do
            Time.use_zone('Australia/Sydney') do
                simple_date_text(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012'
            end
        end

        it 'should handle Date objects' do
            simple_date_text(Date.new(2012, 11, 21)).should == 'November 21, 2012'
        end

    end

end