Skip to content

Commit

Permalink
Merge pull request #1653 from alphagov/ldeb-use-page-repository-in-specs
Browse files Browse the repository at this point in the history
Use PageRepository in specs where appropriate
  • Loading branch information
lfdebrux authored Dec 9, 2024
2 parents 58ee663 + dca6b76 commit 953e610
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/controllers/pages/guidance_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create
def edit
guidance_input = Pages::GuidanceInput.new(page_heading: draft_question.page_heading,
guidance_markdown: draft_question.guidance_markdown)
back_link = edit_question_path(current_form, page)
back_link = edit_question_path(current_form, page.id)

render :guidance, locals: view_locals(page, guidance_input, back_link)
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/pages/selection/bulk_options_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def edit
@bulk_options_path = selection_bulk_options_update_path(current_form)
@bulk_options_input = Pages::Selection::BulkOptionsInput.new(draft_question:)
@bulk_options_input.assign_form_values
@back_link_url = edit_question_path(current_form, page)
@back_link_url = edit_question_path(current_form, page.id)
render "pages/selection/bulk_options", locals: { current_form: }
end

def update
@bulk_options_input = Pages::Selection::BulkOptionsInput.new(**bulk_options_input_params,
draft_question:)
@bulk_options_path = selection_bulk_options_update_path(current_form)
@back_link_url = edit_question_path(current_form, page)
@back_link_url = edit_question_path(current_form, page.id)

if @bulk_options_input.submit
redirect_to edit_question_path(current_form)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/pages/type_of_answer_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def update
def selection_path(form, action)
return question_text_new_path(form) if action == :create

selection_type_edit_path(form, page)
selection_type_edit_path(form, page.id)
end

def text_path(form, action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, fake_page.to_json, 200
mock.post "/api/v1/forms/1/pages", post_headers, fake_page.to_json, 200
mock.put "/api/v1/forms/1", post_headers, form.to_json, 200
end

allow(PageRepository).to receive(:create!).with(hash_including(form_id: 1)).and_return(fake_page)
allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(fake_page)

GroupForm.create!(group:, form_id: form.id)
create(:membership, group:, user: standard_user, added_by: standard_user)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.post "/api/v1/forms/1/pages", post_headers
pages.each do |page|
mock.get "/api/v1/forms/1/pages/#{page.id}", headers, page.to_json, 200
end
end

pages.each do |page|
allow(PageRepository).to receive(:find).with(page_id: page.id.to_s, form_id: 1).and_return(page)
end
allow(PageRepository).to receive(:create!).with(hash_including(form_id: 1))

GroupForm.create! group:, form_id: form.id
create(:membership, group:, user: standard_user, added_by: standard_user)

Expand Down
5 changes: 3 additions & 2 deletions spec/features/form/share_a_preview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, fake_page.to_json, 200
mock.post "/api/v1/forms/1/pages", post_headers, fake_page.to_json, 200
mock.put "/api/v1/forms/1", post_headers, form.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(fake_page)
allow(PageRepository).to receive(:create!).with(hash_including(form_id: 1))

GroupForm.create!(group:, form_id: form.id)
create(:membership, group:, user: standard_user, added_by: standard_user, role: :group_admin)

Expand Down
9 changes: 6 additions & 3 deletions spec/requests/pages/address_settings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)

draft_question
get address_settings_edit_path(form_id: page.form_id, page_id: page.id)
end
Expand Down Expand Up @@ -144,9 +146,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))
end

context "when form is valid and ready to update in the DB" do
Expand Down
18 changes: 12 additions & 6 deletions spec/requests/pages/conditions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/1", headers, selected_page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "1", form_id: 1).and_return(selected_page)

get new_condition_path(form_id: 1, page_id: 1)
end

Expand Down Expand Up @@ -134,9 +135,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/1", headers, selected_page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "1", form_id: 1).and_return(selected_page)

conditions_input = Pages::ConditionsInput.new(form:, page: selected_page, answer_value: "Yes", goto_page_id: 3)

allow(conditions_input).to receive(:submit).and_return(submit_result)
Expand Down Expand Up @@ -195,9 +197,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/#{selected_page.id}", headers, selected_page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: selected_page.id.to_s, form_id: 1).and_return(selected_page)

allow(ConditionRepository).to receive(:find).and_return(condition)

allow(Pages::ConditionsInput).to receive(:new).and_return(conditions_input)
Expand Down Expand Up @@ -245,9 +248,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/#{selected_page.id}", headers, selected_page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: selected_page.id.to_s, form_id: 1).and_return(selected_page)

