Browse Source

test new helper methods

pull/1657/head
Arthur Campbell 3 years ago
parent
commit
7cb90c6210
  1. 3
      app/helpers/filters_helper.rb
  2. 69
      spec/helpers/filters_helper_spec.rb
  3. 71
      spec/helpers/log_list_helper_spec.rb

3
app/helpers/filters_helper.rb

@ -18,8 +18,7 @@ module FiltersHelper
filter_selected?("user", "yours") ||
selected_filters["organisation"]&.present? ||
selected_filters["status"]&.compact_blank&.any? ||
selected_filters["years"]&.compact_blank&.any? ||
selected_filters["bulk_upload_id"]&.compact_blank&.any?
selected_filters["years"]&.compact_blank&.any?
end
def status_filters

69
spec/helpers/filters_helper_spec.rb

@ -70,6 +70,75 @@ RSpec.describe FiltersHelper do
end
end
describe "#any_filter_selected?" do
let(:result) { any_filter_selected? }
let(:serialised_filters) { filters&.to_json }
let(:filters) { nil }
before do
session[:logs_filters] = serialised_filters if serialised_filters
end
it "returns false if the session contains no logs filters" do
expect(result).to be_falsey
end
context "when organisation and user are set to all" do
let(:filters) { { "organisation_select" => "all", "user" => "all" } }
it "returns false" do
expect(result).to be_falsey
end
end
context "when user is set to 'yours'" do
let(:filters) { { "user" => "yours" } }
it "returns true" do
expect(result).to be true
end
end
context "when organisation is filtered" do
let(:filters) { { "organisation" => 2 } }
it "returns true" do
expect(result).to be true
end
end
context "when status is filtered" do
let(:filters) { { "status" => ["in_progress"] } }
it "returns true" do
expect(result).to be true
end
end
context "when collection year is filtered" do
let(:filters) { { "years" => ["2023"] } }
it "returns true" do
expect(result).to be true
end
end
context "when a range of filters are applied" do
let(:filters) do
{
"user" => "all",
"status" => %w[in_progress completed],
"years" => [""],
"organisation" => 2,
}
end
it "returns true" do
expect(result).to be true
end
end
end
describe "#selected_option" do
before do
session[:lettings_logs_filters] = {}.to_json

71
spec/helpers/log_list_helper_spec.rb

@ -0,0 +1,71 @@
require "rails_helper"
RSpec.describe LogListHelper, type: :helper do
include FiltersHelper
describe "#display_delete_logs?" do
let(:search_term) { nil }
context "when logged in as a data provider" do
let(:result) { display_delete_logs?(user, search_term) }
let(:user) { create(:user) }
it "returns false if no filters or search are set" do
allow(self).to receive(:filter_selected?).and_return false
expect(result).to be false
end
it "returns true if the user filter is set to 'yours'" do
allow(self).to receive(:filter_selected?).with("user", "yours").and_return true
expect(result).to be true
end
it "returns false if any filters other than the user filter are set" do
allow(self).to receive(:filter_selected?).and_return true
allow(self).to receive(:filter_selected?).with("user", "yours").and_return false
expect(result).to be false
end
context "when there is a search term present" do
let(:search_term) { "word" }
it "still returns false as long as the user filter is not set to yours" do
allow(self).to receive(:filter_selected?).with("user", "yours").and_return false
expect(result).to be false
end
end
end
context "when logged in as a support user or data coordinator" do
let(:support_user) { create(:user, :support) }
let(:data_coordinator) { create(:user, :data_coordinator) }
let(:support_result) { display_delete_logs?(support_user, search_term) }
let(:coordinator_result) { display_delete_logs?(data_coordinator, search_term) }
let(:results) { [support_result, coordinator_result] }
it "returns false if no filters or search are set" do
allow(self).to receive(:any_filter_selected?).and_return false
expect(results).to all be false
end
it "returns true if any filter is set" do
allow(self).to receive(:any_filter_selected?).and_return true
expect(results).to all be true
end
context "when there is a search term present" do
let(:search_term) { "word" }
it "returns true if no filter is selected" do
allow(self).to receive(:any_filter_selected?).and_return false
expect(results).to all be true
end
it "returns true if any filter is selected" do
allow(self).to receive(:any_filter_selected?).and_return true
expect(results).to all be true
end
end
end
end
end
Loading…
Cancel
Save