|
1 | 1 | import * as Plot from "@observablehq/plot"; |
2 | 2 | import assert from "assert"; |
| 3 | +import * as d3 from "d3"; |
3 | 4 |
|
4 | 5 | it("Plot.group does not return unspecified options", () => { |
5 | 6 | const A = Plot.group({}); |
@@ -34,3 +35,67 @@ it("Plot.group does return specified options", () => { |
34 | 35 | assert.strictEqual("fill" in C, false); |
35 | 36 | assert.strictEqual("stroke" in C, false); |
36 | 37 | }); |
| 38 | + |
| 39 | +it("Plot.group’s min reducer does not coerce numbers and uses natural order", () => { |
| 40 | + const group = Plot.groupZ({y: "min"}, {y: "y"}); |
| 41 | + const data = [{y: "10"}, {y: "4"}, {y: "2"}]; |
| 42 | + const facets = [d3.range(data.length)]; |
| 43 | + const {data: groupData} = group.transform(data, facets); |
| 44 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), ["10"]); |
| 45 | +}); |
| 46 | + |
| 47 | +it("Plot.group’s max reducer does not coerce numbers and uses natural order", () => { |
| 48 | + const group = Plot.groupZ({y: "max"}, {y: "y"}); |
| 49 | + const data = [{y: "10"}, {y: "4"}, {y: "2"}]; |
| 50 | + const facets = [d3.range(data.length)]; |
| 51 | + const {data: groupData} = group.transform(data, facets); |
| 52 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), ["4"]); |
| 53 | +}); |
| 54 | + |
| 55 | +it("Plot.group’s percentile reducer coerces numbers", () => { |
| 56 | + const group = Plot.groupZ({y: "p00"}, {y: "y"}); |
| 57 | + const data = [{y: "1"}, {y: "4"}, {y: "2"}]; |
| 58 | + const facets = [d3.range(data.length)]; |
| 59 | + const {data: groupData} = group.transform(data, facets); |
| 60 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), [1]); |
| 61 | +}); |
| 62 | + |
| 63 | +it("Plot.group’s median reducer coerces numbers", () => { |
| 64 | + const group = Plot.groupZ({y: "median"}, {y: "y"}); |
| 65 | + const data = [{y: "1"}, {y: "4"}, {y: "2"}]; |
| 66 | + const facets = [d3.range(data.length)]; |
| 67 | + const {data: groupData} = group.transform(data, facets); |
| 68 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), [2]); |
| 69 | +}); |
| 70 | + |
| 71 | +it("Plot.group’s median reducer ignores NaN, undefined, and null", () => { |
| 72 | + const group = Plot.groupZ({y: "median"}, {y: "y"}); |
| 73 | + const data = [{y: "1"}, {y: "4"}, {y: null}, {y: null}, {y: NaN}, {}, {y: "2"}]; |
| 74 | + const facets = [d3.range(data.length)]; |
| 75 | + const {data: groupData} = group.transform(data, facets); |
| 76 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), [2]); |
| 77 | +}); |
| 78 | + |
| 79 | +it("Plot.group’s median reducer preserves dates", () => { |
| 80 | + const group = Plot.groupZ({y: "median"}, {y: "y"}); |
| 81 | + const data = [{y: new Date("2021-01-01")}, {y: new Date("2024-01-01")}, {y: new Date("2022-01-01")}]; |
| 82 | + const facets = [d3.range(data.length)]; |
| 83 | + const {data: groupData} = group.transform(data, facets); |
| 84 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), [new Date("2022-01-01")]); |
| 85 | +}); |
| 86 | + |
| 87 | +it("Plot.group’s mean reducer preserves dates", () => { |
| 88 | + const group = Plot.groupZ({y: "mean"}, {y: "y"}); |
| 89 | + const data = [{y: new Date("2021-01-01")}, {y: new Date("2024-01-01")}, {y: new Date("2022-01-01")}]; |
| 90 | + const facets = [d3.range(data.length)]; |
| 91 | + const {data: groupData} = group.transform(data, facets); |
| 92 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), [new Date("2022-05-02T16:00:00Z")]); |
| 93 | +}); |
| 94 | + |
| 95 | +it("Plot.group’s min reducer preserves dates", () => { |
| 96 | + const group = Plot.groupZ({y: "min"}, {y: "y"}); |
| 97 | + const data = [{y: new Date("2021-01-01")}, {y: new Date("2024-01-01")}, {y: new Date("2022-01-01")}]; |
| 98 | + const facets = [d3.range(data.length)]; |
| 99 | + const {data: groupData} = group.transform(data, facets); |
| 100 | + assert.deepStrictEqual(Plot.valueof(groupData, group.y), [new Date("2021-01-01")]); |
| 101 | +}); |
0 commit comments