diff options
Diffstat (limited to 'frontend/tests/Selection.test.ts')
-rw-r--r-- | frontend/tests/Selection.test.ts | 92 |
1 files changed, 50 insertions, 42 deletions
diff --git a/frontend/tests/Selection.test.ts b/frontend/tests/Selection.test.ts index c7847cd..1bdbb55 100644 --- a/frontend/tests/Selection.test.ts +++ b/frontend/tests/Selection.test.ts @@ -1,4 +1,4 @@ -import { ItemSelection } from '$lib/Selection'; +import { ItemSelection } from '$lib/selection/Selection.svelte'; import { expect, test } from 'vitest'; interface TestItem { @@ -17,100 +17,104 @@ 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>(); + const selection = new ItemSelection<TestItem>('test', (t) => t.id.toString()); selection.view = items; return selection; }; test('selects a single item', () => { - let selection = setup(); + const selection = setup(); - selection = selection.update(0, false); + selection.update(0, false); expect(selection.ids).toStrictEqual([items[0].id]); }); test('selects a single item (with empty shift select)', () => { - let selection = setup(); + const selection = setup(); - selection = selection.update(0, true); + selection.update(0, true); expect(selection.ids).toStrictEqual([items[0].id]); }); test('selects multiple items (forwards)', () => { - let selection = setup(); + const selection = setup(); - selection = selection.update(0, false); - selection = selection.update(2, true); + selection.update(0, false); + 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(); + const selection = setup(); - selection = selection.update(2, false); - selection = selection.update(0, true); + selection.update(2, false); + 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(); + const selection = setup(); selection.selectable = (i) => i.selectable; - selection = selection.update(0, false); - selection = selection.update(3, true); + selection.update(0, false); + selection.update(3, true); expect(selection.ids).toStrictEqual(selectable); }); test('selects all', () => { - const selection = setup().all(); + const selection = setup(); + + selection.all(); expect(selection.ids).toStrictEqual(all); }); test('selects all selectables', () => { - let selection = setup(); + const selection = setup(); selection.selectable = (i) => i.selectable; - selection = selection.all(); + selection.all(); expect(selection.ids).toStrictEqual(selectable); }); test('deselects all', () => { - let selection = setup().all(); + const selection = setup(); - selection = selection.none(); + selection.all(); + selection.none(); expect(selection.ids).toHaveLength(0); }); test('deselects a single item', () => { - let selection = setup().all(); + const selection = setup(); - selection = selection.update(0, false); + selection.all(); + selection.update(0, false); expect(selection.ids).toStrictEqual(all.slice(1)); }); test('deselects multiple items', () => { - let selection = setup(); + const selection = setup(); - selection = selection.update(0, false); - selection = selection.update(2, true); - selection = selection.update(2, true); + selection.update(0, false); + selection.update(2, true); + selection.update(2, true); expect(selection.ids).toHaveLength(0); }); test('retains selection', () => { - let selection = setup(); + const selection = setup(); - selection = selection.all(); + selection.all(); selection.view = items.slice(0, 2); expect(selection.ids).toStrictEqual(all.slice(0, 2)); @@ -125,40 +129,40 @@ test('is inactive by default', () => { }); test('is inactive after clearing', () => { - let selection = setup(); + const selection = setup(); selection.active = true; - selection = selection.clear(); + selection.clear(); expect(selection.active).toBeFalsy(); }); test('can be toggled', () => { - let selection = setup(); + const selection = setup(); - selection = selection.toggle(); + selection.toggle(); expect(selection.active).toBe(true); - selection = selection.all(); - selection = selection.toggle(); + selection.all(); + selection.toggle(); expect(selection.active).toBe(false); expect(selection.ids).toHaveLength(0); }); test('can be cleared', () => { - let selection = setup(); + const selection = setup(); - selection = selection.all(); + selection.all(); - selection = selection.clear(); + selection.clear(); expect(selection.ids).toHaveLength(0); }); test('reports selected items', () => { - let selection = setup(); + const selection = setup(); - selection = selection.update(0, false); - selection = selection.update(2, false); + selection.update(0, false); + selection.update(2, false); expect(selection.contains(all[0])).toBeTruthy(); expect(selection.contains(all[1])).toBeFalsy(); @@ -167,13 +171,17 @@ test('reports selected items', () => { }); test('reports size', () => { - const selection = setup().all(); + const selection = setup(); + + selection.all(); expect(selection.size).toBe(all.length); }); test('reports size of visible items', () => { - const selection = setup().all(); + const selection = setup(); + + selection.all(); selection.view = items.slice(0, 2); expect(selection.size).toBe(2); |