summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/tests/Selection.test.ts
blob: 1bdbb5572de1d25859bb6faee29efd66c3a93099 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { ItemSelection } from '$lib/selection/Selection.svelte';
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>('test', (t) => t.id.toString());
	selection.view = items;
	return selection;
};

test('selects a single item', () => {
	const selection = setup();

	selection.update(0, false);

	expect(selection.ids).toStrictEqual([items[0].id]);
});

test('selects a single item (with empty shift select)', () => {
	const selection = setup();

	selection.update(0, true);

	expect(selection.ids).toStrictEqual([items[0].id]);
});

test('selects multiple items (forwards)', () => {
	const selection = setup();

	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)', () => {
	const selection = setup();

	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)', () => {
	const selection = setup();
	selection.selectable = (i) => i.selectable;

	selection.update(0, false);
	selection.update(3, true);

	expect(selection.ids).toStrictEqual(selectable);
});

test('selects all', () => {
	const selection = setup();

	selection.all();

	expect(selection.ids).toStrictEqual(all);
});

test('selects all selectables', () => {
	const selection = setup();
	selection.selectable = (i) => i.selectable;

	selection.all();

	expect(selection.ids).toStrictEqual(selectable);
});

test('deselects all', () => {
	const selection = setup();

	selection.all();
	selection.none();

	expect(selection.ids).toHaveLength(0);
});

test('deselects a single item', () => {
	const selection = setup();

	selection.all();
	selection.update(0, false);

	expect(selection.ids).toStrictEqual(all.slice(1));
});

test('deselects multiple items', () => {
	const selection = setup();

	selection.update(0, false);
	selection.update(2, true);
	selection.update(2, true);

	expect(selection.ids).toHaveLength(0);
});

test('retains selection', () => {
	const selection = setup();

	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', () => {
	const selection = setup();

	selection.active = true;

	selection.clear();
	expect(selection.active).toBeFalsy();
});

test('can be toggled', () => {
	const selection = setup();

	selection.toggle();
	expect(selection.active).toBe(true);

	selection.all();
	selection.toggle();
	expect(selection.active).toBe(false);
	expect(selection.ids).toHaveLength(0);
});

test('can be cleared', () => {
	const selection = setup();

	selection.all();

	selection.clear();
	expect(selection.ids).toHaveLength(0);
});

test('reports selected items', () => {
	const selection = setup();

	selection.update(0, false);
	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();

	selection.all();

	expect(selection.size).toBe(all.length);
});

test('reports size of visible items', () => {
	const selection = setup();

	selection.all();

	selection.view = items.slice(0, 2);
	expect(selection.size).toBe(2);

	selection.view = items;
	expect(selection.size).toBe(all.length);
});