summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/tests/Selection.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/tests/Selection.test.ts')
-rw-r--r--frontend/tests/Selection.test.ts183
1 files changed, 183 insertions, 0 deletions
diff --git a/frontend/tests/Selection.test.ts b/frontend/tests/Selection.test.ts
new file mode 100644
index 0000000..67e8c4c
--- /dev/null
+++ b/frontend/tests/Selection.test.ts
@@ -0,0 +1,183 @@
+import { ItemSelection } from '$lib/Selection';
+import { expect, test } from 'vitest';
+
+interface TestItem {
+ id: number;
+ selectable: boolean;
+}
+
+const items: TestItem[] = [
+ { id: 1, selectable: true },
+ { id: 2, selectable: true },
+ { id: 3, selectable: false },
+ { id: 4, selectable: true }
+];
+
+const all = items.map((i) => i.id);
+const selectable = items.filter((i) => i.selectable).map((i) => i.id);
+
+const setup = () => {
+ const selection = new ItemSelection<TestItem>();
+ selection.view = items;
+ return selection;
+};
+
+test('selects a single item', () => {
+ let selection = setup();
+
+ selection = selection.update(0, false);
+
+ expect(selection.ids).toStrictEqual([items[0].id]);
+});
+
+test('selects a single item (with empty shift select)', () => {
+ let selection = setup();
+
+ selection = selection.update(0, true);
+
+ expect(selection.ids).toStrictEqual([items[0].id]);
+});
+
+test('selects multiple items (forwards)', () => {
+ let selection = setup();
+
+ selection = selection.update(0, false);
+ selection = selection.update(2, true);
+
+ expect(selection.ids.toSorted((a, b) => a - b)).toStrictEqual(all.slice(0, 3));
+});
+
+test('selects multiple items (backwards)', () => {
+ let selection = setup();
+
+ selection = selection.update(2, false);
+ selection = selection.update(0, true);
+
+ expect(selection.ids.toSorted((a, b) => a - b)).toStrictEqual(all.slice(0, 3));
+});
+
+test('selects multiple items (only selectables)', () => {
+ let selection = setup();
+ selection.selectable = (i) => i.selectable;
+
+ selection = selection.update(0, false);
+ selection = selection.update(3, true);
+
+ expect(selection.ids).toStrictEqual(selectable);
+});
+
+test('selects all', () => {
+ const selection = setup().all();
+
+ expect(selection.ids).toStrictEqual(all);
+});
+
+test('selects all selectables', () => {
+ let selection = setup();
+ selection.selectable = (i) => i.selectable;
+
+ selection = selection.all();
+
+ expect(selection.ids).toStrictEqual(selectable);
+});
+
+test('deselects all', () => {
+ let selection = setup().all();
+
+ selection = selection.none();
+
+ expect(selection.ids).empty;
+});
+
+test('deselects a single item', () => {
+ let selection = setup().all();
+
+ selection = selection.update(0, false);
+
+ expect(selection.ids).toStrictEqual(all.slice(1));
+});
+
+test('deselects multiple items', () => {
+ let selection = setup();
+
+ selection = selection.update(0, false);
+ selection = selection.update(2, true);
+ selection = selection.update(2, true);
+
+ expect(selection.ids).empty;
+});
+
+test('retains selection', () => {
+ let selection = setup();
+
+ selection = selection.all();
+
+ selection.view = items.slice(0, 2);
+ expect(selection.ids).toStrictEqual(all.slice(0, 2));
+
+ selection.view = items;
+ expect(selection.ids).toStrictEqual(all);
+});
+
+test('is inactive by default', () => {
+ const selection = setup();
+ expect(selection.active).toBe(false);
+});
+
+test('is inactive after clearing', () => {
+ let selection = setup();
+
+ selection.active = true;
+
+ selection = selection.clear();
+ expect(selection.active).false;
+});
+
+test('can be toggled', () => {
+ let selection = setup();
+
+ selection = selection.toggle();
+ expect(selection.active).toBe(true);
+
+ selection = selection.all();
+ selection = selection.toggle();
+ expect(selection.active).toBe(false);
+ expect(selection.ids).empty;
+});
+
+test('can be cleared', () => {
+ let selection = setup();
+
+ selection = selection.all();
+
+ selection = selection.clear();
+ expect(selection.ids).empty;
+});
+
+test('reports selected items', () => {
+ let selection = setup();
+
+ selection = selection.update(0, false);
+ selection = selection.update(2, false);
+
+ expect(selection.contains(all[0])).toBeTruthy();
+ expect(selection.contains(all[1])).toBeFalsy();
+ expect(selection.contains(all[2])).toBeTruthy();
+ expect(selection.contains(all[3])).toBeFalsy();
+});
+
+test('reports size', () => {
+ const selection = setup().all();
+
+ expect(selection.size).toBe(all.length);
+});
+
+test('reports size of visible items', () => {
+ const selection = setup().all();
+
+ selection.view = items.slice(0, 2);
+ expect(selection.size).toBe(2);
+
+ selection.view = items;
+ expect(selection.size).toBe(all.length);
+});