utils.spec.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. getQueryFromQueryString,
  3. getQueryStringFromQuery,
  4. queryHasChanged,
  5. getOrderbyFields,
  6. parseSavedQuery,
  7. generateQueryName,
  8. } from 'app/views/discover/utils';
  9. import createQueryBuilder from 'app/views/discover/queryBuilder';
  10. import {COLUMNS} from 'app/views/discover/data';
  11. const queryString =
  12. '?aggregations=%5B%5B%22count()%22%2Cnull%2C%22count%22%5D%2C%5B%22uniq%22%2C%22os_build%22%2C%22uniq_os_build%22%5D%5D&conditions=%5B%5D&end=%222018-07-10T01%3A18%3A04%22&fields=%5B%22id%22%2C%22timestamp%22%5D&limit=1000&orderby=%22-timestamp%22&projects=%5B8%5D&start=%222018-06-26T01%3A18%3A04%22';
  13. const queryStringWithInvalidKey =
  14. '?aggregations=%5B%5B%22count()%22%2Cnull%2C%22count%22%5D%2C%5B%22uniq%22%2C%22os_build%22%2C%22uniq_os_build%22%5D%5D&conditions=%5B%5D&end=%222018-07-10T01%3A18%3A04%22&fields=%5B%22id%22%2C%22timestamp%22%5D&limit=1000&orderby=%22-timestamp%22&projects=%5B8%5D&start=%222018-06-26T01%3A18%3A04%22&invalid=true';
  15. const query = {
  16. aggregations: [['count()', null, 'count'], ['uniq', 'os_build', 'uniq_os_build']],
  17. conditions: [],
  18. end: '2018-07-10T01:18:04',
  19. fields: ['id', 'timestamp'],
  20. limit: 1000,
  21. orderby: '-timestamp',
  22. projects: [8],
  23. start: '2018-06-26T01:18:04',
  24. };
  25. describe('getQueryFromQueryString()', function() {
  26. it('returns empty object if empty query string', function() {
  27. expect(getQueryFromQueryString('')).toEqual({});
  28. });
  29. it('handles aggregations', function() {
  30. expect(getQueryFromQueryString(queryString)).toEqual(query);
  31. });
  32. it('strips invalid keys', function() {
  33. expect(getQueryFromQueryString(queryStringWithInvalidKey)).toEqual(query);
  34. });
  35. });
  36. describe('getQueryStringFromQuery()', function() {
  37. it('parses query from query string', function() {
  38. expect(getQueryStringFromQuery(query)).toEqual(queryString);
  39. });
  40. it('keeps location in query string if provided', function() {
  41. expect(getQueryStringFromQuery(query, {visualization: 'table'})).toEqual(
  42. `${queryString}&visualization=table`
  43. );
  44. });
  45. });
  46. describe('queryHasChanged()', function() {
  47. it('checks only query fields', function() {
  48. const prev = '?fields=%5B"id"%5D&editing=true';
  49. const next = '?fields=%5B"id"%5D';
  50. expect(queryHasChanged(prev, next)).toBe(false);
  51. });
  52. });
  53. describe('getOrderbyFields()', function() {
  54. const organization = TestStubs.Organization({projects: [TestStubs.Project()]});
  55. const queryBuilder = createQueryBuilder({}, organization);
  56. it('allows ordering by all fields when no aggregations except project.name', function() {
  57. expect(getOrderbyFields(queryBuilder)).toHaveLength(COLUMNS.length - 1);
  58. });
  59. it('allows ordering by aggregations with aggregations and no fields', function() {
  60. queryBuilder.updateField('fields', []);
  61. queryBuilder.updateField('aggregations', [['count()', null, 'count']]);
  62. const options = getOrderbyFields(queryBuilder);
  63. expect(options).toHaveLength(1);
  64. expect(options).toEqual([{label: 'count', value: 'count'}]);
  65. });
  66. it('allows ordering by aggregations and fields', function() {
  67. queryBuilder.updateField('fields', ['message']);
  68. queryBuilder.updateField('aggregations', [['count()', null, 'count']]);
  69. const options = getOrderbyFields(queryBuilder);
  70. expect(options).toHaveLength(2);
  71. expect(options).toEqual([
  72. {label: 'message', value: 'message'},
  73. {label: 'count', value: 'count'},
  74. ]);
  75. });
  76. });
  77. describe('parseSavedQuery()', function() {
  78. it('strips metadata', function() {
  79. const queryFromApi = {
  80. id: '1',
  81. name: 'Test query',
  82. dateCreated: '2018-09-25T00:14:04.914Z',
  83. dateUpdated: '2018-09-25T00:14:04.914Z',
  84. fields: [],
  85. projects: [],
  86. conditions: [],
  87. limit: 10,
  88. };
  89. expect(parseSavedQuery(queryFromApi)).toEqual({
  90. fields: [],
  91. projects: [],
  92. conditions: [],
  93. limit: 10,
  94. });
  95. });
  96. });
  97. describe('generateQueryName()', function() {
  98. it('generates name', function() {
  99. expect(generateQueryName()).toBe('Result - Oct 17 02:41:20');
  100. });
  101. });