Skip to content

Commit

Permalink
[in-review] Fix search by company name and search by industry name (#693
Browse files Browse the repository at this point in the history
)

* Fix search by company name and search by industry name

* Add testsuite for searched_list/2 action of helper.ex
  • Loading branch information
imahmedismail authored Dec 22, 2023
1 parent 911f91b commit 93e0379
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
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

0 comments on commit 93e0379

Please sign in to comment.