1import { parse, stringify, quote } from 'src/pages/list/Filter';
2
3it('parses a simple query', () => {
4 expect(parse('foo:bar')).toEqual({
5 foo: ['bar'],
6 });
7});
8
9it('parses a query with multiple filters', () => {
10 expect(parse(`foo:bar baz:foo-bar`)).toEqual({
11 foo: ['bar'],
12 baz: ['foo-bar'],
13 });
14
15 expect(parse(`label:abc freetext`)).toEqual({
16 label: [`abc`],
17 freetext: [''],
18 });
19
20 expect(parse(`label:abc with "quotes" in freetext`)).toEqual({
21 label: [`abc`],
22 with: [''],
23 quotes: [''],
24 in: [''],
25 freetext: [''],
26 });
27});
28
29it('parses a quoted query', () => {
30 expect(parse(`foo:"bar"`)).toEqual({
31 foo: [`"bar"`],
32 });
33
34 expect(parse(`foo:'bar'`)).toEqual({
35 foo: [`'bar'`],
36 });
37
38 expect(parse(`label:'multi word label'`)).toEqual({
39 label: [`'multi word label'`],
40 });
41
42 expect(parse(`label:"multi word label"`)).toEqual({
43 label: [`"multi word label"`],
44 });
45
46 expect(parse(`label:'multi word label with "nested" quotes'`)).toEqual({
47 label: [`'multi word label with "nested" quotes'`],
48 });
49
50 expect(parse(`label:"multi word label with 'nested' quotes"`)).toEqual({
51 label: [`"multi word label with 'nested' quotes"`],
52 });
53
54 expect(parse(`label:"with:quoated:colon"`)).toEqual({
55 label: [`"with:quoated:colon"`],
56 });
57});
58
59it('should not escape nested quotes', () => {
60 expect(parse(`foo:'do not escape this ->'<- quote'`)).toEqual({
61 foo: [`'do not escape this ->'<- quote'`],
62 });
63
64 expect(parse(`foo:'do not escape this ->"<- quote'`)).toEqual({
65 foo: [`'do not escape this ->"<- quote'`],
66 });
67
68 expect(parse(`foo:"do not escape this ->"<- quote"`)).toEqual({
69 foo: [`"do not escape this ->"<- quote"`],
70 });
71
72 expect(parse(`foo:"do not escape this ->'<- quote"`)).toEqual({
73 foo: [`"do not escape this ->'<- quote"`],
74 });
75});
76
77it('parses a query with repetitions', () => {
78 expect(parse(`foo:bar foo:baz`)).toEqual({
79 foo: ['bar', 'baz'],
80 });
81});
82
83it('parses a complex query', () => {
84 expect(parse(`foo:bar foo:baz baz:"foobar" idont:'know'`)).toEqual({
85 foo: ['bar', 'baz'],
86 baz: [`"foobar"`],
87 idont: [`'know'`],
88 });
89});
90
91it('parses a key:value:value query', () => {
92 expect(parse(`meta:github:"https://github.com/MichaelMure/git-bug"`)).toEqual(
93 {
94 meta: [`github:"https://github.com/MichaelMure/git-bug"`],
95 }
96 );
97});
98
99it('quotes values', () => {
100 expect(quote(`foo`)).toEqual(`foo`);
101 expect(quote(`foo bar`)).toEqual(`"foo bar"`);
102 expect(quote(`foo "bar"`)).toEqual(`"foo "bar""`);
103 expect(quote(`foo 'bar'`)).toEqual(`"foo "bar""`);
104 expect(quote(`'foo'`)).toEqual(`"foo"`);
105 expect(quote(`foo "bar" 'baz'`)).toEqual(`"foo "bar" "baz""`);
106});
107
108it('stringifies params', () => {
109 expect(stringify({ foo: ['bar'] })).toEqual('foo:bar');
110 expect(stringify({ foo: ['bar baz'] })).toEqual('foo:"bar baz"');
111 expect(stringify({ foo: ['bar', 'baz'] })).toEqual('foo:bar foo:baz');
112 expect(stringify({ foo: ['bar'], baz: ['foobar'] })).toEqual(
113 'foo:bar baz:foobar'
114 );
115});