Files
MujicaUI/stat_card_test.go
T
2026-10-05 14:45:49 -04:00

74 lines
2.1 KiB
Go

package mujica
import (
"testing"
"github.com/egoist/mygo/ui"
)
func TestStatCardValueChangeTrend(t *testing.T) {
opts := StatCardOptions{Label: "Requests", Change: 0.124, HasChange: true, Trend: []float64{3, 5, 4, 8, 9}}
tt := ui.NewTester(func(c *ui.Context) {
Use(c, Settings{})
StatCard(c, "12,480", opts)
}, 400, 200)
for _, s := range []string{"Requests", "12,480", "+12.4%"} {
if !tt.HasText(s) {
t.Fatalf("missing %q in %q", s, tt.Texts())
}
}
opts.Change = -0.05
tt.Frame()
if !tt.HasText("-5.0%") {
t.Fatalf("fall: %q", tt.Texts())
}
opts.HasChange = false
tt.Frame()
if tt.HasText("-5.0%") {
t.Fatal("change shown without HasChange")
}
}
func TestStatCardLoadingErrorSameSize(t *testing.T) {
status, retries := DataReady, 0
var h []float32
tt := ui.NewTester(func(c *ui.Context) {
Use(c, Settings{})
ui.Column(c).AlignItems(ui.Start).Children(func() {
r := StatCard(c, "42 ms", StatCardOptions{Label: "Latency", Status: status, Error: "timeout"})
h = append(h, r.Element.Bounds().H)
if r.Retry {
retries++
}
})
}, 400, 300)
tt.Frame()
ready := h[len(h)-1]
status = DataLoading
tt.Frame()
tt.Frame()
if !tt.HasText("Loading") || h[len(h)-1] != ready {
t.Fatalf("loading: %q height %v vs %v", tt.Texts(), h[len(h)-1], ready)
}
status = DataFailed
tt.Frame()
tt.Frame()
if !tt.HasText("timeout") || h[len(h)-1] != ready {
t.Fatalf("failed: %q height %v vs %v", tt.Texts(), h[len(h)-1], ready)
}
_ = tt.Click("Retry")
if retries != 1 {
t.Fatalf("retries %d", retries)
}
}
func TestStatCardSnapshot(t *testing.T) {
snapshot(t, "stat_card", 640, 150, func(c *ui.Context) {
ui.Row(c).Padding(16).Gap(12).Children(func() {
StatCard(c, "12,480", StatCardOptions{Label: "Requests", Change: 0.124, HasChange: true, Trend: []float64{3, 5, 4, 8, 6, 9}})
StatCard(c, "182 ms", StatCardOptions{Label: "p95 latency", Change: 0.08, HasChange: true, LowerIsBetter: true, Trend: []float64{120, 140, 130, 182}})
StatCard(c, "", StatCardOptions{Label: "Errors", Status: DataFailed, Error: "Source unreachable"})
})
})
}