-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication_helper_spec.rb
228 lines (180 loc) · 7.02 KB
/
application_helper_spec.rb
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require "rails_helper"
# TestController doesn't have this method so we can't stub it nicely
class ActionView::TestCase::TestController
def previous_step_path
"/foo/bar"
end
end
RSpec.describe ApplicationHelper, type: :helper do
let(:record) { DisclosureCheck.new }
describe "#step_form" do
let(:expected_defaults) do
{
url: {
controller: "application",
action: :update,
},
html: {
class: "edit_disclosure_check",
},
method: :put,
}
end
let(:form_block) { proc {} }
it "acts like FormHelper#form_for with additional defaults" do
expect(helper).to receive(:form_for).with(record, expected_defaults) do |*_args, &block|
expect(block).to eq(form_block)
end
helper.step_form(record, &form_block)
end
it "accepts additional options like FormHelper#form_for would" do
expect(helper).to receive(:form_for).with(record, expected_defaults.merge(foo: "bar"))
helper.step_form(record, { foo: "bar" })
end
it "appends optional css classes if provided" do
expect(helper).to receive(:form_for).with(record, expected_defaults.merge(html: { class: %w[test edit_disclosure_check] }))
helper.step_form(record, html: { class: "test" })
end
end
describe "#step_header" do
let(:form_object) { instance_double("Form object") }
it "renders the expected content" do
allow(helper).to receive(:render).with(partial: "layouts/step_header", locals: { path: "/foo/bar" }).and_return("foobar")
assign(:form_object, form_object)
expect(helper.step_header).to eq("foobar")
end
context "when a specific path is provided" do
it "renders the back link with provided path" do
allow(helper).to receive(:render).with(partial: "layouts/step_header", locals: { path: "/another/step" }).and_return("foobar")
assign(:form_object, form_object)
expect(helper.step_header(path: "/another/step")).to eq("foobar")
end
end
end
describe "#govuk_error_summary" do
context "when no form object is given" do
let(:form_object) { nil }
it "returns nil" do
expect(helper.govuk_error_summary(form_object)).to be_nil
end
end
context "when a form object without errors is given" do
let(:form_object) { BaseForm.new }
it "returns nil" do
expect(helper.govuk_error_summary(form_object)).to be_nil
end
end
context "when a form object with errors is given" do
let(:form_object) { BaseForm.new }
let(:title) { helper.content_for(:page_title) }
before do
helper.title("A page")
form_object.errors.add(:base, :blank)
end
it "returns the summary" do
expect(
helper.govuk_error_summary(form_object),
).to eq(
'<div class="govuk-error-summary" data-module="govuk-error-summary"><div role="alert"><h2 class="govuk-error-summary__title">There is a problem on this page</h2><div class="govuk-error-summary__body"><ul class="govuk-list govuk-error-summary__list"><li><a data-turbolinks="false" href="#base-form-base-field-error">can't be blank</a></li></ul></div></div></div>',
)
end
it "prepends the page title with an error hint" do
helper.govuk_error_summary(form_object)
expect(title).to start_with("Error: A page")
end
end
end
describe "#title" do
let(:title) { helper.content_for(:page_title) }
before do
helper.title(value)
end
context "with a blank value" do
let(:value) { "" }
it { expect(title).to eq("Check when to disclose cautions or convictions - GOV.UK") }
end
context "with a provided value" do
let(:value) { "Test page" }
it { expect(title).to eq("Test page - Check when to disclose cautions or convictions - GOV.UK") }
end
end
describe "#fallback_title" do
before do
allow(Rails).to receive_message_chain(:application, :config, :consider_all_requests_local).and_return(false) # rubocop:disable RSpec/MessageChain
allow(helper).to receive(:controller_name).and_return("my_controller")
allow(helper).to receive(:action_name).and_return("an_action")
end
it "notifies in Sentry about the missing translation" do
expect(Sentry).to receive(:capture_exception).with(
StandardError.new("page title missing: my_controller#an_action"),
)
helper.fallback_title
end
it "calls #title with a blank value" do
expect(helper).to receive(:title).with("")
helper.fallback_title
end
end
describe "#link_button" do
it "builds the link markup styled as a button" do
expect(
helper.link_button(:start_again, root_path),
).to eq('<a class="govuk-button" data-module="govuk-button" href="/">New check</a>')
end
end
describe "#any_completed_checks?" do
let(:disclosure_report) { instance_double(DisclosureReport) }
let(:disclosure_checks) { instance_double("result_set", completed: checks) }
before do
allow(disclosure_report).to receive(:disclosure_checks).and_return(disclosure_checks)
allow(helper).to receive(:current_disclosure_report).and_return(disclosure_report)
end
context "when no checks completed" do
let(:checks) { [] }
it { expect(helper.any_completed_checks?).to eq(false) }
end
context "when at least one check completed" do
let(:checks) { [1] }
it { expect(helper.any_completed_checks?).to eq(true) }
end
end
describe "#resume_check_path" do
let(:current_disclosure_check) { instance_double(DisclosureCheck, navigation_stack:) }
before do
allow(helper).to receive(:current_disclosure_check).and_return(current_disclosure_check)
end
context "when the stack is empty" do
let(:navigation_stack) { [] }
it "returns the root path" do
expect(helper.resume_check_path).to eq("/")
end
end
context "when the stack has elements" do
let(:navigation_stack) { ["/somewhere", "/over", "/the", "/rainbow"] }
it "returns the last element" do
expect(helper.resume_check_path).to eq("/rainbow")
end
end
end
describe "dev_tools_enabled?" do
before do
allow(Rails).to receive_message_chain(:env, :development?).and_return(development_env) # rubocop:disable RSpec/MessageChain
allow(ENV).to receive(:key?).with("DEV_TOOLS_ENABLED").and_return(dev_tools_enabled)
end
context "with development envs" do
let(:development_env) { true }
let(:dev_tools_enabled) { nil }
it { expect(helper.dev_tools_enabled?).to eq(true) }
end
context "with envs that declare the `DEV_TOOLS_ENABLED` env variable" do
let(:development_env) { false }
let(:dev_tools_enabled) { true }
it { expect(helper.dev_tools_enabled?).to eq(true) }
end
context "with envs without `DEV_TOOLS_ENABLED` env variable" do
let(:development_env) { false }
let(:dev_tools_enabled) { false }
it { expect(helper.dev_tools_enabled?).to eq(false) }
end
end
end