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 bars, one per row, and the tiny one has a width. if n := strings.Count(out, `class="rk-bar"`); n != 2 { t.Errorf("drew %d marks for two rows", n) } if strings.Contains(out, "width:0.0%") { t.Error("the tiny value was drawn with no width") } } func TestAnEmptyChartSaysSoRatherThanRenderingNothing(t *testing.T) { // A missing chart reads as a broken page. out := string(HBarChart(nil, "--viz-1", "")) if !strings.Contains(out, "Nothing recorded") { t.Errorf("an empty series rendered as %q", out) } } func TestChartTextUsesTextColoursNotTheSeriesColour(t *testing.T) { // Values and labels wear text tokens; the mark beside them carries the // identity. Colouring the text too makes the palette do two jobs and reads // badly for anyone the hue separation was chosen to protect. out := string(HBarChart([]Row{{Label: "a", Value: 5}}, "--viz-2", "req")) if strings.Count(out, "var(--viz-2)") != 1 || !strings.Contains(out, `class="rk-bar"`) { t.Errorf("the series colour should be on the bar and nowhere else:\n%s", out) } } func TestEveryBarCarriesItsOwnHoverTitle(t *testing.T) { // The chart is in a browser, so it should answer a pointer. A