From 76a19331ac3d55381a338c938bc3598a96519f15 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Wed, 17 Jan 2024 12:16:14 +0000 Subject: [PATCH] feat: add tests --- app/helpers/logs_helper.rb | 2 +- spec/helpers/logs_helper_spec.rb | 64 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 spec/helpers/logs_helper_spec.rb diff --git a/app/helpers/logs_helper.rb b/app/helpers/logs_helper.rb index 509208ad4..c9667d96d 100644 --- a/app/helpers/logs_helper.rb +++ b/app/helpers/logs_helper.rb @@ -74,6 +74,6 @@ module LogsHelper end def unique_answers_to_be_cleared(bulk_upload) - bulk_upload.bulk_upload_errors.reject { |e| e.category == :not_answered }.uniq(&:field) + bulk_upload.bulk_upload_errors.reject { |e| e.category == "not_answered" }.uniq(&:field) end end diff --git a/spec/helpers/logs_helper_spec.rb b/spec/helpers/logs_helper_spec.rb new file mode 100644 index 000000000..fe06e79cf --- /dev/null +++ b/spec/helpers/logs_helper_spec.rb @@ -0,0 +1,64 @@ +require "rails_helper" + +RSpec.describe LogsHelper, type: :helper do + describe "#unique_answers_to_be_cleared" do + let(:result) { unique_answers_to_be_cleared(bulk_upload) } + + context "with a lettings bulk upload with various errors" do + let(:bulk_upload) { create(:bulk_upload, :lettings) } + + before do + errors = [OpenStruct.new(attribute: "field_50", message: "you must answer field 50", category: :not_answered), + OpenStruct.new(attribute: "field_60", message: "some compound error", category: :other_category), + OpenStruct.new(attribute: "field_61", message: "some compound error", category: :other_category), + OpenStruct.new(attribute: "field_61", message: "some other compound error that overlaps with a previous error field", category: :another_category), + OpenStruct.new(attribute: "field_62", message: "some other compound error that overlaps with a previous error field", category: :another_category)] + errors.each do |error| + bulk_upload.bulk_upload_errors.create!( + field: error.attribute, + error: error.message, + tenant_code: "test", + property_ref: "test", + row: "test", + cell: "test", + col: "test", + category: error.category, + ) + end + end + + it "returns the correct unique answers to be cleared" do + expect(result.count).to eq(3) + expect(result.map(&:field)).to match_array(%w[field_60 field_61 field_62]) + end + end + + context "with a sales bulk upload with various errors" do + let(:bulk_upload) { create(:bulk_upload, :sales) } + + before do + errors = [OpenStruct.new(attribute: "field_50", message: "you must answer field 50", category: :not_answered), + OpenStruct.new(attribute: "field_60", message: "some compound error", category: :other_category), + OpenStruct.new(attribute: "field_61", message: "some compound error", category: :other_category), + OpenStruct.new(attribute: "field_61", message: "some other compound error that overlaps with a previous error field", category: :another_category), + OpenStruct.new(attribute: "field_62", message: "some other compound error that overlaps with a previous error field", category: :another_category)] + errors.each do |error| + bulk_upload.bulk_upload_errors.create!( + field: error.attribute, + error: error.message, + purchaser_code: "test", + row: "test", + cell: "test", + col: "test", + category: error.category, + ) + end + end + + it "returns the correct unique answers to be cleared" do + expect(result.count).to eq(3) + expect(result.map(&:field)).to match_array(%w[field_60 field_61 field_62]) + end + end + end +end