HTML-D3

 view release on metacpan or  search on metacpan

t/snippet_treemap.t  view on Meta::CPAN

like($fixed, qr/width="800"/, 'Default: fixed pixel width');

# ── validation errors ─────────────────────────────────────────────────────────
throws_ok { $chart->render_treemap_snippet('not-ref') }
	qr/Data must be an array of hashes or a hash tree/, 'Dies on plain scalar';
throws_ok { $chart->render_treemap_snippet([1,2,3]) }
	qr/Each flat row must be a hash reference/, 'Dies on non-hashref flat row';
throws_ok { $chart->render_treemap_snippet([ { value => 10 } ]) }
	qr/Each flat row must have a 'path' key/, 'Dies on missing path key';
throws_ok { $chart->render_treemap_snippet([ { path => 'A/B', value => 'bad' } ]) }
	qr/Each flat row must have a numeric 'value' key/, 'Dies on non-numeric value';

# ── Colour by value (0.29) ───────────────────────────────────────────────────
# A tile may carry color_value, a second measure that shades it from a
# sequential scheme in place of its group's colour.

subtest 'without color_value the tiles take their group colour' => sub {
	my $h = $chart->render_treemap_snippet(\@flat_data)->{html};
	unlike($h, qr/scaleSequential/, 'no sequential scale');
	unlike($h, qr/tm-legend/,       'no colour legend');
	like($h, qr/\.size\(\[800, 500\]\)/, 'tiles use the whole height');
	like($h, qr/return color\(anc\.id/, 'fill is the top-level group colour');
	like($h, qr/\.attr\("fill","#fff"\)/, 'labels stay white');
};

my @coloured = (
	{ path => 'Root/A/X', value => 420, color_value => 0.10 },
	{ path => 'Root/A/Y', value => 180, color_value => 0.35 },
	{ path => 'Root/B/Z', value => 300, color_value => -0.05 },
	{ path => 'Root/B/W', value => 100 },
);

subtest 'color_value shades the tiles from a sequential scheme' => sub {
	my $h = $chart->render_treemap_snippet(\@coloured)->{html};
	like($h, qr/d3\.scaleSequential\(d3\.interpolateYlOrRd\)/, 'YlOrRd by default');
	like($h, qr/"color_value":0\.35/, 'colour values reach the script');
	unlike($h, qr/"path":"Root\/B\/W"[^}]*color_value/, 'a tile without one carries none');
	like($h, qr/return v === null \? "#ccc" : cscale\(v\)/, 'a tile without one is grey');
	like($h, qr/\.size\(\[800, 466\]\)/, 'tiles leave room for the legend');
	like($h, qr/class","tm-legend"/, 'legend drawn');
	like($h, qr/linearGradient"\)\.attr\("id","treemap_cgrad"\)/, 'gradient named after the chart');
	like($h, qr/var colorLabel = "Colour";/, 'legend label defaults to Colour');
	like($h, qr/d3\.lab\(fillOf\(d\)\)\.l > 65/, 'labels darken on light tiles');
	like($h, qr/esc\(colorLabel\)\+": "\+esc\(cvOf\(d\)\)/, 'tooltip shows the colour value, escaped');
};

subtest 'color_scheme and color_label' => sub {
	my $h = $chart->render_treemap_snippet(\@coloured,
		{ color_scheme => 'Blues', color_label => 'Margin</script><b>', id => 'tm2' })->{html};
	like($h, qr/d3\.scaleSequential\(d3\.interpolateBlues\)/, 'Blues');
	like($h, qr/var colorLabel = "Margin\\u003c\/script>\\u003cb>";/, 'label escaped for the script');
	like($h, qr/"id","tm2_cgrad"/, 'gradient id follows the chart id');

	throws_ok { $chart->render_treemap_snippet(\@coloured, { color_scheme => 'Viridis' }) }
		qr/Unknown color_scheme: Viridis/, 'unknown color_scheme dies when colouring by value';
	lives_ok { $chart->render_treemap_snippet(\@flat_data, { color_scheme => 'Viridis' }) }
		'color_scheme is not read without colour values';
};

subtest 'a bad color_value dies' => sub {
	for my $bad ('high', 'NaN', 'inf', '') {
		throws_ok {
			$chart->render_treemap_snippet([ { path => 'R/A', value => 1, color_value => $bad } ])
		} qr/Each flat row's 'color_value' must be numeric/, "flat: '$bad'";
	}
	throws_ok {
		$chart->render_treemap_snippet({ name => 'R', children => [ { name => 'A', value => 1, color_value => 'x' } ] })
	} qr/Each node's 'color_value' must be numeric/, 'tree: a bad leaf';
};

subtest 'a hash tree is coloured by its leaves' => sub {
	my $tree = {
		name => 'Root',
		children => [
			{ name => 'A', children => [ { name => 'X', value => 4, color_value => 1 } ] },
			{ name => 'B', value => 2, color_value => 3 },
		],
	};
	my $h = $chart->render_treemap_snippet($tree)->{html};
	like($h, qr/scaleSequential/, 'sequential scale');
	like($h, qr/d3\.hierarchy\(rawData\)/, 'tree form');
	is($tree->{children}[1]{color_value}, 3, 'the caller\'s tree is not changed');
};

subtest 'tile names are escaped in the tooltip' => sub {
	my $h = $chart->render_treemap_snippet(\@flat_data)->{html};
	like($h, qr/function esc\(s\)/, 'esc() defined');
	like($h, qr/\.html\("<b>"\+esc\(name\)\+/, 'name escaped');
	unlike($h, qr/\.html\("<b>"\+name\+/, 'never raw');
};



( run in 1.077 second using v1.01-cache-2.11-cpan-60884a8df76 )