web/src/utils/do-searchy-searchy.test.ts
106 lines · 3075 bytes
1import { describe, it, expect } from 'vitest';
2import { prepareSearchItems } from './do-searchy-searchy';
3import type { SearchItem } from './do-searchy-searchy';
4import type { Category } from '../types/Service';
5
6const makeCategory = (overrides: Partial<Category> = {}): Category =>
7 ({
8 name: 'Test Category',
9 sections: [],
10 ...overrides,
11 }) as Category;
12
13describe('prepareSearchItems', () => {
14 it('returns an empty array for no categories', () => {
15 expect(prepareSearchItems([])).toEqual([]);
16 });
17
18 it('creates a category item', () => {
19 const categories = [makeCategory({ name: 'Privacy Tools' })];
20 const items = prepareSearchItems(categories);
21 expect(items).toHaveLength(1);
22 expect(items[0]).toMatchObject({
23 type: 'Category',
24 category: 'Privacy Tools',
25 itemCount: 0,
26 });
27 });
28
29 it('creates section items with category context', () => {
30 const categories = [
31 makeCategory({
32 name: 'Comms',
33 sections: [
34 { name: 'Messaging', intro: 'Secure messaging apps', services: [] },
35 ],
36 }),
37 ] as Category[];
38 const items = prepareSearchItems(categories);
39 const section = items.find((i: SearchItem) => i.type === 'Section');
40 expect(section).toMatchObject({
41 type: 'Section',
42 sectionName: 'Messaging',
43 description: 'Secure messaging apps',
44 category: 'Comms',
45 itemCount: 0,
46 });
47 });
48
49 it('creates service items with section and category context', () => {
50 const categories = [
51 makeCategory({
52 name: 'Comms',
53 sections: [
54 {
55 name: 'Messaging',
56 services: [
57 {
58 name: 'Signal',
59 description: 'Encrypted messenger',
60 url: 'https://signal.org',
61 github: 'signalapp/Signal-Android',
62 icon: 'signal.png',
63 },
64 ],
65 },
66 ],
67 }),
68 ] as Category[];
69 const items = prepareSearchItems(categories);
70 const service = items.find((i: SearchItem) => i.type === 'Service');
71 expect(service).toMatchObject({
72 type: 'Service',
73 name: 'Signal',
74 description: 'Encrypted messenger',
75 url: 'https://signal.org',
76 github: 'signalapp/Signal-Android',
77 category: 'Comms',
78 sectionName: 'Messaging',
79 logo: 'signal.png',
80 });
81 });
82
83 it('counts services across sections for category itemCount', () => {
84 const categories = [
85 makeCategory({
86 name: 'Tools',
87 sections: [
88 {
89 name: 'A',
90 services: [
91 { name: 's1', description: '', url: '' },
92 { name: 's2', description: '', url: '' },
93 ],
94 },
95 {
96 name: 'B',
97 services: [{ name: 's3', description: '', url: '' }],
98 },
99 ],
100 }),
101 ] as Category[];
102 const items = prepareSearchItems(categories);
103 const cat = items.find((i: SearchItem) => i.type === 'Category');
104 expect(cat?.itemCount).toBe(3);
105 });
106});