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
|
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);
});
|