Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dashboard: Fix plugin dashboard save as button (#55197)
* render SaveDashboardErrorProxy only when error is not yet handled * improve dashbaoard drawer test
- Loading branch information
Showing
3 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
public/app/features/dashboard/components/SaveDashboard/SaveDashboardDrawer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { render, waitFor, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import { configureStore } from 'app/store/configureStore'; | ||
|
||
import { DashboardModel } from '../../state'; | ||
|
||
import { SaveDashboardDrawer } from './SaveDashboardDrawer'; | ||
|
||
jest.mock('app/core/core', () => ({ | ||
...jest.requireActual('app/core/core'), | ||
contextSrv: {}, | ||
})); | ||
|
||
jest.mock('@grafana/runtime', () => ({ | ||
...jest.requireActual('@grafana/runtime'), | ||
getBackendSrv: () => ({ | ||
post: mockPost, | ||
}), | ||
})); | ||
|
||
jest.mock('app/core/services/backend_srv', () => ({ | ||
backendSrv: { | ||
getDashboardByUid: jest.fn().mockResolvedValue({ dashboard: {} }), | ||
}, | ||
})); | ||
|
||
const store = configureStore(); | ||
const mockPost = jest.fn(); | ||
const buildMocks = () => ({ | ||
dashboard: new DashboardModel({ | ||
uid: 'mockDashboardUid', | ||
version: 1, | ||
}), | ||
error: { | ||
status: 412, | ||
data: { | ||
status: 'plugin-dashboard', | ||
}, | ||
config: {}, | ||
}, | ||
onDismiss: jest.fn(), | ||
}); | ||
|
||
interface CompProps { | ||
dashboard: DashboardModel; | ||
onDismiss: () => void; | ||
} | ||
const CompWithProvider = (props: CompProps) => ( | ||
<Provider store={store}> | ||
<SaveDashboardDrawer {...props} /> | ||
</Provider> | ||
); | ||
|
||
const setup = (options: CompProps) => waitFor(() => render(<CompWithProvider {...options} />)); | ||
|
||
describe('SaveDashboardDrawer', () => { | ||
beforeEach(() => { | ||
mockPost.mockClear(); | ||
jest.spyOn(console, 'error').mockImplementation(); | ||
}); | ||
|
||
it("renders a modal if there's an unhandled error", async () => { | ||
const { onDismiss, dashboard, error } = buildMocks(); | ||
mockPost.mockRejectedValueOnce(error); | ||
|
||
await setup({ dashboard, onDismiss }); | ||
|
||
await userEvent.click(screen.getByRole('button', { name: /save/i })); | ||
|
||
expect(screen.getByRole('button', { name: /cancel/i })).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: /save as/i })).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: /overwrite/i })).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render corresponding save modal once the errror is handled', async () => { | ||
const { onDismiss, dashboard, error } = buildMocks(); | ||
mockPost.mockRejectedValueOnce(error); | ||
|
||
const { rerender } = await setup({ dashboard, onDismiss }); | ||
|
||
await userEvent.click(screen.getByRole('button', { name: /save/i })); | ||
rerender(<CompWithProvider dashboard={dashboard} onDismiss={onDismiss} />); | ||
|
||
mockPost.mockClear(); | ||
mockPost.mockRejectedValueOnce({ ...error, isHandled: true }); | ||
|
||
await userEvent.click(screen.getByRole('button', { name: /save/i })); | ||
|
||
expect(screen.getByText(/save dashboard/i)).toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: /save as/i })).not.toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: /overwrite/i })).not.toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters