import {mountWithTheme} from 'sentry-test/enzyme'; import {initializeOrg} from 'sentry-test/initializeOrg'; import Dashboard from 'sentry/views/dashboardsV2/dashboard'; import {DisplayType, WidgetType} from 'sentry/views/dashboardsV2/types'; 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 = { title: 'Test Query', displayType: DisplayType.LINE, widgetType: WidgetType.DISCOVER, interval: '5m', queries: [ { name: '', conditions: '', fields: ['count()'], aggregates: ['count()'], columns: [], orderby: '', }, ], }; let initialData; 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/users/', method: 'GET', body: [], }); MockApiClient.addMockResponse({ url: '/organizations/org-slug/tags/', method: 'GET', body: TestStubs.Tags(), }); }); it('dashboard adds new widget if component is mounted with newWidget prop', async () => { const mockHandleAddCustomWidget = jest.fn(); const mockCallbackToUnsetNewWidget = jest.fn(); const wrapper = mountWithTheme( undefined} handleUpdateWidgetList={() => undefined} handleAddCustomWidget={mockHandleAddCustomWidget} router={initialData.router} location={initialData.location} newWidget={newWidget} widgetLimitReached={false} onSetNewWidget={mockCallbackToUnsetNewWidget} />, initialData.routerContext ); await tick(); wrapper.update(); 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 wrapper = mountWithTheme( undefined} handleUpdateWidgetList={() => undefined} handleAddCustomWidget={mockHandleAddCustomWidget} router={initialData.router} location={initialData.location} widgetLimitReached={false} onSetNewWidget={mockCallbackToUnsetNewWidget} />, initialData.routerContext ); expect(mockHandleAddCustomWidget).not.toHaveBeenCalled(); expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled(); wrapper.setProps({newWidget}); await tick(); wrapper.update(); expect(mockHandleAddCustomWidget).toHaveBeenCalled(); expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled(); }); it('dashboard does not try to add new widget if no newWidget', async () => { const mockHandleAddCustomWidget = jest.fn(); const mockCallbackToUnsetNewWidget = jest.fn(); const wrapper = mountWithTheme( undefined} handleUpdateWidgetList={() => undefined} handleAddCustomWidget={mockHandleAddCustomWidget} router={initialData.router} location={initialData.location} widgetLimitReached={false} onSetNewWidget={mockCallbackToUnsetNewWidget} />, initialData.routerContext ); await tick(); wrapper.update(); expect(mockHandleAddCustomWidget).not.toHaveBeenCalled(); expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled(); }); it('displays widgets with drag handle when in edit mode', () => { const dashboardWithOneWidget = {...mockDashboard, widgets: [newWidget]}; const wrapper = mountWithTheme( undefined} handleUpdateWidgetList={() => undefined} handleAddCustomWidget={() => undefined} router={initialData.router} location={initialData.location} widgetLimitReached={false} isEditing />, initialData.routerContext ); expect(wrapper.find('GrabbableButton')).toHaveLength(1); }); });