Skip to content

Commit dbf913b

Browse files
Filmbostock
andauthored
tests for coverage (#1825)
* stack rejects an invalid order * these tests used the default labelAnchor * test curve tension * coverage: valid and invalid curves * move curve tests to a features directory * test channel scales: false, invalid * invalid domain sort options * The aspectRatio option rejects unsupported scale type * test style * test polish * add missing test --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 075bdc8 commit dbf913b

14 files changed

+219
-36
lines changed

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@rollup/plugin-node-resolve": "^15.0.1",
5454
"@rollup/plugin-terser": "^0.4.0",
5555
"@types/d3": "^7.4.0",
56+
"@types/mocha": "^10.0.1",
5657
"@types/node": "^20.5.0",
5758
"@typescript-eslint/eslint-plugin": "^6.0.0",
5859
"@typescript-eslint/parser": "^6.0.0",
@@ -77,8 +78,13 @@
7778
},
7879
"c8": {
7980
"all": true,
80-
"include": ["src/**/*.js"],
81-
"reporter": ["text", "lcov"]
81+
"include": [
82+
"src/**/*.js"
83+
],
84+
"reporter": [
85+
"text",
86+
"lcov"
87+
]
8288
},
8389
"dependencies": {
8490
"d3": "^7.8.0",

test/mark-test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Plot from "@observablehq/plot";
2+
import assert from "assert";
3+
import it from "./jsdom.js";
4+
5+
it("mark(data, {sort}) needs y1 and y2 when sorting by height", () => {
6+
assert.throws(() => Plot.dot([], {sort: {x: "height"}}).plot(), /^Error: missing channel: y1$/);
7+
assert.throws(() => Plot.dot([], {channels: {y1: "1"}, sort: {x: "height"}}).plot(), /^Error: missing channel: y2$/);
8+
});
9+
10+
it("mark(data, {sort}) needs x1 and x2 when sorting by width", () => {
11+
assert.throws(() => Plot.dot([], {sort: {y: "width"}}).plot(), /^Error: missing channel: x1$/);
12+
assert.throws(() => Plot.dot([], {channels: {x1: "1"}, sort: {y: "width"}}).plot(), /^Error: missing channel: x2$/);
13+
});
14+
15+
it("mark(data, {sort}) rejects an invalid order", () => {
16+
assert.throws(() => Plot.dotY([0, 1], {sort: {y: {value: "x", order: "neo" as any}}}).plot(), /^Error: invalid order: neo$/); // prettier-ignore
17+
});

test/marks/line-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Plot from "@observablehq/plot";
22
import {curveStep} from "d3";
3+
import {curveAuto} from "../../src/curve.js";
34
import assert from "assert";
45

56
it("line() has the expected defaults", () => {
@@ -114,3 +115,12 @@ it("line(data, {curve}) specifies a named curve or function", () => {
114115
assert.strictEqual(Plot.line(undefined, {curve: "step"}).curve, curveStep);
115116
assert.strictEqual(Plot.line(undefined, {curve: curveStep}).curve, curveStep);
116117
});
118+
119+
it("line(data, {curve}) rejects an invalid curve", () => {
120+
assert.throws(() => Plot.lineY([], {y: 1, curve: "neo"}), /^Error: unknown curve: neo$/);
121+
assert.throws(() => Plot.lineY([], {y: 1, curve: 42}), /^Error: unknown curve: 42$/);
122+
});
123+
124+
it("line(data, {curve}) accepts the explicit auto curve", () => {
125+
assert.strictEqual(Plot.lineY([], {y: 1, curve: "auto"}).curve, curveAuto);
126+
});

test/output/curves.svg

Lines changed: 83 additions & 0 deletions
Loading

test/plot-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as Plot from "@observablehq/plot";
2+
import assert from "assert";
3+
import it from "./jsdom.js";
4+
5+
it("plot({aspectRatio}) rejects unsupported scale types", () => {
6+
assert.throws(() => Plot.dot([]).plot({aspectRatio: true, x: {type: "symlog"}}), /^Error: unsupported x scale for aspectRatio: symlog$/); // prettier-ignore
7+
assert.throws(() => Plot.dot([]).plot({aspectRatio: true, y: {type: "symlog"}}), /^Error: unsupported y scale for aspectRatio: symlog$/); // prettier-ignore
8+
});

test/plots/curves.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export async function curves() {
5+
const random = d3.randomLcg(42);
6+
const values = d3.ticks(0, 1, 11).map((t) => {
7+
const r = 1 + 2 * random();
8+
return [r * Math.cos(t * 2 * Math.PI), r * Math.sin(t * 2 * Math.PI)];
9+
});
10+
return Plot.plot({
11+
width: 500,
12+
axis: null,
13+
aspectRatio: true,
14+
inset: 10,
15+
marks: [
16+
d3
17+
.ticks(0, 1, 4)
18+
.map((tension) => [
19+
Plot.line(values, {curve: "bundle", tension, stroke: "red", mixBlendMode: "multiply"}),
20+
Plot.line(values, {curve: "cardinal-closed", tension, stroke: "green", mixBlendMode: "multiply"}),
21+
Plot.line(values, {curve: "catmull-rom-closed", tension, stroke: "blue", mixBlendMode: "multiply"})
22+
]),
23+
Plot.dot(values, {stroke: "white", fill: "black"})
24+
]
25+
});
26+
}

test/plots/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export * from "./crimean-war-line.js";
5858
export * from "./crimean-war-overlapped.js";
5959
export * from "./crimean-war-stacked.js";
6060
export * from "./crosshair.js";
61+
export * from "./curves.js";
6162
export * from "./d3-survey-2015-comfort.js";
6263
export * from "./d3-survey-2015-why.js";
6364
export * from "./darker-dodge.js";

test/plots/us-congress-age-color-explicit.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@ export async function usCongressAgeColorExplicit() {
55
const data = await d3.csv<any>("data/us-congress-members.csv", d3.autoType);
66
return Plot.plot({
77
height: 300,
8-
x: {
9-
nice: true,
10-
label: "Age",
11-
labelAnchor: "right"
12-
},
13-
y: {
14-
grid: true,
15-
label: "Frequency"
16-
},
8+
x: {nice: true, label: "Age"},
9+
y: {grid: true, label: "Frequency"},
1710
marks: [
1811
Plot.dot(
1912
data,

test/plots/us-congress-age-gender.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ export async function usCongressAgeGender() {
55
const data = await d3.csv<any>("data/us-congress-members.csv", d3.autoType);
66
return Plot.plot({
77
height: 300,
8-
x: {
9-
nice: true,
10-
label: "Age",
11-
labelAnchor: "right"
12-
},
8+
x: {nice: true, label: "Age"},
139
y: {
1410
grid: true,
1511
label: "← Women · Men →",

test/plots/us-congress-age-symbol-explicit.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@ export async function usCongressAgeSymbolExplicit() {
55
const data = await d3.csv<any>("data/us-congress-members.csv", d3.autoType);
66
return Plot.plot({
77
height: 300,
8-
x: {
9-
nice: true,
10-
label: "Age",
11-
labelAnchor: "right"
12-
},
13-
y: {
14-
grid: true,
15-
label: "Frequency"
16-
},
8+
x: {nice: true, label: "Age"},
9+
y: {grid: true, label: "Frequency"},
1710
marks: [
1811
Plot.dot(
1912
data,

test/plots/us-congress-age.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@ export async function usCongressAge() {
55
const data = await d3.csv<any>("data/us-congress-members.csv", d3.autoType);
66
return Plot.plot({
77
height: 300,
8-
x: {
9-
nice: true,
10-
label: "Age",
11-
labelAnchor: "right"
12-
},
13-
y: {
14-
grid: true,
15-
label: "Frequency"
16-
},
8+
x: {nice: true, label: "Age"},
9+
y: {grid: true, label: "Frequency"},
1710
marks: [
1811
Plot.dot(data, Plot.stackY2({x: (d) => 2021 - d.birth, fill: "currentColor", title: "full_name"})),
1912
Plot.ruleY([0])

0 commit comments

Comments
 (0)