utils.spec.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {getAllViews} from 'sentry/views/discover/data';
  4. import {
  5. handleCreateQuery,
  6. handleDeleteQuery,
  7. handleUpdateQuery,
  8. handleUpdateQueryName,
  9. } from 'sentry/views/discover/savedQuery/utils';
  10. describe('SavedQueries API helpers', () => {
  11. const api = new MockApiClient();
  12. const organization = OrganizationFixture();
  13. const errorsQuery = getAllViews(organization).find(
  14. view => view.name === 'Errors by Title'
  15. )!;
  16. const errorsView = EventView.fromSavedQuery(errorsQuery);
  17. errorsView.id = '1'; // set id manually as errorsView.id is undefined
  18. const yAxis = ['count()', 'failure_count()'];
  19. let mockCall: jest.Mock;
  20. afterEach(() => {
  21. MockApiClient.clearMockResponses();
  22. });
  23. describe('handleCreateQuery', () => {
  24. beforeEach(() => {
  25. mockCall = MockApiClient.addMockResponse({
  26. method: 'POST',
  27. url: `/organizations/${organization.slug}/discover/saved/`,
  28. body: {data: {}, fromBody: {}},
  29. });
  30. });
  31. it('calls the correct API endpoint', async () => {
  32. const response = await handleCreateQuery(api, organization, errorsView, yAxis);
  33. expect(mockCall).toHaveBeenCalledWith(
  34. expect.anything(),
  35. expect.objectContaining({
  36. data: expect.objectContaining({yAxis}),
  37. })
  38. );
  39. expect(response).toEqual({data: {}, fromBody: {}});
  40. });
  41. });
  42. describe('handleUpdateQuery', () => {
  43. beforeEach(() => {
  44. mockCall = MockApiClient.addMockResponse({
  45. method: 'PUT',
  46. url: `/organizations/${organization.slug}/discover/saved/${errorsView.id}/`,
  47. body: {data: {}, fromBody: {}},
  48. });
  49. });
  50. it('calls the correct API endpoint', async () => {
  51. const response = await handleUpdateQuery(api, organization, errorsView, yAxis);
  52. expect(mockCall).toHaveBeenCalledWith(
  53. expect.anything(),
  54. expect.objectContaining({
  55. data: expect.objectContaining({yAxis}),
  56. })
  57. );
  58. expect(response).toEqual({data: {}, fromBody: {}});
  59. });
  60. });
  61. describe('handleUpdateQueryName', () => {
  62. beforeEach(() => {
  63. MockApiClient.addMockResponse({
  64. method: 'PUT',
  65. url: `/organizations/${organization.slug}/discover/saved/${errorsView.id}/`,
  66. body: {data: {}, fromBody: {}},
  67. });
  68. });
  69. it('calls the correct API endpoint', async () => {
  70. const response = await handleUpdateQueryName(api, organization, errorsView);
  71. expect(response).toEqual({data: {}, fromBody: {}});
  72. });
  73. });
  74. describe('handleDeleteQuery', () => {
  75. beforeEach(() => {
  76. MockApiClient.addMockResponse({
  77. method: 'DELETE',
  78. url: `/organizations/${organization.slug}/discover/saved/${errorsView.id}/`,
  79. body: {data: {}, fromBody: {}},
  80. });
  81. });
  82. it('calls the correct API endpoint', async () => {
  83. const response = await handleDeleteQuery(api, organization, errorsView);
  84. expect(response).toEqual({data: {}, fromBody: {}});
  85. });
  86. });
  87. });