conditions_input = Pages::ConditionsInput.new(form:, page: selected_page, record: condition, answer_value: "Yes", goto_page_id: 3)

allow(ConditionRepository).to receive(:find).and_return(condition)
Expand Down Expand Up @@ -309,9 +313,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/#{selected_page.id}", headers, selected_page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: selected_page.id.to_s, form_id: 1).and_return(selected_page)

allow(ConditionRepository).to receive(:find).and_return(condition)

delete_condition_input = Pages::DeleteConditionInput.new(form:, page: selected_page, record: condition, answer_value: "Yes", goto_page_id: 3)
Expand Down Expand Up @@ -355,9 +360,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/#{selected_page.id}", headers, selected_page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: selected_page.id.to_s, form_id: 1).and_return(selected_page)

allow(ConditionRepository).to receive(:find).and_return(condition)
allow(ConditionRepository).to receive(:destroy)

Expand Down
9 changes: 6 additions & 3 deletions spec/requests/pages/date_settings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)

draft_question
get date_settings_edit_path(form_id: page.form_id, page_id: page.id)
end
Expand Down Expand Up @@ -127,9 +129,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))
end

context "when form is valid and ready to update in the DB" do
Expand Down
5 changes: 3 additions & 2 deletions spec/requests/pages/guidance_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/#{page.id}", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: page.id.to_s, form_id: 1).and_return(page)

get guidance_edit_path(form_id: form.id, page_id: page.id)
end

Expand Down Expand Up @@ -184,8 +185,8 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/#{page.id}", headers, page.to_json, 200
end
allow(PageRepository).to receive(:find).with(page_id: page.id.to_s, form_id: 1).and_return(page)
allow(controller_spy).to receive(:draft_question).and_return(draft_question)
post guidance_update_path(form_id: form.id, page_id: page.id), params: { pages_guidance_input: { page_heading:, guidance_markdown: }, route_to: }
end
Expand Down
9 changes: 6 additions & 3 deletions spec/requests/pages/name_settings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)

draft_question
get name_settings_edit_path(form_id: page.form_id, page_id: page.id)
end
Expand Down Expand Up @@ -128,9 +130,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))
end

context "when form is valid and ready to update in the DB" do
Expand Down
3 changes: 2 additions & 1 deletion spec/requests/pages/routes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/101", headers, selected_page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "101", form_id: 1).and_return(selected_page)

get show_routes_path(form_id: form.id, page_id: selected_page.id)
end

Expand Down
7 changes: 4 additions & 3 deletions spec/requests/pages/selection/bulk_options_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end
allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
draft_question
get selection_bulk_options_edit_path(form_id: page.form_id, page_id: page.id)
end
Expand Down Expand Up @@ -163,9 +163,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))
end

context "when form is valid and ready to update in the DB" do
Expand Down
10 changes: 7 additions & 3 deletions spec/requests/pages/selection/options_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)

draft_question
get selection_options_edit_path(form_id: page.form_id, page_id: page.id)
end
Expand Down Expand Up @@ -178,9 +180,11 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))

draft_question
end

Expand Down
9 changes: 6 additions & 3 deletions spec/requests/pages/selection/type_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)

draft_question
get selection_type_edit_path(form_id: page.form_id, page_id: page.id)
end
Expand Down Expand Up @@ -155,9 +157,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))
end

context "when form is valid and ready to update in the DB" do
Expand Down
9 changes: 6 additions & 3 deletions spec/requests/pages/text_settings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)

draft_question
get text_settings_edit_path(form_id: page.form_id, page_id: page.id)
end
Expand Down Expand Up @@ -123,9 +125,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))
end

context "when form is valid and ready to update in the DB" do
Expand Down
8 changes: 5 additions & 3 deletions spec/requests/pages/type_of_answer_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)

get type_of_answer_edit_path(form_id: page.form_id, page_id: page.id)
end

Expand Down Expand Up @@ -214,9 +215,10 @@
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/forms/1", headers, form.to_json, 200
mock.get "/api/v1/forms/1/pages", headers, pages.to_json, 200
mock.get "/api/v1/forms/1/pages/2", headers, page.to_json, 200
mock.put "/api/v1/forms/1/pages/2", post_headers
end

allow(PageRepository).to receive(:find).with(page_id: "2", form_id: 1).and_return(page)
allow(PageRepository).to receive(:save!).with(hash_including(page_id: "2", form_id: 1))
end

context "when form is valid and ready to update in the DB" do
Expand Down

0 comments on commit 953e610

Please sign in to comment.