Skip to content

Commit 849cc31

Browse files
author
Nils Stolpe
committed
added tests
added code to handle 'extra' data keys passed in categories
1 parent 7623959 commit 849cc31

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/victory-pie/src/helper-methods.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ const sortDataByCategories = (props, data) => {
6666
const sorted: string[] = [];
6767
props.categories.x.forEach((category) => {
6868
const idx = data.findIndex(({ x }) => x === category);
69-
const datum = data.splice(idx, 1)[0];
70-
sorted.push(datum);
69+
if (idx >= 0) {
70+
const datum = data.splice(idx, 1)[0];
71+
sorted.push(datum);
72+
}
7173
});
7274
return [...sorted, ...data];
7375
}

packages/victory-pie/src/victory-pie.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,30 @@ describe("components/victory-pie", () => {
151151
expect(xValues).toEqual(xValuesFromGivenData);
152152
});
153153

154+
it("renders data values sorted by categories prop", () => {
155+
const { container } = render(
156+
<VictoryPie categories={{ x: ["E", "A", "D", "C", "B"] }} />,
157+
);
158+
159+
const xValues = Array.from(
160+
container.querySelectorAll("text[id^=pie-labels] > tspan"),
161+
).map((slice) => slice.textContent);
162+
163+
expect(xValues).toEqual(["E", "A", "D", "C", "B"]);
164+
});
165+
166+
it("renders data values sorted by categories prop, appending any data keys missing from categories and ignoring any categories values that are not valid data keys", () => {
167+
const { container } = render(
168+
<VictoryPie categories={{ x: ["E", "C", "B", "Z"] }} />,
169+
);
170+
171+
const xValues = Array.from(
172+
container.querySelectorAll("text[id^=pie-labels] > tspan"),
173+
).map((slice) => slice.textContent);
174+
175+
expect(xValues).toEqual(["E", "C", "B", "A", "D"]);
176+
});
177+
154178
it("renders data values sorted by sortKey prop", () => {
155179
const data = Helpers.range(9)
156180
.map((i) => ({ x: i, y: i }))

0 commit comments

Comments
 (0)