towerops/test/towerops_web/live/dashboard_live_test.exs
Graham McIntire 7d4f7dd7d4
Add active page navigation highlighting
- Add active_page attribute to authenticated layout
- Update nav_link component to show active state with border and text styling
- Update all LiveView templates to pass active_page parameter
- Fix datetime truncation issues in tests and schemas for :utc_datetime fields
- Fix dashboard test assertions for equipment status text
2026-01-03 12:28:26 -06:00

133 lines
3.9 KiB
Elixir

defmodule ToweropsWeb.DashboardLiveTest do
use ToweropsWeb.ConnCase
import Phoenix.LiveViewTest
setup :register_and_log_in_user
setup %{user: user} do
{:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)
{:ok, site} =
Towerops.Sites.create_site(%{
name: "Test Site",
organization_id: organization.id
})
%{organization: organization, site: site}
end
describe "Dashboard" do
test "displays organization name and stats", %{conn: conn, organization: organization} do
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")
assert html =~ organization.name
assert html =~ "Sites"
assert html =~ "Equipment"
assert html =~ "Active Alerts"
end
test "displays correct site count", %{conn: conn, organization: organization} do
# Create additional sites
{:ok, _site2} =
Towerops.Sites.create_site(%{
name: "Site 2",
organization_id: organization.id
})
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")
assert html =~ "Sites"
end
test "displays correct equipment counts", %{conn: conn, organization: organization, site: site} do
# Create equipment with different statuses
{:ok, equipment_up} =
Towerops.Equipment.create_equipment(%{
name: "Router 1",
ip_address: "192.168.1.1",
site_id: site.id,
monitoring_enabled: true
})
Towerops.Equipment.update_equipment_status(equipment_up, :up)
{:ok, equipment_down} =
Towerops.Equipment.create_equipment(%{
name: "Router 2",
ip_address: "192.168.1.2",
site_id: site.id,
monitoring_enabled: true
})
Towerops.Equipment.update_equipment_status(equipment_down, :down)
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")
assert html =~ "Equipment"
assert html =~ "Up"
assert html =~ "Down"
end
test "displays active alerts count", %{conn: conn, organization: organization, site: site} do
{:ok, equipment} =
Towerops.Equipment.create_equipment(%{
name: "Router 1",
ip_address: "192.168.1.1",
site_id: site.id
})
# Create an active alert
{:ok, _alert} =
Towerops.Alerts.create_alert(%{
equipment_id: equipment.id,
alert_type: :equipment_down,
triggered_at: DateTime.utc_now(),
message: "Equipment is down"
})
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")
assert html =~ "Active Alerts"
end
test "updates in real-time when new alert is triggered", %{
conn: conn,
organization: organization,
site: site
} do
{:ok, equipment} =
Towerops.Equipment.create_equipment(%{
name: "Router 1",
ip_address: "192.168.1.1",
site_id: site.id
})
{:ok, view, _html} = live(conn, ~p"/orgs/#{organization.slug}")
# Trigger a new alert via PubSub
Phoenix.PubSub.broadcast(
Towerops.PubSub,
"alerts:new",
{:new_alert, equipment.id, :equipment_down}
)
# View should update
assert render(view)
end
test "stat cards are clickable links", %{conn: conn, organization: organization} do
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")
# Check for navigation links
assert html =~ ~p"/orgs/#{organization.slug}/sites"
assert html =~ ~p"/orgs/#{organization.slug}/equipment"
assert html =~ ~p"/orgs/#{organization.slug}/alerts"
end
test "requires authentication", %{organization: organization} do
conn = build_conn()
{:error, redirect} = live(conn, ~p"/orgs/#{organization.slug}")
assert {:redirect, %{to: path}} = redirect
assert path == ~p"/users/log-in"
end
end
end