Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / lib / search / match-items.spec.ts
blob6159da813e28125609a9f1bddb025408e956b3ee
1 import type { ItemRevision } from '@proton/pass/types';
2 import { CardType } from '@proton/pass/types/protobuf/item-v1';
3 import { obfuscate } from '@proton/pass/utils/obfuscate/xor';
5 import { searchItems } from './match-items';
7 const searchAndExpect = (items: ItemRevision[], expected: ItemRevision[]) => (search: string) => {
8     const result = searchItems(items, search);
9     expect(result).toEqual(expected);
12 describe('searchItems', () => {
13     const items = [
14         {
15             data: {
16                 type: 'login',
18                 metadata: {
19                     name: 'Login item',
20                     note: obfuscate('This is item 1'),
21                     itemUuid: '1',
22                 },
23                 content: {
24                     itemEmail: obfuscate('user1@example.com'),
25                     itemUsername: obfuscate('user1'),
26                     urls: ['https://example.com'],
27                     password: obfuscate(''),
28                     totpUri: obfuscate('otpauth://totp/label?secret=secret&issuer=issuer'),
29                 },
30                 extraFields: [
31                     {
32                         fieldName: 'hidden label',
33                         type: 'hidden',
34                         data: {
35                             content: obfuscate('hidden value'),
36                         },
37                     },
38                     {
39                         fieldName: 'text label',
40                         type: 'text',
41                         data: {
42                             content: obfuscate('text value'),
43                         },
44                     },
45                     {
46                         fieldName: 'totp label',
47                         type: 'totp',
48                         data: {
49                             totpUri: obfuscate('otpauth://totp/label?secret=extrafieldsecret&issuer=issuer'),
50                         },
51                     },
52                 ],
53             },
54         },
55         {
56             data: {
57                 type: 'note',
58                 metadata: {
59                     name: 'Item 3',
60                     note: obfuscate('This is item 2'),
61                     itemUuid: '2',
62                 },
63                 content: {},
64                 extraFields: [],
65             },
66         },
67         {
68             data: {
69                 type: 'creditCard',
70                 metadata: {
71                     name: 'Credit card item',
72                     note: obfuscate('This is item 3'),
73                     itemUuid: '3',
74                 },
75                 content: {
76                     cardholderName: 'John Doe',
77                     number: obfuscate('1234567890'),
78                     verificationNumber: obfuscate(''),
79                     pin: obfuscate(''),
80                     cardType: CardType.Unspecified,
81                     expirationDate: '',
82                 },
83                 extraFields: [],
84             },
85         },
86         {
87             data: {
88                 type: 'alias',
89                 metadata: {
90                     name: 'Alias item',
91                     note: obfuscate('This is item 4'),
92                     itemUuid: '4',
93                 },
94                 content: {},
95                 extraFields: [],
96             },
97         },
98         {
99             data: {
100                 type: 'identity',
101                 metadata: {
102                     name: 'Identity item',
103                     note: obfuscate('This is item 5'),
104                     itemUuid: '4',
105                 },
106                 content: {
107                     fullName: '::full-name::',
108                     birthdate: '::birthdate::',
109                     extraPersonalDetails: [
110                         { fieldName: '::field::', type: 'text', data: { content: '::personal-detail-1::' } },
111                         { fieldName: '::field::', type: 'hidden', data: { content: '::personal-detail-2::' } },
112                     ],
113                     streetAddress: '::street-address::',
114                     city: '::city::',
115                     workEmail: '::work-email::',
116                     extraWorkDetails: [
117                         { fieldName: '::field::', type: 'hidden', data: { content: '::work-detail-1::' } },
118                         { fieldName: '::field::', type: 'text', data: { content: '::work-detail-2::' } },
119                     ],
120                     extraSections: [
121                         {
122                             sectionName: '::section-1::',
123                             sectionFields: [
124                                 { fieldName: '::field::', type: 'text', data: { content: '::first-section-1::' } },
125                                 { fieldName: '::field::', type: 'hidden', data: { content: '::first-section-2::' } },
126                             ],
127                         },
128                         {
129                             sectionName: '::section-2::',
130                             sectionFields: [
131                                 { fieldName: '::field::', type: 'hidden', data: { content: '::second-section-1::' } },
132                                 { fieldName: '::field::', type: 'text', data: { content: '::second-section-2::' } },
133                             ],
134                         },
135                     ],
136                 },
137                 extraFields: [],
138             },
139         },
140     ] as ItemRevision[];
142     it.each([
143         { key: 'no search', search: [''], expected: items },
144         { key: 'note', search: ['this is item'], expected: [items[0], items[1], items[2], items[3], items[4]] },
145         {
146             key: 'login item',
147             search: ['Login item', 'user1@example.com', 'user1', 'example.com', 'text label', 'text value'],
148             expected: [items[0]],
149         },
150         { key: 'card item', search: ['John Doe', '1234567890'], expected: [items[2]] },
151         {
152             key: 'identity item',
153             search: [
154                 'full-name',
155                 'birthdate',
156                 'street-address',
157                 'city',
158                 'work-email',
159                 '::personal-detail-2::',
160                 '::work-detail-1::',
161                 'detail-2',
162                 'first-section-1::',
163                 'second-section',
164             ],
165             expected: [items[4]],
166         },
167     ])('should return matching items based on $key', ({ search, expected }) => {
168         search.forEach(searchAndExpect(items, expected));
169     });
171     it.each([
172         { key: 'query', search: 'db' },
173         { key: 'otp fields', search: 'otpauth://totp/label?secret=secret&issuer=issuer' },
174         { key: 'otp fields with extra fields', search: 'otpauth://totp/label?secret=extrafieldsecret&issuer=issuer' },
175     ])('should return empty array when no match $key', ({ search }) => {
176         searchAndExpect(items, [])(search);
177     });