|
| 1 | +import {initializeOrg} from 'sentry-test/initializeOrg'; |
| 2 | +import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary'; |
| 3 | + |
| 4 | +import {SavedQueriesTable} from 'sentry/views/explore/savedQueries/savedQueriesTable'; |
| 5 | + |
| 6 | +describe('SavedQueriesTable', () => { |
| 7 | + const {organization} = initializeOrg(); |
| 8 | + let getQueriesMock: jest.Mock; |
| 9 | + let deleteQueryMock: jest.Mock; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + getQueriesMock = MockApiClient.addMockResponse({ |
| 13 | + url: `/organizations/${organization.slug}/explore/saved/`, |
| 14 | + body: [ |
| 15 | + { |
| 16 | + id: 1, |
| 17 | + name: 'Query Name', |
| 18 | + visualize: [], |
| 19 | + projects: [1], |
| 20 | + createdBy: { |
| 21 | + name: 'Test User', |
| 22 | + }, |
| 23 | + }, |
| 24 | + ], |
| 25 | + }); |
| 26 | + deleteQueryMock = MockApiClient.addMockResponse({ |
| 27 | + url: `/organizations/${organization.slug}/explore/saved/1/`, |
| 28 | + method: 'DELETE', |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + MockApiClient.clearMockResponses(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should render', async () => { |
| 37 | + render(<SavedQueriesTable mode="owned" />); |
| 38 | + expect(screen.getByText('Name')).toBeInTheDocument(); |
| 39 | + expect(screen.getByText('Projects')).toBeInTheDocument(); |
| 40 | + expect(screen.getByText('Query')).toBeInTheDocument(); |
| 41 | + expect(screen.getByText('Owner')).toBeInTheDocument(); |
| 42 | + expect(screen.getByText('Access')).toBeInTheDocument(); |
| 43 | + expect(screen.getByText('Last Viewed')).toBeInTheDocument(); |
| 44 | + await screen.findByText('Query Name'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should request for owned queries', async () => { |
| 48 | + render(<SavedQueriesTable mode="owned" />); |
| 49 | + await waitFor(() => |
| 50 | + expect(getQueriesMock).toHaveBeenCalledWith( |
| 51 | + `/organizations/${organization.slug}/explore/saved/`, |
| 52 | + expect.objectContaining({ |
| 53 | + method: 'GET', |
| 54 | + query: expect.objectContaining({sortBy: 'mostPopular', exclude: 'shared'}), |
| 55 | + }) |
| 56 | + ) |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should request for shared queries', async () => { |
| 61 | + render(<SavedQueriesTable mode="shared" />); |
| 62 | + await waitFor(() => |
| 63 | + expect(getQueriesMock).toHaveBeenCalledWith( |
| 64 | + `/organizations/${organization.slug}/explore/saved/`, |
| 65 | + expect.objectContaining({ |
| 66 | + method: 'GET', |
| 67 | + query: expect.objectContaining({ |
| 68 | + sortBy: 'mostPopular', |
| 69 | + exclude: 'owned', |
| 70 | + }), |
| 71 | + }) |
| 72 | + ) |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('deletes a query', async () => { |
| 77 | + render(<SavedQueriesTable mode="owned" />); |
| 78 | + await screen.findByText('Query Name'); |
| 79 | + await userEvent.click(screen.getByLabelText('Query actions')); |
| 80 | + await userEvent.click(screen.getByText('Delete')); |
| 81 | + await waitFor(() => |
| 82 | + expect(deleteQueryMock).toHaveBeenCalledWith( |
| 83 | + `/organizations/${organization.slug}/explore/saved/1/`, |
| 84 | + expect.objectContaining({ |
| 85 | + method: 'DELETE', |
| 86 | + }) |
| 87 | + ) |
| 88 | + ); |
| 89 | + }); |
| 90 | +}); |
0 commit comments