aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/__tests__/query.ts
blob: 97ec75a6c0da8c73e2f365f4fc4054cce279b166 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { parse, stringify, quote } from 'src/pages/list/Filter';

it('parses a simple query', () => {
  expect(parse('foo:bar')).toEqual({
    foo: ['bar'],
  });
});

it('parses a query with multiple filters', () => {
  expect(parse(`foo:bar baz:foo-bar`)).toEqual({
    foo: ['bar'],
    baz: ['foo-bar'],
  });

  expect(parse(`label:abc freetext`)).toEqual({
    label: [`abc`],
    freetext: [''],
  });

  expect(parse(`label:abc with "quotes" 'in' freetext`)).toEqual({
    label: [`abc`],
    with: [''],
    '"quotes"': [''],
    "'in'": [''],
    freetext: [''],
  });
});

it('parses a quoted query', () => {
  expect(parse(`foo:"bar"`)).toEqual({
    foo: [`"bar"`],
  });

  expect(parse(`foo:'bar'`)).toEqual({
    foo: [`'bar'`],
  });

  expect(parse(`label:'multi word label'`)).toEqual({
    label: [`'multi word label'`],
  });

  expect(parse(`label:"multi word label"`)).toEqual({
    label: [`"multi word label"`],
  });

  expect(parse(`label:'multi word label with "nested" quotes'`)).toEqual({
    label: [`'multi word label with "nested" quotes'`],
  });

  expect(parse(`label:"multi word label with 'nested' quotes"`)).toEqual({
    label: [`"multi word label with 'nested' quotes"`],
  });

  expect(parse(`label:"with:quoated:colon"`)).toEqual({
    label: [`"with:quoated:colon"`],
  });

  expect(parse(`label:'name ends after this ->' quote'`)).toEqual({
    label: [`'name ends after this ->'`],
    "quote'": [``],
  });

  expect(parse(`label:"name ends after this ->" quote"`)).toEqual({
    label: [`"name ends after this ->"`],
    'quote"': [``],
  });

  expect(parse(`label:'this ->"<- quote belongs to label name'`)).toEqual({
    label: [`'this ->"<- quote belongs to label name'`],
  });

  expect(parse(`label:"this ->'<- quote belongs to label name"`)).toEqual({
    label: [`"this ->'<- quote belongs to label name"`],
  });

  expect(parse(`label:'names end with'whitespace not with quotes`)).toEqual({
    label: [`'names end with'whitespace`],
    not: [``],
    with: [``],
    quotes: [``],
  });

  expect(parse(`label:"names end with"whitespace not with quotes`)).toEqual({
    label: [`"names end with"whitespace`],
    not: [``],
    with: [``],
    quotes: [``],
  });
});

it('should not escape nested quotes', () => {
  expect(parse(`foo:'do not escape this ->'<- quote'`)).toEqual({
    foo: [`'do not escape this ->'<-`],
    "quote'": [``],
  });

  expect(parse(`foo:'do not escape this ->"<- quote'`)).toEqual({
    foo: [`'do not escape this ->"<- quote'`],
  });

  expect(parse(`foo:"do not escape this ->"<- quote"`)).toEqual({
    foo: [`"do not escape this ->"<-`],
    'quote"': [``],
  });

  expect(parse(`foo:"do not escape this ->'<- quote"`)).toEqual({
    foo: [`"do not escape this ->'<- quote"`],
  });
});

it('parses a query with repetitions', () => {
  expect(parse(`foo:bar foo:baz`)).toEqual({
    foo: ['bar', 'baz'],
  });
});

it('parses a complex query', () => {
  expect(parse(`foo:bar foo:baz baz:"foobar" idont:'know'`)).toEqual({
    foo: ['bar', 'baz'],
    baz: [`"foobar"`],
    idont: [`'know'`],
  });
});

it('parses a key:value:value query', () => {
  expect(parse(`meta:github:"https://github.com/MichaelMure/git-bug"`)).toEqual(
    {
      meta: [`github:"https://github.com/MichaelMure/git-bug"`],
    }
  );
});

it('quotes values', () => {
  expect(quote(`foo`)).toEqual(`foo`);
  expect(quote(`foo bar`)).toEqual(`"foo bar"`);
  expect(quote(`foo "bar"`)).toEqual(`"foo "bar""`);
  expect(quote(`foo 'bar'`)).toEqual(`"foo "bar""`);
  expect(quote(`'foo'`)).toEqual(`"foo"`);
  expect(quote(`foo "bar" 'baz'`)).toEqual(`"foo "bar" "baz""`);
});

it('stringifies params', () => {
  expect(stringify({ foo: ['bar'] })).toEqual('foo:bar');
  expect(stringify({ foo: ['bar baz'] })).toEqual('foo:"bar baz"');
  expect(stringify({ foo: ['bar', 'baz'] })).toEqual('foo:bar foo:baz');
  expect(stringify({ foo: ['bar'], baz: ['foobar'] })).toEqual(
    'foo:bar baz:foobar'
  );
});