1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-20 02:38:53 +00:00

Clean reference to floor compare (#28269)

Fix floor compare
This commit is contained in:
Paul Bottein
2025-12-01 15:47:09 +01:00
committed by GitHub
parent de53ad8dce
commit c33cb7fff9
3 changed files with 6 additions and 143 deletions

View File

@@ -1,116 +0,0 @@
import { describe, expect, it } from "vitest";
import { floorCompare } from "../../src/data/floor_registry";
import type { FloorRegistryEntry } from "../../src/data/floor_registry";
describe("floorCompare", () => {
describe("floorCompare()", () => {
it("sorts by floor ID alphabetically", () => {
const floors = ["basement", "attic", "ground"];
expect(floors.sort(floorCompare())).toEqual([
"attic",
"basement",
"ground",
]);
});
it("handles numeric strings in natural order", () => {
const floors = ["floor10", "floor2", "floor1"];
expect(floors.sort(floorCompare())).toEqual([
"floor1",
"floor2",
"floor10",
]);
});
});
describe("floorCompare(entries)", () => {
it("sorts by level descending (highest to lowest), then by name", () => {
const entries = {
floor1: { name: "Ground Floor", level: 0 } as FloorRegistryEntry,
floor2: { name: "First Floor", level: 1 } as FloorRegistryEntry,
floor3: { name: "Basement", level: -1 } as FloorRegistryEntry,
};
const floors = ["floor1", "floor2", "floor3"];
expect(floors.sort(floorCompare(entries))).toEqual([
"floor2",
"floor1",
"floor3",
]);
});
it("treats null level as -9999, placing it at the end", () => {
const entries = {
floor1: { name: "Ground Floor", level: 0 } as FloorRegistryEntry,
floor2: { name: "First Floor", level: 1 } as FloorRegistryEntry,
floor3: { name: "Unassigned", level: null } as FloorRegistryEntry,
};
const floors = ["floor2", "floor3", "floor1"];
expect(floors.sort(floorCompare(entries))).toEqual([
"floor2",
"floor1",
"floor3",
]);
});
it("sorts by name when levels are equal", () => {
const entries = {
floor1: { name: "Suite B", level: 1 } as FloorRegistryEntry,
floor2: { name: "Suite A", level: 1 } as FloorRegistryEntry,
};
const floors = ["floor1", "floor2"];
expect(floors.sort(floorCompare(entries))).toEqual(["floor2", "floor1"]);
});
it("falls back to floor ID when entry not found", () => {
const entries = {
floor1: { name: "Ground Floor" } as FloorRegistryEntry,
};
const floors = ["xyz", "floor1", "abc"];
expect(floors.sort(floorCompare(entries))).toEqual([
"abc",
"floor1",
"xyz",
]);
});
});
describe("floorCompare(entries, order)", () => {
it("follows order array", () => {
const entries = {
basement: { name: "Basement" } as FloorRegistryEntry,
ground: { name: "Ground Floor" } as FloorRegistryEntry,
first: { name: "First Floor" } as FloorRegistryEntry,
};
const order = ["first", "ground", "basement"];
const floors = ["basement", "first", "ground"];
expect(floors.sort(floorCompare(entries, order))).toEqual([
"first",
"ground",
"basement",
]);
});
it("places items not in order array at the end, sorted by name", () => {
const entries = {
floor1: { name: "First Floor" } as FloorRegistryEntry,
floor2: { name: "Ground Floor" } as FloorRegistryEntry,
floor3: { name: "Basement" } as FloorRegistryEntry,
};
const order = ["floor1"];
const floors = ["floor3", "floor2", "floor1"];
expect(floors.sort(floorCompare(entries, order))).toEqual([
"floor1",
"floor3",
"floor2",
]);
});
});
});