query.ts

  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  expect(parse(`label:'name ends after this ->' quote'`)).toEqual({
 59    label: [`'name ends after this ->'`],
 60    "quote'": [``],
 61  });
 62
 63  expect(parse(`label:"name ends after this ->" quote"`)).toEqual({
 64    label: [`"name ends after this ->"`],
 65    'quote"': [``],
 66  });
 67
 68  expect(parse(`label:'this ->"<- quote belongs to label name'`)).toEqual({
 69    label: [`'this ->"<- quote belongs to label name'`],
 70  });
 71
 72  expect(parse(`label:"this ->'<- quote belongs to label name"`)).toEqual({
 73    label: [`"this ->'<- quote belongs to label name"`],
 74  });
 75
 76  expect(parse(`label:'names end with'whitespace not with quotes`)).toEqual({
 77    label: [`'names end with'whitespace`],
 78    not: [``],
 79    with: [``],
 80    quotes: [``],
 81  });
 82
 83  expect(parse(`label:"names end with"whitespace not with quotes`)).toEqual({
 84    label: [`"names end with"whitespace`],
 85    not: [``],
 86    with: [``],
 87    quotes: [``],
 88  });
 89});
 90
 91it('should not escape nested quotes', () => {
 92  expect(parse(`foo:'do not escape this ->'<- quote'`)).toEqual({
 93    foo: [`'do not escape this ->'<-`],
 94    "quote'": [``],
 95  });
 96
 97  expect(parse(`foo:'do not escape this ->"<- quote'`)).toEqual({
 98    foo: [`'do not escape this ->"<- quote'`],
 99  });
100
101  expect(parse(`foo:"do not escape this ->"<- quote"`)).toEqual({
102    foo: [`"do not escape this ->"<-`],
103    'quote"': [``],
104  });
105
106  expect(parse(`foo:"do not escape this ->'<- quote"`)).toEqual({
107    foo: [`"do not escape this ->'<- quote"`],
108  });
109});
110
111it('parses a query with repetitions', () => {
112  expect(parse(`foo:bar foo:baz`)).toEqual({
113    foo: ['bar', 'baz'],
114  });
115});
116
117it('parses a complex query', () => {
118  expect(parse(`foo:bar foo:baz baz:"foobar" idont:'know'`)).toEqual({
119    foo: ['bar', 'baz'],
120    baz: [`"foobar"`],
121    idont: [`'know'`],
122  });
123});
124
125it('parses a key:value:value query', () => {
126  expect(parse(`meta:github:"https://github.com/git-bug/git-bug"`)).toEqual({
127    meta: [`github:"https://github.com/git-bug/git-bug"`],
128  });
129});
130
131it('quotes values', () => {
132  expect(quote(`foo`)).toEqual(`foo`);
133  expect(quote(`foo bar`)).toEqual(`"foo bar"`);
134  expect(quote(`foo "bar"`)).toEqual(`"foo "bar""`);
135  expect(quote(`foo 'bar'`)).toEqual(`"foo "bar""`);
136  expect(quote(`'foo'`)).toEqual(`"foo"`);
137  expect(quote(`foo "bar" 'baz'`)).toEqual(`"foo "bar" "baz""`);
138});
139
140it('stringifies params', () => {
141  expect(stringify({ foo: ['bar'] })).toEqual('foo:bar');
142  expect(stringify({ foo: ['bar baz'] })).toEqual('foo:"bar baz"');
143  expect(stringify({ foo: ['bar', 'baz'] })).toEqual('foo:bar foo:baz');
144  expect(stringify({ foo: ['bar'], baz: ['foobar'] })).toEqual(
145    'foo:bar baz:foobar'
146  );
147});