Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
added code to handle 'extra' data keys passed in categories
  • Loading branch information
nstolpe committed Oct 28, 2024
1 parent 7623959 commit 849cc31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/victory-pie/src/helper-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ const sortDataByCategories = (props, data) => {
const sorted: string[] = [];
props.categories.x.forEach((category) => {
const idx = data.findIndex(({ x }) => x === category);
const datum = data.splice(idx, 1)[0];
sorted.push(datum);
if (idx >= 0) {
const datum = data.splice(idx, 1)[0];
sorted.push(datum);
}
});
return [...sorted, ...data];
}
Expand Down
24 changes: 24 additions & 0 deletions packages/victory-pie/src/victory-pie.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ describe("components/victory-pie", () => {
expect(xValues).toEqual(xValuesFromGivenData);
});

it("renders data values sorted by categories prop", () => {
const { container } = render(
<VictoryPie categories={{ x: ["E", "A", "D", "C", "B"] }} />,
);

const xValues = Array.from(
container.querySelectorAll("text[id^=pie-labels] > tspan"),
).map((slice) => slice.textContent);

expect(xValues).toEqual(["E", "A", "D", "C", "B"]);
});

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", () => {
const { container } = render(
<VictoryPie categories={{ x: ["E", "C", "B", "Z"] }} />,
);

const xValues = Array.from(
container.querySelectorAll("text[id^=pie-labels] > tspan"),
).map((slice) => slice.textContent);

expect(xValues).toEqual(["E", "C", "B", "A", "D"]);
});

it("renders data values sorted by sortKey prop", () => {
const data = Helpers.range(9)
.map((i) => ({ x: i, y: i }))
Expand Down

0 comments on commit 849cc31

Please sign in to comment.