summaryrefslogblamecommitdiffstatshomepage
path: root/frontend/tests/Selection.test.ts
blob: 1bdbb5572de1d25859bb6faee29efd66c3a93099 (plain) (tree)
1
                                                                

















                                                                      
                                                                                      




                                     
                                  
 
                                   




                                                               
                                  
 
                                  




                                                           
                                  
 

                                   




                                                                                       
                                  
 

                                   




                                                                                       
                                  

                                                   

                                   




                                                        


                                  




                                                 
                                  

                                                   
                        




                                                        
                                  
 

                         
 
                                              


                                       
                                  
 

                                   




                                                          
                                  
 


                                   
 
                                              


                                 
                                  
 
                        













                                                             
                                  


                                
                          
                                             


                              
                                  
 
                           

                                            

                           
                                             
                                              


                              
                                  
 
                        
 
                          
                                              


                                      
                                  
 

                                   







                                                        


                                  




                                                


                                  






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