Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[in-review] Fix search by company name and search by industry name #693

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/companies/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ defmodule Companies.Helpers do

@default_page_size "16"

def searched_list(list, _params) do
list
def searched_list(list, params) do
search_param = params["search"]["text"]

if search_param do
list
|> Enum.filter(
&(String.contains?(String.downcase(&1.name), String.downcase(search_param)) or
String.contains?(String.downcase(&1.industry), String.downcase(search_param)))
)
else
list
end
end

def sorted_list(list, params) do
Expand Down
77 changes: 77 additions & 0 deletions test/companies/helper_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
defmodule Companies.HelperTest do
use ExUnit.Case

alias Companies.Helpers

describe "searched_list/2" do
test "returns the original list if search_param is not provided" do
list = [
%{name: "Company A", industry: "Tech"},
%{name: "Company B", industry: "Health"}
]

params = %{"search" => %{"text" => nil}}

assert Helpers.searched_list(list, params) == list
end

test "filters the list based on search_param" do
list = [
%{name: "Company A", industry: "Tech"},
%{name: "Company B", industry: "Health"}
]

params = %{"search" => %{"text" => "tech"}}

result = Helpers.searched_list(list, params)

assert length(result) == 1
assert Enum.at(result, 0) == %{name: "Company A", industry: "Tech"}
end

test "handles case-insensitive search" do
list = [
%{name: "Company A", industry: "Tech"},
%{name: "Company B", industry: "Health"}
]

params = %{"search" => %{"text" => "COMPANY"}}

result = Helpers.searched_list(list, params)

assert length(result) == 2
assert Enum.at(result, 0) == %{name: "Company A", industry: "Tech"}
assert Enum.at(result, 1) == %{name: "Company B", industry: "Health"}
end

test "handles an empty list" do
list = []

params = %{"search" => %{"text" => "tech"}}

assert Helpers.searched_list(list, params) == []
end

test "handles an empty search_param" do
list = [
%{name: "Company A", industry: "Tech"},
%{name: "Company B", industry: "Health"}
]

params = %{"search" => %{"text" => ""}}

assert Helpers.searched_list(list, params) == list
end

test "handles a non-matching search_param" do
list = [
%{name: "Company A", industry: "Tech"},
%{name: "Company B", industry: "Health"}
]

params = %{"search" => %{"text" => "Finance"}}

assert Helpers.searched_list(list, params) == []
end
end
end
Loading