summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/tests
diff options
context:
space:
mode:
authorWolfgang Müller2024-03-05 18:08:09 +0100
committerWolfgang Müller2024-03-05 19:25:59 +0100
commitd1d654ebac2d51e3841675faeb56480e440f622f (patch)
tree56ef123c1a15a10dfd90836e4038e27efde950c6 /frontend/tests
downloadhircine-d1d654ebac2d51e3841675faeb56480e440f622f.tar.gz
Initial commit0.1.0
Diffstat (limited to '')
-rw-r--r--frontend/tests/Reader.test.ts45
-rw-r--r--frontend/tests/Selection.test.ts183
2 files changed, 228 insertions, 0 deletions
diff --git a/frontend/tests/Reader.test.ts b/frontend/tests/Reader.test.ts
new file mode 100644
index 0000000..e12d69b
--- /dev/null
+++ b/frontend/tests/Reader.test.ts
@@ -0,0 +1,45 @@
+import { Layout, type PageFragment } from '$gql/graphql';
+import { partition, type Chunk } from '$lib/Reader';
+import { expect, test } from 'vitest';
+
+const normalAttrs = { aspectRatio: 0.7, width: 140, height: 200 };
+const wideAttrs = { aspectRatio: 1.4, width: 280, height: 200 };
+
+const pages: PageFragment[] = [
+ { id: 0, path: '000.png', image: { id: 0, hash: '0', ...normalAttrs } },
+ { id: 1, path: '001.png', image: { id: 1, hash: '1', ...normalAttrs } },
+ { id: 2, path: '002.png', image: { id: 2, hash: '2', ...normalAttrs } },
+ { id: 3, path: '003.png', image: { id: 3, hash: '3', ...normalAttrs } },
+ { id: 4, path: '004.png', image: { id: 4, hash: '4', ...wideAttrs } },
+ { id: 5, path: '005.png', image: { id: 5, hash: '5', ...normalAttrs } },
+ { id: 6, path: '006.png', image: { id: 6, hash: '6', ...normalAttrs } }
+];
+
+const ids = (chunks: Chunk[]) =>
+ chunks.map((c) => (c.secondary ? [c.main.id, c.secondary.id] : [c.main.id]));
+
+const indices = (chunks: Chunk[]) => chunks.map((c) => c.index);
+
+test('partitions single layout', () => {
+ const [chunks, lookup] = partition(pages, Layout.Single);
+
+ expect(ids(chunks)).toStrictEqual([[0], [1], [2], [3], [4], [5], [6]]);
+ expect(indices(chunks)).toStrictEqual([0, 1, 2, 3, 4, 5, 6]);
+ expect(lookup).toStrictEqual([0, 1, 2, 3, 4, 5, 6]);
+});
+
+test('partitions double layout', () => {
+ const [chunks, lookup] = partition(pages, Layout.Double);
+
+ expect(ids(chunks)).toStrictEqual([[0, 1], [2, 3], [4], [5, 6]]);
+ expect(indices(chunks)).toStrictEqual([0, 2, 4, 5]);
+ expect(lookup).toStrictEqual([0, 0, 1, 1, 2, 3, 3]);
+});
+
+test('partitions double (offset) layout', () => {
+ const [chunks, lookup] = partition(pages, Layout.DoubleOffset);
+
+ expect(ids(chunks)).toStrictEqual([[0], [1, 2], [3], [4], [5, 6]]);
+ expect(indices(chunks)).toStrictEqual([0, 1, 3, 4, 5]);
+ expect(lookup).toStrictEqual([0, 1, 1, 2, 3, 4, 4]);
+});
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);
+});