Skip to content
Snippets Groups Projects

Resolve "FR-09: Update Property Advertisement"

Merged shyngys requested to merge 19-fr-09-update-property-advertisement into main
All threads resolved!
9 files
+ 553
8
Compare changes
  • Side-by-side
  • Inline
Files
9
+ 159
0
defmodule PropertyUpdateContext do
use WhiteBread.Context
use Hound.Helpers
alias PropTrackr.Repo
alias PropTrackr.Accounts.User
alias PropTrackr.Properties.Property
scenario_starting_state fn _state ->
Ecto.Adapters.SQL.Sandbox.checkout(PropTrackr.Repo)
Ecto.Adapters.SQL.Sandbox.mode(PropTrackr.Repo, {:shared, self()})
Hound.start_session()
%{}
end
scenario_finalize fn _status, _state ->
Ecto.Adapters.SQL.Sandbox.checkin(PropTrackr.Repo)
Hound.end_session()
end
given_ ~r/^there exists following accounts$/, fn state, %{table_data: table} ->
table
|> Enum.map(fn user_details -> User.changeset(%User{}, user_details) end)
|> Enum.each(fn changeset -> Repo.insert!(changeset) end)
existing_user = List.first(table)
random_user = List.last(table)
{
:ok,
state
|> Map.put(:email, existing_user[:email])
|> Map.put(:password, existing_user[:password])
|> Map.put(:random_email, random_user[:email])
|> Map.put(:random_password, random_user[:password])
}
end
and_ ~r/^the following properties exist$/, fn state, %{ table_data: table } ->
logged_in_user = Repo.get_by(User, email: state[:email])
advertisements =
table
|> Enum.map(fn details -> details |> Map.put(:reference, Ecto.UUID.generate()) end)
|> Enum.map(fn details -> {Ecto.build_assoc(logged_in_user, :properties, details), details} end)
|> Enum.map(fn {assoc, details} -> Property.changeset(assoc, details) end)
|> Enum.map(fn changeset -> Repo.insert!(changeset) end)
{
:ok,
state
|> Map.put(:advertisements, advertisements)
}
end
and_ ~r/^I am logged in$/, fn state ->
setup_session(state[:email], state[:password])
{:ok, state}
end
and_ ~r/^I navigate to my property's details page$/, fn state ->
advertisement = List.first(state[:advertisements])
click({:id, "edit-#{advertisement.reference}"})
{:ok, state}
end
when_ ~r/^I click the edit button$/, fn state ->
click({:id, "edit-property"})
{:ok, state}
end
and_ ~r/^I update the following fields$/, fn state, %{ table_data: table } ->
data = List.first(table)
fill_field({:id, "title"}, data[:title])
fill_field({:id, "description"}, data[:description])
select_dropdown("type", data[:type])
select_dropdown("property_type", data[:property_type])
select_dropdown("state", data[:state])
fill_field({:id, "location"}, data[:location])
fill_field({:id, "room_count"}, data[:room_count])
fill_field({:id, "area"}, data[:area])
fill_field({:id, "floor"}, data[:floor])
fill_field({:id, "price"}, data[:price])
{
:ok,
state
|> Map.put(:updated_data, data)
}
end
and_ ~r/^I click save changes$/, fn state ->
click({:id, "save-changes"})
{:ok, state}
end
then_ ~r/^I should see a success message$/, fn state ->
assert visible_in_page? ~r/Property updated successfully./
{:ok, state}
end
and_ ~r/^I should be redirected back to the property page$/, fn state ->
advertisement = List.first(state[:advertisements])
assert current_path() == "/properties/#{advertisement.reference}"
{:ok, state}
end
and_ ~r/^I should see the updated property details$/, fn state ->
advertisement = state[:updated_data]
assert visible_in_page? ~r/#{advertisement.title}/
assert visible_in_page? ~r/#{advertisement.description}/
assert visible_in_page? ~r/#{advertisement.type}/i
assert visible_in_page? ~r/#{advertisement.property_type}/i
assert visible_in_page? ~r/#{advertisement.location}/
assert visible_in_page? ~r/#{advertisement.price}/
assert visible_in_page? ~r/#{advertisement.room_count}/
assert visible_in_page? ~r/#{advertisement.area}/
assert visible_in_page? ~r/#{advertisement.floor}/
assert visible_in_page? ~r/#{advertisement.state}/i
{:ok, state}
end
then_ ~r/^I should see error message$/, fn state ->
assert visible_in_page? ~r/Oops, something went wrong! Please check the errors below./
{:ok, state}
end
and_ ~r/^I should see the edit page again$/, fn state ->
assert visible_in_page? ~r/Edit Property/
{:ok, state}
end
and_ ~r/^I am logged in as a random user who does not own the advertisement$/, fn state ->
setup_session(state[:random_email], state[:random_password])
{:ok, state}
end
and_ ~r/^I navigate to the property's details page$/, fn state ->
advertisement = List.first(state[:advertisements])
click({:id, "edit-#{advertisement.reference}"})
{:ok, state}
end
then_ ~r/^I should not see an edit button$/, fn state ->
assert not visible_in_page? ~r/Edit Property/
{: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
# https://stackoverflow.com/a/49861811
defp select_dropdown(drop_down_id, option) do
find_element(:css, "##{drop_down_id} option[value='#{option}']") |> click()
end
end
Loading