~james/core

core/web/src/lib/components/ui/rangeMode.test.ts -rw-r--r-- 2.6 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { describe, expect, it } from "vitest";
import { rangeFor, type RangeMode } from "#lib/components/ui/rangeMode.ts";

// 5 rows, the range sits on 1..3, and nothing shorter than 3 rows is legal
const at = (mode: RangeMode, row: number, moving: "start" | "end" = "start") =>
	rangeFor(mode, row, { cur: { start: 1, end: 3 }, minLength: 3, count: 5, moving });

describe("rangeFor", () => {
	it("resizes the moving end and keeps the other fixed", () => {
		expect(at("resize", 0)).toEqual({ start: 0, end: 3 });
		expect(at("resize", 1)).toEqual({ start: 1, end: 3 });
		expect(at("resize", 4, "end")).toEqual({ start: 1, end: 4 });
		// too close to the fixed end
		expect(at("resize", 2)).toBeNull();
	});

	it("lets the moving end cross the other in resize", () => {
		const crossed = rangeFor("resize", 4, {
			cur: { start: 1, end: 3 },
			minLength: 1,
			count: 5,
			moving: "start"
		});
		expect(crossed).toEqual({ start: 3, end: 4 });
	});

	it("keeps resize-top and resize-bottom one-sided", () => {
		expect(at("resize-top", 0)).toEqual({ start: 0, end: 3 });
		expect(at("resize-top", 1)).toEqual({ start: 1, end: 3 });
		expect(at("resize-top", 2)).toBeNull();

		expect(at("resize-bottom", 4)).toEqual({ start: 1, end: 4 });
		expect(at("resize-bottom", 3)).toEqual({ start: 1, end: 3 });
		expect(at("resize-bottom", 2)).toBeNull();
	});

	it("pushes the far end only as far as minLength needs", () => {
		expect(at("resize-top-push", 0)).toEqual({ start: 0, end: 3 });
		expect(at("resize-top-push", 2)).toEqual({ start: 2, end: 4 });
		// pushing the bottom to row 5 would run off the list
		expect(at("resize-top-push", 3)).toBeNull();

		expect(at("resize-bottom-push", 4)).toEqual({ start: 1, end: 4 });
		expect(at("resize-bottom-push", 2)).toEqual({ start: 0, end: 2 });
		expect(at("resize-bottom-push", 1)).toBeNull();
	});

	it("selects a minLength span anchored on the pointed row", () => {
		expect(at("select-top", 0)).toEqual({ start: 0, end: 2 });
		expect(at("select-top", 2)).toEqual({ start: 2, end: 4 });
		expect(at("select-top", 3)).toBeNull();
		expect(at("select-top", 4)).toBeNull();

		expect(at("select-bottom", 4)).toEqual({ start: 2, end: 4 });
		expect(at("select-bottom", 2)).toEqual({ start: 0, end: 2 });
		expect(at("select-bottom", 1)).toBeNull();
	});

	it("selects the single pointed row at the default minLength", () => {
		const one = { cur: { start: 1, end: 3 }, minLength: 1, count: 5, moving: "start" } as const;
		expect(rangeFor("select-top", 4, one)).toEqual({ start: 4, end: 4 });
		expect(rangeFor("select-bottom", 0, one)).toEqual({ start: 0, end: 0 });
	});
});