Farmbot-Web-App/frontend/farm_designer/point_groups/__tests__/point_group_sort_test.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-02-20 19:38:50 -07:00
import { SORT_OPTIONS } from "../point_group_sort";
2019-10-08 12:56:03 -06:00
import { PointGroupSortType } from "farmbot/dist/resources/api_resources";
2020-02-07 16:05:16 -07:00
import { TaggedPoint } from "farmbot";
import { fakePlant } from "../../../__test_support__/fake_state/resources";
2019-10-08 12:56:03 -06:00
2020-02-07 16:05:16 -07:00
describe("sort()", () => {
2020-02-28 09:34:28 -07:00
const phony = (plantName: string, x: number, y: number): TaggedPoint => {
2020-02-07 16:05:16 -07:00
const plant = fakePlant();
2020-02-28 09:34:28 -07:00
plant.body.name = plantName;
2020-02-07 16:05:16 -07:00
plant.body.x = x;
plant.body.y = y;
return plant;
2019-10-08 12:56:03 -06:00
};
2020-02-07 16:05:16 -07:00
const fakePoints = [
2019-10-08 12:56:03 -06:00
phony("A", 0, 0),
phony("B", 1, 0),
phony("C", 1, 1),
phony("D", 0, 1)
];
const sort = (sortType: PointGroupSortType): string[] => {
2020-02-07 16:05:16 -07:00
const array = SORT_OPTIONS[sortType](fakePoints);
2020-01-03 13:04:45 -07:00
return array.map(x => x?.body?.name || "NA");
2019-10-08 12:56:03 -06:00
};
it("sorts randomly", () => {
const results = sort("random");
2020-02-07 16:05:16 -07:00
expect(results.length).toEqual(fakePoints.length);
2019-10-08 12:56:03 -06:00
});
it("sorts by xy_ascending", () => {
const results = sort("xy_ascending");
expect(results).toEqual(["A", "D", "B", "C"]);
});
it("sorts by xy_descending", () => {
const results = sort("xy_descending");
expect(results).toEqual(["C", "B", "D", "A"]);
});
it("sorts by yx_ascending", () => {
const results = sort("yx_ascending");
2019-10-08 13:06:17 -06:00
expect(results).toEqual(["A", "B", "D", "C"]);
2019-10-08 12:56:03 -06:00
});
it("sorts by yx_descending", () => {
const results = sort("yx_descending");
expect(results).toEqual(["C", "D", "B", "A"]);
});
});