From 9cb7633a14447007d7a62c0b924af51821f30b7d Mon Sep 17 00:00:00 2001 From: Elin Olsson Date: Tue, 27 Aug 2024 23:12:18 +0200 Subject: [PATCH] Add tests for dashboard map (#1471) * Add some test cases for dashboard map * Make sure invalid locations doesn't render marker. --- lib/nerves_hub_web/live/dashboard/index.ex | 10 +- .../live/dashboard/index.html.heex | 3 +- .../live/product/dashboard_test.exs | 106 ++++++++++++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 test/nerves_hub_web/live/product/dashboard_test.exs diff --git a/lib/nerves_hub_web/live/dashboard/index.ex b/lib/nerves_hub_web/live/dashboard/index.ex index ed86cd5d8..a8c31f17c 100644 --- a/lib/nerves_hub_web/live/dashboard/index.ex +++ b/lib/nerves_hub_web/live/dashboard/index.ex @@ -76,7 +76,7 @@ defmodule NervesHubWeb.Live.Dashboard.Index do socket |> assign(:devices, devices) - |> assign(:map_markers, Jason.encode!(map_markers)) + |> assign(:map_markers, map_markers) end defp generate_map_marker( @@ -84,17 +84,19 @@ defmodule NervesHubWeb.Live.Dashboard.Index do id: id, identifier: identifier, connection_status: connection_status, - connection_metadata: %{"location" => location} + connection_metadata: %{ + "location" => %{"longitude" => longitude, "latitude" => latitude} + } }, markers ) - when map_size(location) > 0 do + when is_number(longitude) and is_number(latitude) do new_marker = %{ id: id, identifier: identifier, status: get_connection_status(connection_status), - location: location + location: %{"longitude" => longitude, "latitude" => latitude} } [new_marker | markers] diff --git a/lib/nerves_hub_web/live/dashboard/index.html.heex b/lib/nerves_hub_web/live/dashboard/index.html.heex index 132f43809..e3275623e 100644 --- a/lib/nerves_hub_web/live/dashboard/index.html.heex +++ b/lib/nerves_hub_web/live/dashboard/index.html.heex @@ -32,7 +32,8 @@
<% else %> -
+

Showing <%= length(@map_markers) %> devices with location data.

+

diff --git a/test/nerves_hub_web/live/product/dashboard_test.exs b/test/nerves_hub_web/live/product/dashboard_test.exs new file mode 100644 index 000000000..837a2594b --- /dev/null +++ b/test/nerves_hub_web/live/product/dashboard_test.exs @@ -0,0 +1,106 @@ +defmodule NervesHubWeb.Live.Product.DashboardTest do + use NervesHubWeb.ConnCase.Browser, async: true + + alias NervesHub.Devices + alias NervesHub.Devices.Device + alias NervesHubWeb.Endpoint + + alias Phoenix.Socket.Broadcast + + @valid_location %{ + "latitude" => 60.6204, + "longitude" => 16.7697, + "source" => "geoip" + } + + @invalid_location %{ + "latitude" => 60.6204, + "source" => "geoip" + } + + setup %{fixture: %{device: device}} do + Endpoint.subscribe("device:#{device.id}") + end + + describe "dashboard map" do + test "assert devices with location data renders map and marker info", %{ + conn: conn, + org: org, + product: product, + device: device + } do + assert {:ok, %Device{}} = + Devices.update_device(device, %{ + connection_metadata: %{"location" => @valid_location} + }) + + conn + |> visit("/org/#{org.name}/#{product.name}/dashboard") + |> assert_has("#map") + |> assert_has("#map-markers") + end + + test "assert page render without locations ", %{ + conn: conn, + org: org, + product: product, + device: device + } do + assert device.connection_metadata["location"] == nil + + conn + |> visit("/org/#{org.name}/#{product.name}/dashboard") + |> assert_has("h3", text: "#{product.name} doesn’t have any devices with location data.") + end + + test "assert page render without devices", %{ + conn: conn, + org: org, + product: product, + device: device + } do + assert {:ok, _} = Devices.delete_device(device) + + conn + |> visit("/org/#{org.name}/#{product.name}/dashboard") + |> assert_has("h3", text: "#{product.name} doesn’t have any devices yet.") + end + + test "assert page still render with invalid location data", %{ + conn: conn, + org: org, + product: product, + device: device + } do + assert {:ok, %Device{}} = + Devices.update_device(device, %{ + connection_metadata: %{"location" => @invalid_location} + }) + + conn + |> visit("/org/#{org.name}/#{product.name}/dashboard") + |> assert_has("h3", text: "#{product.name} doesn’t have any devices with location data.") + end + + test "handle_info - location:update", %{ + conn: conn, + org: org, + product: product, + device: device + } do + conn + |> visit("/org/#{org.name}/#{product.name}/dashboard") + |> assert_has("#map") + |> assert_has("h3", text: "#{product.name} doesn’t have any devices with location data.") + |> unwrap(fn view -> + {:ok, _} = + Devices.update_device(device, %{connection_metadata: %{"location" => @valid_location}}) + + send(view.pid, %Broadcast{event: "location:updated", payload: @valid_location}) + render(view) + end) + |> refute_has("h3", text: "#{product.name} doesn’t have any devices with location data.") + |> assert_has("#map-markers") + end + end +end