aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/__tests__/query.ts
blob: 5f4b58eb85539a4dbf6ac7e7f6a9975e705c1721 (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
import { parse, stringify, quote } from '../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'],
  });
});

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

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

  expect(parse('foo:\'bar "nested" quotes\'')).toEqual({
    foo: ['bar "nested" quotes'],
  });

  expect(parse("foo:'escaped\\' quotes'")).toEqual({
    foo: ["escaped' quotes"],
  });
});

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('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" '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'
  );
});