Skip to content

Commit 1b4aa8c

Browse files
committed
Remove hardcoded URLs from add another answer specs
These test were hardcoding the format of the URL string, which meant they were fragile when the URL format changed. This commit updates them to use Rails's path helpers so that they're a little more resilient to changes in the URL structure.
1 parent 12e87b8 commit 1b4aa8c

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

spec/requests/forms/add_another_answer_controller_spec.rb

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,63 +40,70 @@
4040
end
4141

4242
describe "GET #show" do
43+
before do
44+
get add_another_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id)
45+
end
46+
4347
it "renders the show template" do
44-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer"
4548
expect(response).to render_template(:show)
4649
end
4750

4851
it "assigns @rows" do
49-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer"
5052
expect(assigns(:rows).count).to eq 2
5153
end
5254

5355
it "adds the change and remove links to each row" do
54-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer"
5556
expect(assigns(:rows).first[:actions].first[:text]).to eq("Change")
5657
expect(assigns(:rows).first[:actions].second[:text]).to eq("Remove")
5758
expect(response.body).to include(form_remove_answer_path(form.id, form.form_slug, first_step_in_form.id, answer_index: 1, changing_existing_answer: nil))
5859
end
5960

6061
it "initializes @add_another_answer_input" do
61-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer"
6262
expect(assigns(:add_another_answer_input)).to be_a(AddAnotherAnswerInput)
6363
end
6464
end
6565

6666
describe "POST #save" do
67+
before do
68+
post add_another_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id), params:
69+
end
70+
6771
context "with valid params" do
6872
context "when adding another answer" do
73+
let(:params) { { add_another_answer_input: { add_another_answer: "yes" } } }
74+
6975
it "redirects to first page to add another" do
70-
post "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer", params: { add_another_answer_input: { add_another_answer: "yes" } }
71-
expect(response).to redirect_to("/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/3")
76+
expect(response).to redirect_to(form_page_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id, answer_index: 3))
7277
end
7378
end
7479

7580
context "when not adding another answer" do
81+
let(:params) { { add_another_answer_input: { add_another_answer: "no" } } }
82+
7683
it "redirects to next page" do
77-
post "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer", params: { add_another_answer_input: { add_another_answer: "no" } }
78-
expect(response).to redirect_to("/preview-draft/#{form.id}/#{form.form_slug}/#{second_step_in_form.id}")
84+
expect(response).to redirect_to(form_page_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: second_step_in_form.id))
7985
end
8086
end
8187
end
8288

8389
context "with invalid params" do
90+
let(:params) { { add_another_answer_input: { add_another_answer: "" } } }
91+
8492
it "renders the show template" do
85-
post "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer", params: { add_another_answer_input: { add_another_answer: "" } }
8693
expect(response).to render_template(:show)
8794
end
8895

8996
it "assigns @rows" do
90-
post "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer", params: { add_another_answer_input: { add_another_answer: "" } }
9197
expect(assigns(:rows).count).to be_present
9298
end
9399
end
94100

95101
context "with the maximum number of answers" do
96102
let(:stored_answers) { Array.new(RepeatableStep::MAX_ANSWERS) { |i| { text: i.to_s } } }
103+
let(:params) { { add_another_answer_input: { add_another_answer: "yes" } } }
97104

98105
it "renders the show template with an error" do
99-
post "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer", params: { add_another_answer_input: { add_another_answer: "yes" } }
106+
post add_another_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id), params: { add_another_answer_input: { add_another_answer: "yes" } }
100107
expect(response).to render_template(:show)
101108
expect(response.body).to include("You cannot add another answer")
102109
end
@@ -106,13 +113,13 @@
106113
describe "redirect_if_not_repeating" do
107114
context "when step is not RepeatableStep" do
108115
it "redirects to form_page when not changing existing answer" do
109-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{second_step_in_form.id}/add-another-answer"
110-
expect(response).to redirect_to("/preview-draft/#{form.id}/#{form.form_slug}/#{second_step_in_form.id}")
116+
get add_another_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: second_step_in_form.id)
117+
expect(response).to redirect_to(form_page_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: second_step_in_form.id))
111118
end
112119

113120
it "redirects to form_change_answer_path when changing existing answer" do
114-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{second_step_in_form.id}/add-another-answer/change"
115-
expect(response).to redirect_to("/preview-draft/#{form.id}/#{form.form_slug}/#{second_step_in_form.id}/change")
121+
get change_add_another_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: second_step_in_form.id)
122+
expect(response).to redirect_to(form_change_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: second_step_in_form.id))
116123
end
117124
end
118125
end

spec/requests/forms/remove_answer_controller_spec.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,36 @@
4343
end
4444

4545
describe "GET #show" do
46+
before do
47+
get form_remove_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id, answer_index: 1)
48+
end
49+
4650
it "renders the show template" do
47-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/1/remove"
4851
expect(response).to render_template(:show)
4952
end
5053

5154
it "initializes @remove_input" do
52-
get "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/1/remove"
5355
expect(assigns(:remove_input)).to be_a(RemoveInput)
5456
end
5557
end
5658

5759
describe "DELETE #delete" do
60+
let(:params) { { remove_input: { remove: } } }
61+
62+
before do
63+
delete form_remove_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id, answer_index: 1), params:
64+
end
65+
5866
context "with valid params" do
5967
it "redirects to add another answer" do
60-
delete "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/1/remove", params: { remove_input: { remove: } }
61-
expect(response).to redirect_to("/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer")
68+
expect(response).to redirect_to(add_another_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id))
6269
end
6370

6471
context "when not removing answer" do
6572
let(:remove) { "no" }
6673

6774
it "redirects to add another answer" do
68-
delete "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/1/remove", params: { remove_input: { remove: } }
69-
expect(response).to redirect_to("/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/add-another-answer")
75+
expect(response).to redirect_to(add_another_answer_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id))
7076
end
7177
end
7278
end
@@ -75,7 +81,6 @@
7581
let(:remove) { "invalid" }
7682

7783
it "renders the show template" do
78-
delete "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/1/remove", params: { remove_input: { remove: } }
7984
expect(response).to render_template(:show)
8085
expect(response).to have_http_status(:unprocessable_content)
8186
end
@@ -85,9 +90,8 @@
8590
let(:stored_answers) { [{ text: "answer 1" }] }
8691
let(:is_optional) { true }
8792

88-
it "redirects to the next question page" do
89-
delete "/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}/1/remove", params: { remove_input: { remove: } }
90-
expect(response).to redirect_to("/preview-draft/#{form.id}/#{form.form_slug}/#{first_step_in_form.id}")
93+
it "redirects to the question page" do
94+
expect(response).to redirect_to(form_page_path(mode: "preview-draft", form_id: form.id, form_slug: form.form_slug, page_slug: first_step_in_form.id))
9195
end
9296
end
9397
end

0 commit comments

Comments
 (0)