14841: Don't defer initializing sessions
[arvados.git] / src / store / search-bar / search-bar-actions.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { getAdvancedDataFromQuery, getQueryFromAdvancedData, parseSearchQuery } from "~/store/search-bar/search-bar-actions";
6 import { ResourceKind } from "~/models/resource";
7
8 describe('search-bar-actions', () => {
9     describe('parseSearchQuery', () => {
10         it('should correctly parse query #1', () => {
11             const q = 'val0 is:trashed val1';
12             const r = parseSearchQuery(q);
13             expect(r.hasKeywords).toBeTruthy();
14             expect(r.values).toEqual(['val0', 'val1']);
15             expect(r.properties).toEqual({
16                 is: ['trashed']
17             });
18         });
19
20         it('should correctly parse query #2 (value with keyword should be ignored)', () => {
21             const q = 'val0 is:from:trashed val1';
22             const r = parseSearchQuery(q);
23             expect(r.hasKeywords).toBeTruthy();
24             expect(r.values).toEqual(['val0', 'val1']);
25             expect(r.properties).toEqual({
26                 from: ['trashed']
27             });
28         });
29
30         it('should correctly parse query #3 (many keywords)', () => {
31             const q = 'val0 is:trashed val2 from:2017-04-01 val1';
32             const r = parseSearchQuery(q);
33             expect(r.hasKeywords).toBeTruthy();
34             expect(r.values).toEqual(['val0', 'val2', 'val1']);
35             expect(r.properties).toEqual({
36                 is: ['trashed'],
37                 from: ['2017-04-01']
38             });
39         });
40
41         it('should correctly parse query #4 (no duplicated values)', () => {
42             const q = 'val0 is:trashed val2 val2 val0';
43             const r = parseSearchQuery(q);
44             expect(r.hasKeywords).toBeTruthy();
45             expect(r.values).toEqual(['val0', 'val2']);
46             expect(r.properties).toEqual({
47                 is: ['trashed']
48             });
49         });
50
51         it('should correctly parse query #5 (properties)', () => {
52             const q = 'val0 has:filesize:100mb val2 val2 val0';
53             const r = parseSearchQuery(q);
54             expect(r.hasKeywords).toBeTruthy();
55             expect(r.values).toEqual(['val0', 'val2']);
56             expect(r.properties).toEqual({
57                 'has': ['filesize:100mb']
58             });
59         });
60
61         it('should correctly parse query #6 (multiple properties, multiple is)', () => {
62             const q = 'val0 has:filesize:100mb val2 has:user:daniel is:starred val2 val0 is:trashed';
63             const r = parseSearchQuery(q);
64             expect(r.hasKeywords).toBeTruthy();
65             expect(r.values).toEqual(['val0', 'val2']);
66             expect(r.properties).toEqual({
67                 'has': ['filesize:100mb', 'user:daniel'],
68                 'is': ['starred', 'trashed']
69             });
70         });
71     });
72
73     describe('getAdvancedDataFromQuery', () => {
74         it('should correctly build advanced data record from query #1', () => {
75             const r = getAdvancedDataFromQuery('val0 has:filesize:100mb val2 has:user:daniel is:starred val2 val0 is:trashed');
76             expect(r).toEqual({
77                 searchValue: 'val0 val2',
78                 type: undefined,
79                 cluster: undefined,
80                 projectUuid: undefined,
81                 inTrash: true,
82                 dateFrom: undefined,
83                 dateTo: undefined,
84                 properties: [{
85                     key: 'filesize',
86                     value: '100mb'
87                 }, {
88                     key: 'user',
89                     value: 'daniel'
90                 }],
91                 saveQuery: false,
92                 queryName: ''
93             });
94         });
95
96         it('should correctly build advanced data record from query #2', () => {
97             const r = getAdvancedDataFromQuery('document from:2017-08-01 pdf has:filesize:101mb is:trashed type:arvados#collection cluster:c97qx');
98             expect(r).toEqual({
99                 searchValue: 'document pdf',
100                 type: ResourceKind.COLLECTION,
101                 cluster: 'c97qx',
102                 projectUuid: undefined,
103                 inTrash: true,
104                 dateFrom: '2017-08-01',
105                 dateTo: undefined,
106                 properties: [{
107                     key: 'filesize',
108                     value: '101mb'
109                 }],
110                 saveQuery: false,
111                 queryName: ''
112             });
113         });
114     });
115
116     describe('getQueryFromAdvancedData', () => {
117         it('should build query from advanced data', () => {
118             const q = getQueryFromAdvancedData({
119                 searchValue: 'document pdf',
120                 type: ResourceKind.COLLECTION,
121                 cluster: 'c97qx',
122                 projectUuid: undefined,
123                 inTrash: true,
124                 dateFrom: '2017-08-01',
125                 dateTo: '',
126                 properties: [{
127                     key: 'filesize',
128                     value: '101mb'
129                 }],
130                 saveQuery: false,
131                 queryName: ''
132             });
133             expect(q).toBe('document pdf type:arvados#collection cluster:c97qx is:trashed from:2017-08-01 has:filesize:101mb');
134         });
135     });
136 });