index.spec.jsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import React from 'react';
  2. import {mount, shallow} from 'enzyme';
  3. import Result from 'app/views/discover/result';
  4. import createQueryBuilder from 'app/views/discover/queryBuilder';
  5. describe('Result', function() {
  6. describe('New query', function() {
  7. let wrapper, data, organization;
  8. beforeEach(function() {
  9. organization = TestStubs.Organization();
  10. data = {
  11. baseQuery: {
  12. data: {data: [], meta: [], timing: {duration_ms: 15}},
  13. query: {
  14. aggregations: [['count()', null, 'count']],
  15. conditions: [],
  16. fields: [],
  17. },
  18. },
  19. byDayQuery: {
  20. query: null,
  21. data: null,
  22. },
  23. };
  24. wrapper = shallow(
  25. <Result
  26. data={data}
  27. organization={organization}
  28. onFetchPage={jest.fn()}
  29. location={{
  30. query: {},
  31. search: '',
  32. }}
  33. />,
  34. {
  35. context: {organization},
  36. disableLifecycleMethods: false,
  37. }
  38. );
  39. });
  40. afterEach(function() {
  41. MockApiClient.clearMockResponses();
  42. });
  43. describe('Render Summary', function() {
  44. it('shows correct range for pagination in summary', async function() {
  45. data = {
  46. data: {
  47. baseQuery: {
  48. query: {
  49. aggregations: [],
  50. conditions: [],
  51. fields: ['foo'],
  52. projects: [1],
  53. },
  54. data: {
  55. data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
  56. meta: [],
  57. timing: {duration_ms: 15},
  58. },
  59. previous: null,
  60. current: '0:0:1',
  61. next: '0:10:0',
  62. },
  63. byDayQuery: {
  64. query: null,
  65. data: null,
  66. },
  67. },
  68. };
  69. wrapper.setProps(data);
  70. expect(
  71. wrapper
  72. .find('ResultSummary')
  73. .render()
  74. .text()
  75. ).toEqual('query time: 15 ms, rows 1 - 10');
  76. });
  77. it('shows correct number of results shown when going to next page (next page function mocked on click)', async function() {
  78. data = {
  79. data: {
  80. baseQuery: {
  81. query: {
  82. aggregations: [],
  83. conditions: [],
  84. fields: ['foo'],
  85. projects: [1],
  86. },
  87. data: {
  88. data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
  89. meta: [],
  90. timing: {duration_ms: 15},
  91. },
  92. previous: '0:0:1',
  93. current: '0:10:0',
  94. next: '0:20:0',
  95. },
  96. byDayQuery: {
  97. query: null,
  98. data: null,
  99. },
  100. },
  101. };
  102. wrapper.setProps(data);
  103. expect(
  104. wrapper
  105. .find('ResultSummary')
  106. .render()
  107. .text()
  108. ).toBe('query time: 15 ms, rows 11 - 20');
  109. });
  110. it('shows 0 Results with no data', async function() {
  111. wrapper.setProps({
  112. data: {
  113. baseQuery: {
  114. query: {
  115. aggregations: [],
  116. conditions: [],
  117. fields: ['foo'],
  118. projects: [1],
  119. },
  120. data: {data: [], meta: [], timing: {duration_ms: 15}},
  121. previous: null,
  122. current: '0:10:0',
  123. next: null,
  124. },
  125. byDayQuery: {
  126. query: null,
  127. data: null,
  128. },
  129. },
  130. });
  131. expect(
  132. wrapper
  133. .find('ResultSummary')
  134. .render()
  135. .text()
  136. ).toBe('query time: 15 ms, 0 rows');
  137. });
  138. });
  139. describe('Toggles Visualizations', function() {
  140. beforeEach(function() {
  141. wrapper = mount(
  142. <Result
  143. data={data}
  144. organization={organization}
  145. onFetchPage={jest.fn()}
  146. location={{query: {}, search: ''}}
  147. />,
  148. TestStubs.routerContext([{organization}])
  149. );
  150. });
  151. it('displays options', function() {
  152. const buttons = wrapper.find('ResultViewButtons').find('a');
  153. expect(buttons).toHaveLength(3);
  154. });
  155. it('toggles buttons', function() {
  156. expect(wrapper.find('ResultTable')).toHaveLength(1);
  157. expect(wrapper.find('LineChart')).toHaveLength(0);
  158. wrapper
  159. .find('ResultViewButtons')
  160. .find('a')
  161. .at(1)
  162. .simulate('click');
  163. wrapper.update();
  164. expect(wrapper.find('ResultTable')).toHaveLength(0);
  165. expect(wrapper.find('LineChart')).toHaveLength(1);
  166. });
  167. it('toggles dropdown', function() {
  168. expect(wrapper.find('ResultTable')).toHaveLength(1);
  169. expect(wrapper.find('LineChart')).toHaveLength(0);
  170. wrapper
  171. .find('ul.dropdown-menu')
  172. .find('a')
  173. .at(1)
  174. .simulate('click');
  175. expect(wrapper.find('ResultTable')).toHaveLength(0);
  176. expect(wrapper.find('LineChart')).toHaveLength(1);
  177. });
  178. });
  179. });
  180. describe('Saved query', function() {
  181. let wrapper, queryBuilder;
  182. beforeEach(function() {
  183. const organization = TestStubs.Organization();
  184. queryBuilder = createQueryBuilder({}, organization);
  185. queryBuilder.updateField('aggregations', [['count()', null, 'count']]);
  186. const data = {
  187. baseQuery: {
  188. query: queryBuilder.getInternal(),
  189. data: {data: [], meta: [], timing: {duration_ms: 15}},
  190. },
  191. byDayQuery: {
  192. query: null,
  193. data: null,
  194. },
  195. };
  196. wrapper = mount(
  197. <Result
  198. data={data}
  199. organization={organization}
  200. savedQuery={TestStubs.DiscoverSavedQuery()}
  201. onFetchPage={jest.fn()}
  202. location={{query: {}}}
  203. />,
  204. TestStubs.routerContext()
  205. );
  206. });
  207. it('renders query name', function() {
  208. expect(wrapper.find('PageHeading').text()).toBe('Saved query #1');
  209. });
  210. });
  211. });