Browse Source

write tests for the duplicate logs index page

pull/1776/head
Arthur Campbell 3 years ago committed by Kat
parent
commit
4cb22bf6b5
  1. 3
      app/controllers/duplicate_logs_controller.rb
  2. 100
      spec/requests/duplicate_logs_controller_spec.rb

3
app/controllers/duplicate_logs_controller.rb

@ -27,10 +27,11 @@ class DuplicateLogsController < ApplicationController
def index def index
@duplicates = params.permit(duplicates: {})[:duplicates]&.to_h || duplicates_for_user(current_user) @duplicates = params.permit(duplicates: {})[:duplicates]&.to_h || duplicates_for_user(current_user)
return render_not_found unless @duplicates
@duplicates[:lettings] ||= [] @duplicates[:lettings] ||= []
@duplicates[:sales] ||= [] @duplicates[:sales] ||= []
@duplicate_sets_count = @duplicates[:lettings].count + @duplicates[:sales].count @duplicate_sets_count = @duplicates[:lettings].count + @duplicates[:sales].count
render_not_found if @duplicate_sets_count.zero?
end end
private private

100
spec/requests/duplicate_logs_controller_spec.rb

@ -313,4 +313,104 @@ RSpec.describe DuplicateLogsController, type: :request do
end end
end end
end end
describe "GET #index" do
before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
end
context "when the user is support" do
let(:user) { create(:user, :support) }
it "renders not found" do
get duplicate_logs_path
expect(response).to have_http_status(:not_found)
end
end
context "when the user is a data coordinator" do
let(:user) { create(:user, :data_coordinator) }
it "renders not found" do
get duplicate_logs_path
expect(response).to have_http_status(:not_found)
end
end
context "when the user is a provider" do
let(:user) { create(:user) }
let(:duplicates) do
{
lettings: {
"0" => [1, 2],
"1" => [3, 4, 5],
},
sales: {
"0" => [11, 12],
},
}
end
context "when duplicates are not provided in the params" do
before do
allow_any_instance_of(DuplicateLogsHelper).to receive(:duplicates_for_user).and_return duplicates
end
it "calls the helper method to retrieve duplicates for the current user" do
expect_any_instance_of(DuplicateLogsHelper).to receive(:duplicates_for_user).with(user)
get duplicate_logs_path
end
end
context "when duplicates are provided in the params" do
it "does not call the helper method" do
expect_any_instance_of(DuplicateLogsHelper).not_to receive :duplicates_for_user # rubocop:disable RSpec/AnyInstance
get duplicate_logs_path(duplicates:)
end
context "and either lettings or sales is not present" do
let(:duplicates) { { lettings: { "0" => [1, 2] } } }
it "does not throw an error" do
expect { get duplicate_logs_path(duplicates:) }.not_to raise_error
end
end
end
describe "viewing the page" do
before do
get duplicate_logs_path(duplicates:)
end
it "has the correct headers" do
headers = page.find_css("th").map(&:text)
expect(headers).to include("Type of logs", "Log IDs")
end
it "has the correct number of rows for each log type" do
expect(page).to have_selector "tbody tr td", text: "Lettings", count: duplicates[:lettings].count
expect(page).to have_selector "tbody tr td", text: "Sales", count: duplicates[:sales].count
end
it "shows the log ids for each set of duplicates" do
id_strings = duplicates.values.flat_map(&:values).map do |id_set|
id_set.map { |id| "Log #{id}" }.join(", ")
end
id_strings.each do |id_string|
expect(page).to have_selector "td", text: id_string
end
end
it "shows links for each set of duplciates" do
duplicates[:lettings].each_value do |id_set|
expect(page).to have_link "Review logs", href: lettings_log_duplicate_logs_path(id_set.first)
end
duplicates[:sales].each_value do |id_set|
expect(page).to have_link "Review logs", href: sales_log_duplicate_logs_path(id_set.first)
end
end
end
end
end
end end

Loading…
Cancel
Save