Skip to content

Commit c10b6dc

Browse files
committed
tests!
1 parent 3601ab6 commit c10b6dc

1 file changed

Lines changed: 39 additions & 34 deletions

File tree

test/scales/scales-test.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,19 +2429,42 @@ describe("Plot.scale({projection})", () => {
24292429
}
24302430
});
24312431

2432-
it("matches plot.scale('projection') given explicit dimensions", () => {
2433-
for (const [type, height] of [
2434-
["mercator", 640],
2435-
["equal-earth", 311],
2436-
["equirectangular", 320]
2432+
it("matches plot.scale('projection')", () => {
2433+
for (const type of ["mercator", "equal-earth", "equirectangular"]) {
2434+
const p1 = Plot.plot({projection: type}).scale("projection");
2435+
const p2 = Plot.scale({projection: {type}});
2436+
assert.allCloseTo(p1.apply([-1.55, 47.22]), p2.apply([-1.55, 47.22]));
2437+
}
2438+
});
2439+
2440+
it("matches plot.scale('projection') with explicit dimensions", () => {
2441+
for (const [type, width, height] of [
2442+
["mercator", 800, 500],
2443+
["equal-earth", 960, 400]
24372444
]) {
2438-
const plot = Plot.plot({width: 640, height, margin: 0, projection: type, marks: [Plot.graticule()]});
2439-
const p1 = plot.scale("projection");
2440-
const p2 = Plot.scale({projection: {type, width: 640, height}});
2445+
const p1 = Plot.plot({width, height, projection: type}).scale("projection");
2446+
const p2 = Plot.scale({projection: {type, width, height}});
2447+
assert.allCloseTo(p1.apply([-1.55, 47.22]), p2.apply([-1.55, 47.22]));
2448+
}
2449+
});
2450+
2451+
it("matches plot.scale('projection') with explicit margins", () => {
2452+
for (const type of ["mercator", "equal-earth"]) {
2453+
const p1 = Plot.plot({margin: 20, marginLeft: 40, projection: type}).scale("projection");
2454+
const p2 = Plot.scale({projection: {type, margin: 20, marginLeft: 40}});
24412455
assert.allCloseTo(p1.apply([-1.55, 47.22]), p2.apply([-1.55, 47.22]));
24422456
}
24432457
});
24442458

2459+
it("supports a custom projection factory", () => {
2460+
const sphere = {type: "Sphere"};
2461+
const factory = ({width, height}) => d3.geoOrthographic().fitExtent([[10, 10], [width - 10, height - 10]], sphere); // prettier-ignore
2462+
const p1 = Plot.scale({projection: {type: factory, width: 400, height: 400}});
2463+
const p2 = Plot.plot({width: 400, height: 400, projection: {type: factory}}).scale("projection");
2464+
assert.allCloseTo(p1.apply([0, 0]), p2.apply([0, 0]));
2465+
assert.allCloseTo(p1.invert(p1.apply([10, 20])), [10, 20]);
2466+
});
2467+
24452468
it("respects margins and insets", () => {
24462469
// standalone projection
24472470
const p1 = Plot.scale({projection: {type: "mercator", width: 640, height: 640, margin: 40, inset: 10}});
@@ -2452,7 +2475,7 @@ describe("Plot.scale({projection})", () => {
24522475
height: 640,
24532476
margin: 40,
24542477
projection: {type: "mercator", inset: 10},
2455-
marks: [Plot.graticule()]
2478+
marks: []
24562479
}).scale("projection");
24572480
assert.allCloseTo(p1.apply([-1.55, 47.22]), p2.apply([-1.55, 47.22]));
24582481
// reuse the standalone projection in a plot
@@ -2463,42 +2486,24 @@ describe("Plot.scale({projection})", () => {
24632486
it("supports domain", async () => {
24642487
const us = await d3.json("data/us-counties-10m.json");
24652488
const domain = topojson.feature(us, us.objects.nation);
2466-
const p1 = Plot.scale({projection: {type: "albers-usa", domain, width: 640, height: 400}});
2467-
const p2 = Plot.plot({
2468-
width: 640,
2469-
height: 400,
2470-
margin: 0,
2471-
projection: {type: "albers-usa", domain},
2472-
marks: [Plot.graticule()]
2473-
}).scale("projection");
2474-
assert.allCloseTo(p1.apply([-98, 39]), p2.apply([-98, 39])); // center of the US
2489+
const p1 = Plot.scale({projection: {type: "albers-usa", domain}});
2490+
const p2 = Plot.plot({projection: {type: "albers-usa", domain}}).scale("projection");
2491+
assert.allCloseTo(p1.apply([-98, 39]), p2.apply([-98, 39]));
24752492
assert.allCloseTo(p1.invert(p1.apply([-98, 39])), [-98, 39]);
24762493
});
24772494

24782495
it("supports a metric domain with reflect-y", async () => {
24792496
const house = await d3.json("data/westport-house.json");
2480-
const p1 = Plot.scale({projection: {type: "reflect-y", domain: house, width: 640, height: 400}});
2481-
const p2 = Plot.plot({
2482-
width: 640,
2483-
height: 400,
2484-
margin: 0,
2485-
projection: {type: "reflect-y", domain: house},
2486-
marks: [Plot.geo(house)]
2487-
}).scale("projection");
2497+
const p1 = Plot.scale({projection: {type: "reflect-y", domain: house}});
2498+
const p2 = Plot.plot({projection: {type: "reflect-y", domain: house}}).scale("projection");
24882499
assert.allCloseTo(p1.apply([200, 120]), p2.apply([200, 120]));
24892500
assert.allCloseTo(p1.invert(p1.apply([200, 120])), [200, 120]);
24902501
});
24912502

24922503
it("supports a metric domain with identity", async () => {
24932504
const house = await d3.json("data/westport-house.json");
2494-
const p1 = Plot.scale({projection: {type: "identity", domain: house, width: 640, height: 400}});
2495-
const p2 = Plot.plot({
2496-
width: 640,
2497-
height: 400,
2498-
margin: 0,
2499-
projection: {type: "identity", domain: house},
2500-
marks: [Plot.geo(house)]
2501-
}).scale("projection");
2505+
const p1 = Plot.scale({projection: {type: "identity", domain: house}});
2506+
const p2 = Plot.plot({projection: {type: "identity", domain: house}}).scale("projection");
25022507
assert.allCloseTo(p1.apply([200, 120]), p2.apply([200, 120]));
25032508
assert.allCloseTo(p1.invert(p1.apply([200, 120])), [200, 120]);
25042509
});

0 commit comments

Comments
 (0)