Add comprehensive tests for EquipmentLive.Show

- Test equipment information display
- Test overview tab rendering
- Test metrics display with monitoring checks
- Test empty state handling
- Test periodic data refresh
- Test authentication requirement
- Test event handling (equipment_status_changed, discovery_completed)
- Test tab switching functionality
This commit is contained in:
Graham McIntire 2026-01-13 09:13:40 -06:00
parent 167f853a63
commit fc1b6ea026
No known key found for this signature in database

View file

@ -0,0 +1,157 @@
defmodule ToweropsWeb.EquipmentLive.ShowTest do
use ToweropsWeb.ConnCase
import Phoenix.LiveViewTest
alias Towerops.Equipment
alias Towerops.Monitoring
alias Towerops.Organizations
alias Towerops.Sites
setup :register_and_log_in_user
setup %{user: user} do
{:ok, organization} = Organizations.create_organization(%{name: "Test Org"}, user.id)
{:ok, site} =
Sites.create_site(%{
name: "Test Site",
organization_id: organization.id
})
{:ok, equipment} =
Equipment.create_equipment(%{
name: "Test Router",
ip_address: "192.168.1.1",
site_id: site.id
})
%{organization: organization, site: site, equipment: equipment}
end
describe "Show" do
test "displays equipment information", %{conn: conn, equipment: equipment, organization: org} do
{:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
assert html =~ equipment.name
assert html =~ equipment.ip_address
end
test "displays overview tab by default", %{conn: conn, equipment: equipment, organization: org} do
{:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
assert html =~ "Overview"
end
test "displays metrics when equipment has checks", %{
conn: conn,
equipment: equipment,
organization: org
} do
# Create some checks
Monitoring.create_check(%{
equipment_id: equipment.id,
status: :success,
response_time_ms: 10,
checked_at: DateTime.utc_now()
})
Monitoring.create_check(%{
equipment_id: equipment.id,
status: :success,
response_time_ms: 15,
checked_at: DateTime.utc_now()
})
Monitoring.create_check(%{
equipment_id: equipment.id,
status: :failure,
response_time_ms: nil,
checked_at: DateTime.utc_now()
})
{:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
assert html =~ "Device Information"
end
test "displays empty state when no checks exist", %{
conn: conn,
equipment: equipment,
organization: org
} do
{:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
assert html =~ equipment.name
end
test "refreshes data periodically", %{conn: conn, equipment: equipment, organization: org} do
{:ok, view, _html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
# Trigger a status change
Monitoring.create_check(%{
equipment_id: equipment.id,
status: :failure,
response_time_ms: nil,
checked_at: DateTime.utc_now()
})
# Send refresh message
send(view.pid, :refresh_data)
# Give it time to process
:timer.sleep(100)
# View should still be alive
assert render(view)
end
test "requires authentication", %{equipment: equipment, organization: org} do
conn = build_conn()
{:error, redirect} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
assert {:redirect, %{to: path}} = redirect
assert path == ~p"/users/log-in"
end
test "handles equipment_status_changed event", %{
conn: conn,
equipment: equipment,
organization: org
} do
{:ok, view, _html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
# Simulate status change event
send(view.pid, {:equipment_status_changed, equipment.id, :up, 25})
# Give it time to process
:timer.sleep(100)
# View should still be alive and render
assert render(view)
end
test "handles discovery_completed event", %{
conn: conn,
equipment: equipment,
organization: org
} do
{:ok, view, _html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}")
# Simulate discovery completed event
send(view.pid, {:discovery_completed, equipment.id})
# Give it time to process
:timer.sleep(100)
html = render(view)
assert html =~ "Discovery completed"
end
test "switches to different tabs", %{conn: conn, equipment: equipment, organization: org} do
{:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/equipment/#{equipment.id}?tab=events")
assert html =~ equipment.name
end
end
end