123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import {act, fireEvent, render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
- import {addErrorMessage} from 'sentry/actionCreators/indicator';
- import WrappedDataExport from 'sentry/components/dataExport';
- jest.mock('sentry/actionCreators/indicator');
- const mockUnauthorizedOrg = TestStubs.Organization({
- features: [],
- });
- const mockAuthorizedOrg = TestStubs.Organization({
- features: ['discover-query'],
- });
- const mockPayload = {
- queryType: 'Issues-by-Tag',
- queryInfo: {project_id: '1', group_id: '1027', key: 'user'},
- };
- const mockRouterContext = mockOrganization =>
- TestStubs.routerContext([
- {
- organization: mockOrganization,
- },
- ]);
- describe('DataExport', function () {
- it('should not render anything for an unauthorized organization', function () {
- render(<WrappedDataExport payload={mockPayload} />, {
- context: mockRouterContext(mockUnauthorizedOrg),
- });
- expect(screen.queryByRole('button')).not.toBeInTheDocument();
- });
- it('should render the button for an authorized organization', function () {
- render(<WrappedDataExport payload={mockPayload} />, {
- context: mockRouterContext(mockAuthorizedOrg),
- });
- expect(screen.getByText(/Export All to CSV/)).toBeInTheDocument();
- });
- it('should render custom children if provided', function () {
- render(
- <WrappedDataExport payload={mockPayload}>
- This is an example string
- </WrappedDataExport>,
- {context: mockRouterContext(mockAuthorizedOrg)}
- );
- expect(screen.getByText(/This is an example string/)).toBeInTheDocument();
- });
- it('should respect the disabled prop and not be clickable', function () {
- const postDataExport = MockApiClient.addMockResponse({
- url: `/organizations/${mockAuthorizedOrg.slug}/data-export/`,
- method: 'POST',
- body: {id: 721},
- });
- render(<WrappedDataExport payload={mockPayload} disabled />, {
- context: mockRouterContext(mockAuthorizedOrg),
- });
- fireEvent.click(screen.getByRole('button'));
- expect(postDataExport).not.toHaveBeenCalled();
- expect(screen.getByRole('button')).toBeDisabled();
- });
- it('should send a request and disable itself when clicked', async function () {
- const postDataExport = MockApiClient.addMockResponse({
- url: `/organizations/${mockAuthorizedOrg.slug}/data-export/`,
- method: 'POST',
- body: {id: 721},
- });
- render(<WrappedDataExport payload={mockPayload} />, {
- context: mockRouterContext(mockAuthorizedOrg),
- });
- act(() => {
- fireEvent.click(screen.getByRole('button'));
- });
- expect(postDataExport).toHaveBeenCalledWith(
- `/organizations/${mockAuthorizedOrg.slug}/data-export/`,
- {
- data: {
- query_type: mockPayload.queryType,
- query_info: mockPayload.queryInfo,
- },
- method: 'POST',
- error: expect.anything(),
- success: expect.anything(),
- }
- );
- await waitFor(() => {
- expect(screen.getByRole('button')).toBeDisabled();
- });
- });
- it('should reset the state when receiving a new payload', async function () {
- MockApiClient.addMockResponse({
- url: `/organizations/${mockAuthorizedOrg.slug}/data-export/`,
- method: 'POST',
- body: {id: 721},
- });
- const {rerender} = render(<WrappedDataExport payload={mockPayload} />, {
- context: mockRouterContext(mockAuthorizedOrg),
- });
- act(() => {
- fireEvent.click(screen.getByText(/Export All to CSV/));
- });
- await waitFor(() => {
- expect(screen.getByRole('button')).toBeDisabled();
- });
- rerender(<WrappedDataExport payload={{...mockPayload, queryType: 'Discover'}} />);
- await waitFor(() => {
- expect(screen.getByRole('button')).toBeEnabled();
- });
- });
- it('should display default error message if non provided', async function () {
- MockApiClient.addMockResponse({
- url: `/organizations/${mockAuthorizedOrg.slug}/data-export/`,
- method: 'POST',
- statusCode: 400,
- });
- render(<WrappedDataExport payload={mockPayload} />, {
- context: mockRouterContext(mockAuthorizedOrg),
- });
- act(() => {
- fireEvent.click(screen.getByRole('button'));
- });
- await waitFor(() => {
- expect(addErrorMessage).toHaveBeenCalledWith(
- "We tried our hardest, but we couldn't export your data. Give it another go."
- );
- });
- await waitFor(() => {
- expect(screen.getByRole('button')).toBeEnabled();
- });
- });
- it('should display provided error message', async function () {
- MockApiClient.addMockResponse({
- url: `/organizations/${mockAuthorizedOrg.slug}/data-export/`,
- method: 'POST',
- statusCode: 400,
- body: {detail: 'uh oh'},
- });
- render(<WrappedDataExport payload={mockPayload} />, {
- context: mockRouterContext(mockAuthorizedOrg),
- });
- act(() => {
- fireEvent.click(screen.getByRole('button'));
- });
- await waitFor(() => {
- expect(addErrorMessage).toHaveBeenCalledWith('uh oh');
- });
- });
- });
|