123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import MemberListStore from 'sentry/stores/memberListStore';
- import {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
- import Dashboard from 'sentry/views/dashboardsV2/dashboard';
- import {DisplayType, Widget, WidgetType} from 'sentry/views/dashboardsV2/types';
- import {OrganizationContext} from '../organizationContext';
- describe('Dashboards > Dashboard', () => {
- const organization = TestStubs.Organization({
- features: ['dashboards-basic', 'dashboards-edit'],
- });
- const mockDashboard = {
- dateCreated: '2021-08-10T21:20:46.798237Z',
- id: '1',
- title: 'Test Dashboard',
- widgets: [],
- projects: [],
- filters: {},
- };
- const newWidget: Widget = {
- id: '1',
- title: 'Test Discover Widget',
- displayType: DisplayType.LINE,
- widgetType: WidgetType.DISCOVER,
- interval: '5m',
- queries: [
- {
- name: '',
- conditions: '',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: [],
- orderby: '',
- },
- ],
- };
- const issueWidget: Widget = {
- id: '2',
- title: 'Test Issue Widget',
- displayType: DisplayType.TABLE,
- widgetType: WidgetType.ISSUE,
- interval: '5m',
- queries: [
- {
- name: '',
- conditions: '',
- fields: ['title', 'assignee'],
- aggregates: [],
- columns: ['title', 'assignee'],
- orderby: '',
- },
- ],
- };
- let initialData, tagsMock;
- beforeEach(() => {
- initialData = initializeOrg({organization, router: {}, project: 1, projects: []});
- MockApiClient.addMockResponse({
- url: `/organizations/org-slug/dashboards/widgets/`,
- method: 'POST',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- method: 'GET',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/issues/',
- method: 'GET',
- body: [
- {
- annotations: [],
- id: '1',
- title: 'Error: Failed',
- project: {
- id: '3',
- },
- owners: [
- {
- type: 'ownershipRule',
- owner: 'user:2',
- },
- ],
- },
- ],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/users/',
- method: 'GET',
- body: [
- {
- user: {
- id: '2',
- name: 'test@sentry.io',
- email: 'test@sentry.io',
- avatar: {
- avatarType: 'letter_avatar',
- avatarUuid: null,
- },
- },
- },
- ],
- });
- tagsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/',
- method: 'GET',
- body: TestStubs.Tags(),
- });
- });
- it('fetches tags', () => {
- render(
- <Dashboard
- paramDashboardId="1"
- dashboard={mockDashboard}
- organization={initialData.organization}
- onUpdate={() => undefined}
- handleUpdateWidgetList={() => undefined}
- handleAddCustomWidget={() => undefined}
- router={initialData.router}
- location={initialData.router.location}
- widgetLimitReached={false}
- isEditing={false}
- />,
- {context: initialData.routerContext}
- );
- expect(tagsMock).toHaveBeenCalled();
- });
- it('dashboard adds new widget if component is mounted with newWidget prop', async () => {
- const mockHandleAddCustomWidget = jest.fn();
- const mockCallbackToUnsetNewWidget = jest.fn();
- render(
- <Dashboard
- paramDashboardId="1"
- dashboard={mockDashboard}
- organization={initialData.organization}
- isEditing={false}
- onUpdate={() => undefined}
- handleUpdateWidgetList={() => undefined}
- handleAddCustomWidget={mockHandleAddCustomWidget}
- router={initialData.router}
- location={initialData.router.location}
- newWidget={newWidget}
- widgetLimitReached={false}
- onSetNewWidget={mockCallbackToUnsetNewWidget}
- />,
- {context: initialData.routerContext}
- );
- await waitFor(() => expect(mockHandleAddCustomWidget).toHaveBeenCalled());
- expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
- });
- it('dashboard adds new widget if component updated with newWidget prop', async () => {
- const mockHandleAddCustomWidget = jest.fn();
- const mockCallbackToUnsetNewWidget = jest.fn();
- const {rerender} = render(
- <Dashboard
- paramDashboardId="1"
- dashboard={mockDashboard}
- organization={initialData.organization}
- isEditing={false}
- onUpdate={() => undefined}
- handleUpdateWidgetList={() => undefined}
- handleAddCustomWidget={mockHandleAddCustomWidget}
- router={initialData.router}
- location={initialData.router.location}
- widgetLimitReached={false}
- onSetNewWidget={mockCallbackToUnsetNewWidget}
- />,
- {context: initialData.routerContext}
- );
- expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
- expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
- // Re-render with newWidget prop
- rerender(
- <Dashboard
- paramDashboardId="1"
- dashboard={mockDashboard}
- organization={initialData.organization}
- isEditing={false}
- onUpdate={() => undefined}
- handleUpdateWidgetList={() => undefined}
- handleAddCustomWidget={mockHandleAddCustomWidget}
- router={initialData.router}
- location={initialData.router.location}
- widgetLimitReached={false}
- onSetNewWidget={mockCallbackToUnsetNewWidget}
- newWidget={newWidget}
- />
- );
- await waitFor(() => expect(mockHandleAddCustomWidget).toHaveBeenCalled());
- expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
- });
- it('dashboard does not try to add new widget if no newWidget', () => {
- const mockHandleAddCustomWidget = jest.fn();
- const mockCallbackToUnsetNewWidget = jest.fn();
- render(
- <Dashboard
- paramDashboardId="1"
- dashboard={mockDashboard}
- organization={initialData.organization}
- isEditing={false}
- onUpdate={() => undefined}
- handleUpdateWidgetList={() => undefined}
- handleAddCustomWidget={mockHandleAddCustomWidget}
- router={initialData.router}
- location={initialData.router.location}
- widgetLimitReached={false}
- onSetNewWidget={mockCallbackToUnsetNewWidget}
- />,
- {context: initialData.routerContext}
- );
- expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
- expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
- });
- describe('Issue Widgets', () => {
- beforeEach(() => {
- MemberListStore.init();
- });
- const mount = (dashboard, mockedOrg = initialData.organization) => {
- render(
- <OrganizationContext.Provider value={initialData.organization}>
- <MEPSettingProvider forceTransactions={false}>
- <Dashboard
- paramDashboardId="1"
- dashboard={dashboard}
- organization={mockedOrg}
- isEditing={false}
- onUpdate={() => undefined}
- handleUpdateWidgetList={() => undefined}
- handleAddCustomWidget={() => undefined}
- router={initialData.router}
- location={initialData.router.location}
- widgetLimitReached={false}
- />
- </MEPSettingProvider>
- </OrganizationContext.Provider>
- );
- };
- it('dashboard displays issue widgets if the user has issue widgets feature flag', async () => {
- const mockDashboardWithIssueWidget = {
- ...mockDashboard,
- widgets: [newWidget, issueWidget],
- };
- await act(async () => {
- mount(mockDashboardWithIssueWidget, organization);
- await tick();
- });
- expect(screen.getByText('Test Discover Widget')).toBeInTheDocument();
- expect(screen.getByText('Test Issue Widget')).toBeInTheDocument();
- });
- it('renders suggested assignees', async () => {
- const mockDashboardWithIssueWidget = {
- ...mockDashboard,
- widgets: [{...issueWidget}],
- };
- mount(mockDashboardWithIssueWidget, organization);
- expect(await screen.findByText('T')).toBeInTheDocument();
- userEvent.hover(screen.getByText('T'));
- expect(await screen.findByText('Suggestion: test@sentry.io')).toBeInTheDocument();
- expect(screen.getByText('Matching Issue Owners Rule')).toBeInTheDocument();
- });
- });
- describe('Edit mode', () => {
- let widgets: Widget[];
- const mount = (
- dashboard,
- mockedOrg = initialData.organization,
- mockedRouter = initialData.router,
- mockedLocation = initialData.router.location
- ) => {
- const getDashboardComponent = () => (
- <OrganizationContext.Provider value={initialData.organization}>
- <MEPSettingProvider forceTransactions={false}>
- <Dashboard
- paramDashboardId="1"
- dashboard={dashboard}
- organization={mockedOrg}
- isEditing
- onUpdate={newWidgets => {
- widgets.splice(0, widgets.length, ...newWidgets);
- }}
- handleUpdateWidgetList={() => undefined}
- handleAddCustomWidget={() => undefined}
- router={mockedRouter}
- location={mockedLocation}
- widgetLimitReached={false}
- />
- </MEPSettingProvider>
- </OrganizationContext.Provider>
- );
- const {rerender} = render(getDashboardComponent());
- return {rerender: () => rerender(getDashboardComponent())};
- };
- beforeEach(() => {
- widgets = [newWidget];
- });
- it('displays the copy widget button in edit mode', async () => {
- const dashboardWithOneWidget = {...mockDashboard, widgets};
- await act(async () => {
- mount(dashboardWithOneWidget);
- await tick();
- });
- expect(screen.getByLabelText('Duplicate Widget')).toBeInTheDocument();
- });
- it('duplicates the widget', async () => {
- const dashboardWithOneWidget = {...mockDashboard, widgets};
- await act(async () => {
- const {rerender} = mount(dashboardWithOneWidget);
- userEvent.click(screen.getByLabelText('Duplicate Widget'));
- rerender();
- await tick();
- });
- expect(screen.getAllByText('Test Discover Widget')).toHaveLength(2);
- });
- it('opens the widget builder when editing with the modal access flag', async function () {
- const testData = initializeOrg({
- ...initializeOrg(),
- organization: {
- features: ['dashboards-basic', 'dashboards-edit'],
- },
- });
- const dashboardWithOneWidget = {
- ...mockDashboard,
- widgets: [newWidget],
- };
- await act(async () => {
- mount(
- dashboardWithOneWidget,
- testData.organization,
- testData.router,
- testData.router.location
- );
- await tick();
- userEvent.click(screen.getByLabelText('Edit Widget'));
- });
- expect(testData.router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/dashboard/1/widget/0/edit/',
- })
- );
- });
- });
- });
|