package web import ( "strings" "testing" ) func TestRankedBarsLabelEveryRowDirectly(t *testing.T) { // The label is the point of this chart: the question is which person or // which model, not what shape the distribution is. Rotated or truncated // axis labels would say the chart matters more than reading it. out := string(HBarChart([]Row{ {Label: "ilker@example.com", Value: 1200}, {Label: "deniz@example.com", Value: 300}, }, "--viz-1", "tokens")) for _, want := range []string{"ilker@example.com", "deniz@example.com", "1.2k", "300"} { if !strings.Contains(out, want) { t.Errorf("the chart does not show %q:\n%s", want, out) } } } func TestASmallValueStillDrawsAMark(t *testing.T) { // A bar that rounds to zero width reads as no data rather than as a small // number, which is a different and wrong claim. out := string(HBarChart([]Row{ {Label: "huge", Value: 1_000_000}, {Label: "tiny", Value: 1}, }, "--viz-1", "")) // Two paths, one per row: the tiny one is still drawn. if n := strings.Count(out, " does // that with no JavaScript, which is the right trade for a page this project // serves from a single binary. out := string(HBarChart([]Row{ {Label: "a", Value: 1}, {Label: "b", Value: 2}, }, "--viz-1", "tokens")) if n := strings.Count(out, ""); n != 2 { t.Errorf("%d hover titles for two bars", n) } } func TestALongLabelIsTrimmedRatherThanOverflowing(t *testing.T) { long := strings.Repeat("a", 80) + "@example.com" out := string(HBarChart([]Row{{Label: long, Value: 1}}, "--viz-1", "")) // Trimmed where it is drawn, in full where it is hovered: the plot keeps // its geometry and the reader can still find out whose row it is. drawn := out[strings.Index(out, "<text"):strings.Index(out, "</text>")] if strings.Contains(drawn, long) { t.Error("a very long label was drawn in full and would overrun the plot") } if !strings.Contains(out, long) { t.Error("the full label is not available anywhere, not even on hover") } if !strings.Contains(out, "…") { t.Error("the label was cut without saying it had been") } } func TestASparklineOfNothingIsNotDrawn(t *testing.T) { // One point is not a trend, and a single dot invites reading one. if got := Sparkline([]float64{5}, ""); got != "" { t.Errorf("a one-point sparkline rendered: %q", got) } if got := Sparkline(nil, ""); got != "" { t.Errorf("an empty sparkline rendered: %q", got) } } func TestAFlatSparklineIsDrawnAlongTheBottom(t *testing.T) { // No traffic is a real answer. An empty box would read as a broken chart. out := string(Sparkline([]float64{0, 0, 0}, "")) if !strings.Contains(out, "<polyline") { t.Errorf("a flat series did not draw: %q", out) } } func TestTheChartScalesToItsContainer(t *testing.T) { // The dashboard is read on laptops and phones. A fixed pixel width is how a // table ends up scrolling sideways. out := string(HBarChart([]Row{{Label: "a", Value: 1}}, "", "")) if !strings.Contains(out, "viewBox") { t.Error("no viewBox, so the chart cannot scale") } if strings.Contains(out, `width="720"`) { t.Error("a fixed pixel width was set on the chart") } }