diff --git a/features/contexts/property_search_context.exs b/features/contexts/property_search_context.exs index 7083150782fe0f950e983ee4990f02ea8a26a60a..76743337c12ab4cd6ab324e47dea27e332157792 100644 --- a/features/contexts/property_search_context.exs +++ b/features/contexts/property_search_context.exs @@ -29,6 +29,7 @@ defmodule PropertySearchContext do :ok, state |> Map.put(:email, existing_user[:email]) + |> Map.put(:password, existing_user[:password]) } end @@ -144,4 +145,34 @@ defmodule PropertySearchContext do {:ok, state} end + and_ ~r/^I am logged in$/, fn state -> + setup_session(state[:email], state[:password]) + {:ok, state} + end + + and_ ~r/^I want to search$/, fn state -> + navigate_to("/") + click({:id, "search"}) + {:ok, state} + end + + and_ ~r/^I perform a search$/, fn state -> + click({:id, "search_button"}) + {:ok, state} + end + + then_ ~r/^I should see a saved search query$/, fn state -> + assert visible_in_page? ~r/Previous searches/ + el_count = length(find_all_elements(:class, "previous-search")) + assert el_count == 1 + {:ok, state} + end + + defp setup_session(email, password) do + navigate_to("/login") + fill_field({:id, "email"}, email) + fill_field({:id, "password"}, password) + click({:id, "login_button"}) + end + end diff --git a/features/property_search.feature b/features/property_search.feature index ee8759d98c19f3d9608d0a36633888b1aaaa6592..df75d1a8e211029e4f35b86c5aa761975ecb44b0 100644 --- a/features/property_search.feature +++ b/features/property_search.feature @@ -31,3 +31,17 @@ Feature: FR-13, FR-19 & FR-20: Property Search and Revisit And I want to perform a search When I click on the search button Then I should see "3" properties + + Scenario: Authenticated users should see saved search queries + Given there exists following accounts + | name | surname | birth_date | phone_number | email | password | confirm_password | + | Existing | Account | 2000-01-01 | 000 | existing.account@gmail.com | password | password | + And the following properties exist + | title | description | type | property_type | state | location | room_count | area | floor | floor_count | price | + | Rent property | Also a really cool property | rent | apartment | available | London, UK | 3 | 160.0 | 2 | 5 | 6000 | + And I am logged in + And I want to search + And I perform a search + And I want to search + Then I should see a saved search query + diff --git a/lib/proptrackr/search.ex b/lib/proptrackr/search.ex index f15d3fe737b7abc52b57c7757a0a1427fd8ed07c..fc3a467eb1f0f51978890cd704b9e39e88f49482 100644 --- a/lib/proptrackr/search.ex +++ b/lib/proptrackr/search.ex @@ -77,6 +77,7 @@ defmodule PropTrackr.Search do |> validate_required([:type, :location, :min_price, :max_price, :min_rooms, :max_rooms]) |> validate_minmax(:min_price, :max_price) |> validate_minmax(:min_rooms, :max_rooms) + |> cast_areas() end defp validate_minmax(changeset, min_key, max_key) do @@ -89,4 +90,13 @@ defmodule PropTrackr.Search do true -> changeset end end + + defp cast_areas(changeset) do + areas = get_field(changeset, :areas) + if areas == nil do + put_change(changeset, :areas, []) + else + changeset + end + end end diff --git a/lib/proptrackr_web/controllers/search_html/search.html.heex b/lib/proptrackr_web/controllers/search_html/search.html.heex index da2acc71712cde17c09f8e24da0a374196adf5a3..45c4dc54083b573e2bcf0b594a6de2594433673c 100644 --- a/lib/proptrackr_web/controllers/search_html/search.html.heex +++ b/lib/proptrackr_web/controllers/search_html/search.html.heex @@ -12,7 +12,7 @@ <h3 class="font-bold">Previous searches</h3> <ul> <%= for search <- @previous_searches do %> - <li> + <li class="previous-search"> <.link navigate={"/search/#{search.id}"} class="underline"> <%= "#{search.type} in #{search.location} with #{Enum.join(search.areas, ", ")} areas, #{search.min_price} - #{search.max_price} € price range and #{search.min_rooms} - #{search.max_rooms} rooms" %> </.link>