discoverQuery.spec.jsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import {Client} from 'sentry/api';
  3. import DiscoverQuery from 'sentry/utils/discover/discoverQuery';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. describe('DiscoverQuery', function () {
  6. let location, api, eventView;
  7. beforeEach(() => {
  8. api = new Client();
  9. location = {
  10. pathname: '/events',
  11. query: {},
  12. };
  13. eventView = EventView.fromSavedQuery({
  14. id: '',
  15. name: 'test query',
  16. version: 2,
  17. fields: ['transaction', 'count()'],
  18. projects: [1],
  19. environment: ['dev'],
  20. });
  21. });
  22. describe('with eventsv2', function () {
  23. it('fetches data on mount', async function () {
  24. const getMock = MockApiClient.addMockResponse({
  25. url: '/organizations/test-org/eventsv2/',
  26. body: {
  27. meta: {transaction: 'string', count: 'number'},
  28. data: [{transaction: '/health', count: 1000}],
  29. },
  30. });
  31. render(
  32. <DiscoverQuery
  33. orgSlug="test-org"
  34. api={api}
  35. location={location}
  36. eventView={eventView}
  37. >
  38. {({tableData, isLoading}) => {
  39. if (isLoading) {
  40. return 'loading';
  41. }
  42. return <p>{tableData.data[0].transaction}</p>;
  43. }}
  44. </DiscoverQuery>
  45. );
  46. await tick();
  47. // Children should be rendered, and API should be called.
  48. expect(getMock).toHaveBeenCalledTimes(1);
  49. expect(await screen.findByText('/health')).toBeInTheDocument();
  50. });
  51. it('applies limit and cursor props', async function () {
  52. const getMock = MockApiClient.addMockResponse({
  53. url: '/organizations/test-org/eventsv2/',
  54. body: {
  55. meta: {transaction: 'string', count: 'number'},
  56. data: [{transaction: '/health', count: 1000}],
  57. },
  58. });
  59. render(
  60. <DiscoverQuery
  61. orgSlug="test-org"
  62. api={api}
  63. location={location}
  64. eventView={eventView}
  65. limit={3}
  66. cursor="1:0:1"
  67. >
  68. {({tableData, isLoading}) => {
  69. if (isLoading) {
  70. return 'loading';
  71. }
  72. return <p>{tableData.data[0].transaction}</p>;
  73. }}
  74. </DiscoverQuery>
  75. );
  76. await tick();
  77. expect(getMock).toHaveBeenCalledTimes(1);
  78. expect(getMock).toHaveBeenCalledWith(
  79. '/organizations/test-org/eventsv2/',
  80. expect.objectContaining({
  81. method: 'GET',
  82. query: expect.objectContaining({
  83. per_page: 3,
  84. cursor: '1:0:1',
  85. }),
  86. })
  87. );
  88. });
  89. it('parses string errors correctly', async function () {
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/test-org/eventsv2/',
  92. body: {
  93. detail: 'Error Message',
  94. },
  95. statusCode: 400,
  96. });
  97. let errorValue;
  98. render(
  99. <DiscoverQuery
  100. orgSlug="test-org"
  101. api={api}
  102. location={location}
  103. eventView={eventView}
  104. setError={e => (errorValue = e)}
  105. >
  106. {({isLoading}) => {
  107. if (isLoading) {
  108. return 'loading';
  109. }
  110. return null;
  111. }}
  112. </DiscoverQuery>
  113. );
  114. await tick();
  115. expect(errorValue.message).toEqual('Error Message');
  116. });
  117. it('parses object errors correctly', async function () {
  118. MockApiClient.addMockResponse({
  119. url: '/organizations/test-org/eventsv2/',
  120. body: {
  121. detail: {
  122. code: '?',
  123. message: 'Object Error',
  124. extra: {},
  125. },
  126. },
  127. statusCode: 400,
  128. });
  129. let errorValue;
  130. render(
  131. <DiscoverQuery
  132. orgSlug="test-org"
  133. api={api}
  134. location={location}
  135. eventView={eventView}
  136. setError={e => (errorValue = e)}
  137. >
  138. {({isLoading}) => {
  139. if (isLoading) {
  140. return 'loading';
  141. }
  142. return null;
  143. }}
  144. </DiscoverQuery>
  145. );
  146. await tick();
  147. expect(errorValue.message).toEqual('Object Error');
  148. });
  149. });
  150. describe('with events', function () {
  151. it('fetches data on mount', async function () {
  152. const getMock = MockApiClient.addMockResponse({
  153. url: '/organizations/test-org/events/',
  154. body: {
  155. meta: {fields: {transaction: 'string', count: 'number'}},
  156. data: [{transaction: '/health', count: 1000}],
  157. },
  158. });
  159. render(
  160. <DiscoverQuery
  161. orgSlug="test-org"
  162. api={api}
  163. location={location}
  164. eventView={eventView}
  165. useEvents
  166. >
  167. {({tableData, isLoading}) => {
  168. if (isLoading) {
  169. return 'loading';
  170. }
  171. return <p>{tableData.data[0].transaction}</p>;
  172. }}
  173. </DiscoverQuery>
  174. );
  175. await tick();
  176. // Children should be rendered, and API should be called.
  177. expect(getMock).toHaveBeenCalledTimes(1);
  178. expect(await screen.findByText('/health')).toBeInTheDocument();
  179. });
  180. it('applies limit and cursor props', async function () {
  181. const getMock = MockApiClient.addMockResponse({
  182. url: '/organizations/test-org/events/',
  183. body: {
  184. meta: {fields: {transaction: 'string', count: 'number'}},
  185. data: [{transaction: '/health', count: 1000}],
  186. },
  187. });
  188. render(
  189. <DiscoverQuery
  190. orgSlug="test-org"
  191. api={api}
  192. location={location}
  193. eventView={eventView}
  194. limit={3}
  195. cursor="1:0:1"
  196. useEvents
  197. >
  198. {({tableData, isLoading}) => {
  199. if (isLoading) {
  200. return 'loading';
  201. }
  202. return <p>{tableData.data[0].transaction}</p>;
  203. }}
  204. </DiscoverQuery>
  205. );
  206. await tick();
  207. expect(getMock).toHaveBeenCalledTimes(1);
  208. expect(getMock).toHaveBeenCalledWith(
  209. '/organizations/test-org/events/',
  210. expect.objectContaining({
  211. method: 'GET',
  212. query: expect.objectContaining({
  213. per_page: 3,
  214. cursor: '1:0:1',
  215. }),
  216. })
  217. );
  218. });
  219. it('parses string errors correctly', async function () {
  220. MockApiClient.addMockResponse({
  221. url: '/organizations/test-org/events/',
  222. body: {
  223. detail: 'Error Message',
  224. },
  225. statusCode: 400,
  226. });
  227. let errorValue;
  228. render(
  229. <DiscoverQuery
  230. orgSlug="test-org"
  231. api={api}
  232. location={location}
  233. eventView={eventView}
  234. setError={e => (errorValue = e)}
  235. useEvents
  236. >
  237. {({isLoading}) => {
  238. if (isLoading) {
  239. return 'loading';
  240. }
  241. return null;
  242. }}
  243. </DiscoverQuery>
  244. );
  245. await tick();
  246. expect(errorValue.message).toEqual('Error Message');
  247. });
  248. it('parses object errors correctly', async function () {
  249. MockApiClient.addMockResponse({
  250. url: '/organizations/test-org/events/',
  251. body: {
  252. detail: {
  253. code: '?',
  254. message: 'Object Error',
  255. extra: {},
  256. },
  257. },
  258. statusCode: 400,
  259. });
  260. let errorValue;
  261. render(
  262. <DiscoverQuery
  263. orgSlug="test-org"
  264. api={api}
  265. location={location}
  266. eventView={eventView}
  267. setError={e => (errorValue = e)}
  268. useEvents
  269. >
  270. {({isLoading}) => {
  271. if (isLoading) {
  272. return 'loading';
  273. }
  274. return null;
  275. }}
  276. </DiscoverQuery>
  277. );
  278. await tick();
  279. expect(errorValue.message).toEqual('Object Error');
  280. });
  281. });
  282. });