resultManager.spec.jsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import createResultManager from 'app/views/discover/resultManager';
  2. import createQueryBuilder from 'app/views/discover/queryBuilder';
  3. describe('Result manager', function() {
  4. let resultManager, queryBuilder, discoverMock, discoverByDayMock;
  5. beforeEach(function() {
  6. queryBuilder = createQueryBuilder(
  7. {},
  8. TestStubs.Organization({projects: [TestStubs.Project()]})
  9. );
  10. resultManager = createResultManager(queryBuilder);
  11. discoverMock = MockApiClient.addMockResponse({
  12. url: '/organizations/org-slug/discover/query/?per_page=1000&cursor=0:0:1',
  13. method: 'POST',
  14. body: {
  15. data: [],
  16. },
  17. });
  18. discoverByDayMock = MockApiClient.addMockResponse({
  19. url: '/organizations/org-slug/discover/query/',
  20. method: 'POST',
  21. body: {
  22. data: [],
  23. },
  24. });
  25. });
  26. describe('getAll()', function() {
  27. it('returns defaults', function() {
  28. const data = resultManager.getAll();
  29. expect(data.baseQuery.query).toBeNull();
  30. expect(data.baseQuery.data).toBeNull();
  31. expect(data.byDayQuery.query).toBeNull();
  32. expect(data.byDayQuery.data).toBeNull();
  33. });
  34. });
  35. describe('fetchAll()', function() {
  36. it('handles raw data', async function() {
  37. queryBuilder.updateField('fields', ['id', 'project.id', 'message']);
  38. await resultManager.fetchAll();
  39. expect(discoverMock).toHaveBeenCalledTimes(1);
  40. expect(discoverMock).toHaveBeenCalledWith(
  41. '/organizations/org-slug/discover/query/?per_page=1000&cursor=0:0:1',
  42. expect.objectContaining({
  43. data: expect.objectContaining({
  44. fields: ['id', 'project.id', 'message'],
  45. }),
  46. })
  47. );
  48. });
  49. it('handles aggregations', async function() {
  50. queryBuilder.updateField('aggregations', [['count()', null, 'count']]);
  51. await resultManager.fetchAll();
  52. expect(discoverMock).toHaveBeenCalledTimes(1);
  53. expect(discoverByDayMock).toHaveBeenCalledTimes(1);
  54. expect(discoverMock).toHaveBeenCalledWith(
  55. '/organizations/org-slug/discover/query/?per_page=1000&cursor=0:0:1',
  56. expect.objectContaining({
  57. data: expect.objectContaining({
  58. aggregations: [['count()', null, 'count']],
  59. }),
  60. })
  61. );
  62. });
  63. });
  64. describe('reset()', function() {
  65. it('resets', async function() {
  66. await resultManager.fetchAll();
  67. expect(resultManager.getAll().baseQuery.data).not.toBeNull();
  68. resultManager.reset();
  69. expect(resultManager.getAll().baseQuery.data).toBeNull();
  70. });
  71. });
  72. describe('shouldDisplayResult()', function() {
  73. it('is initially false', function() {
  74. expect(resultManager.shouldDisplayResult()).toBe(false);
  75. });
  76. it('is true after data is fetched', async function() {
  77. await resultManager.fetchAll();
  78. expect(resultManager.shouldDisplayResult()).toBe(true);
  79. });
  80. });
  81. });