123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- import selectEvent from 'react-select-event';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import {ModalRenderProps} from 'sentry/actionCreators/modal';
- import AddToDashboardModal from 'sentry/components/modals/widgetBuilder/addToDashboardModal';
- import {
- DashboardDetails,
- DashboardListItem,
- DashboardWidgetSource,
- DisplayType,
- } from 'sentry/views/dashboardsV2/types';
- const stubEl = (props: {children?: React.ReactNode}) => <div>{props.children}</div>;
- const mockWidgetAsQueryParams = {
- defaultTableColumns: ['field1', 'field2'],
- defaultTitle: 'Default title',
- defaultWidgetQuery: '',
- displayType: DisplayType.LINE,
- environment: [],
- project: [],
- source: DashboardWidgetSource.DISCOVERV2,
- };
- describe('add to dashboard modal', () => {
- let eventsStatsMock;
- let initialData;
- const testDashboardListItem: DashboardListItem = {
- id: '1',
- title: 'Test Dashboard',
- createdBy: undefined,
- dateCreated: '2020-01-01T00:00:00.000Z',
- widgetDisplay: [DisplayType.AREA],
- widgetPreview: [],
- };
- const testDashboard: DashboardDetails = {
- id: '1',
- title: 'Test Dashboard',
- createdBy: undefined,
- dateCreated: '2020-01-01T00:00:00.000Z',
- widgets: [],
- projects: [1],
- period: '1h',
- filters: {release: ['abc@v1.2.0']},
- };
- let widget = {
- title: 'Test title',
- description: 'Test description',
- displayType: DisplayType.LINE,
- interval: '5m',
- queries: [
- {
- conditions: '',
- fields: ['count()'],
- aggregates: ['count()'],
- fieldAliases: [],
- columns: [] as string[],
- orderby: '',
- name: '',
- },
- ],
- };
- const defaultSelection = {
- projects: [],
- environments: [],
- datetime: {
- start: null,
- end: null,
- period: '24h',
- utc: false,
- },
- };
- beforeEach(() => {
- initialData = initializeOrg({
- ...initializeOrg(),
- organization: {
- features: ['new-widget-builder-experience-design'],
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/',
- body: [{...testDashboardListItem, widgetDisplay: [DisplayType.AREA]}],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/1/',
- body: testDashboard,
- });
- eventsStatsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: [],
- });
- });
- it('renders with the widget title and description', async function () {
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- expect(screen.getByText('Test title')).toBeInTheDocument();
- expect(screen.getByText('Select Dashboard')).toBeInTheDocument();
- expect(
- screen.getByText(
- 'This is a preview of how the widget will appear in your dashboard.'
- )
- ).toBeInTheDocument();
- expect(screen.getByRole('button', {name: 'Add + Stay in Discover'})).toBeDisabled();
- expect(screen.getByRole('button', {name: 'Open in Widget Builder'})).toBeDisabled();
- });
- it('enables the buttons when a dashboard is selected', async function () {
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- expect(screen.getByRole('button', {name: 'Add + Stay in Discover'})).toBeDisabled();
- expect(screen.getByRole('button', {name: 'Open in Widget Builder'})).toBeDisabled();
- await selectEvent.select(screen.getByText('Select Dashboard'), 'Test Dashboard');
- expect(screen.getByRole('button', {name: 'Add + Stay in Discover'})).toBeEnabled();
- expect(screen.getByRole('button', {name: 'Open in Widget Builder'})).toBeEnabled();
- });
- it('includes a New Dashboard option in the selector with saved dashboards', async function () {
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- selectEvent.openMenu(screen.getByText('Select Dashboard'));
- expect(screen.getByText('+ Create New Dashboard')).toBeInTheDocument();
- expect(screen.getByText('Test Dashboard')).toBeInTheDocument();
- });
- it('applies dashboard saved filters to visualization', async function () {
- initialData.organization = {
- ...initialData.organization,
- features: ['new-widget-builder-experience-design', 'dashboards-top-level-filter'],
- };
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- environment: [],
- project: [],
- interval: '5m',
- orderby: '',
- statsPeriod: '24h',
- yAxis: ['count()'],
- }),
- })
- );
- await selectEvent.select(screen.getByText('Select Dashboard'), 'Test Dashboard');
- expect(eventsStatsMock).toHaveBeenLastCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- environment: [],
- interval: '5m',
- orderby: '',
- partial: '1',
- project: [1],
- query: ' release:abc@v1.2.0 ',
- statsPeriod: '1h',
- yAxis: ['count()'],
- }),
- })
- );
- });
- it('calls the events stats endpoint with the query and selection values', async function () {
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- environment: [],
- project: [],
- interval: '5m',
- orderby: '',
- statsPeriod: '24h',
- yAxis: ['count()'],
- }),
- })
- );
- });
- it('navigates to the widget builder when clicking Open in Widget Builder', async () => {
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- await selectEvent.select(screen.getByText('Select Dashboard'), 'Test Dashboard');
- userEvent.click(screen.getByText('Open in Widget Builder'));
- expect(initialData.router.push).toHaveBeenCalledWith({
- pathname: '/organizations/org-slug/dashboard/1/widget/new/',
- query: mockWidgetAsQueryParams,
- });
- });
- it('navigates to the widget builder with saved filters', async () => {
- initialData.organization = {
- ...initialData.organization,
- features: ['new-widget-builder-experience-design', 'dashboards-top-level-filter'],
- };
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- await selectEvent.select(screen.getByText('Select Dashboard'), 'Test Dashboard');
- userEvent.click(screen.getByText('Open in Widget Builder'));
- expect(initialData.router.push).toHaveBeenCalledWith({
- pathname: '/organizations/org-slug/dashboard/1/widget/new/',
- query: expect.objectContaining({
- defaultTableColumns: ['field1', 'field2'],
- defaultTitle: 'Default title',
- defaultWidgetQuery: '',
- displayType: DisplayType.LINE,
- project: [1],
- source: DashboardWidgetSource.DISCOVERV2,
- statsPeriod: '1h',
- }),
- });
- });
- it('updates the selected dashboard with the widget when clicking Add + Stay in Discover', async () => {
- const dashboardDetailGetMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/1/',
- body: {id: '1', widgets: []},
- });
- const dashboardDetailPutMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/1/',
- method: 'PUT',
- body: {},
- });
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- await selectEvent.select(screen.getByText('Select Dashboard'), 'Test Dashboard');
- userEvent.click(screen.getByText('Add + Stay in Discover'));
- expect(dashboardDetailGetMock).toHaveBeenCalled();
- // mocked widgets response is an empty array, assert this new widget
- // is sent as an update to the dashboard
- await waitFor(() => {
- expect(dashboardDetailPutMock).toHaveBeenCalledWith(
- '/organizations/org-slug/dashboards/1/',
- expect.objectContaining({data: expect.objectContaining({widgets: [widget]})})
- );
- });
- });
- it('clears sort when clicking Add + Stay in Discover with line chart', async () => {
- const dashboardDetailGetMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/1/',
- body: {id: '1', widgets: []},
- });
- const dashboardDetailPutMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/1/',
- method: 'PUT',
- body: {},
- });
- widget = {
- ...widget,
- queries: [
- {
- conditions: '',
- fields: ['count()'],
- aggregates: ['count()'],
- fieldAliases: [],
- columns: [],
- orderby: '-project',
- name: '',
- },
- ],
- };
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- await selectEvent.select(screen.getByText('Select Dashboard'), 'Test Dashboard');
- userEvent.click(screen.getByText('Add + Stay in Discover'));
- expect(dashboardDetailGetMock).toHaveBeenCalled();
- // mocked widgets response is an empty array, assert this new widget
- // is sent as an update to the dashboard
- await waitFor(() => {
- expect(dashboardDetailPutMock).toHaveBeenCalledWith(
- '/organizations/org-slug/dashboards/1/',
- expect.objectContaining({
- data: expect.objectContaining({
- widgets: [
- {
- description: 'Test description',
- displayType: 'line',
- interval: '5m',
- queries: [
- {
- aggregates: ['count()'],
- columns: [],
- conditions: '',
- fieldAliases: [],
- fields: ['count()'],
- name: '',
- orderby: '',
- },
- ],
- title: 'Test title',
- },
- ],
- }),
- })
- );
- });
- });
- it('saves sort when clicking Add + Stay in Discover with top period chart', async () => {
- const dashboardDetailGetMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/1/',
- body: {id: '1', widgets: []},
- });
- const dashboardDetailPutMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/1/',
- method: 'PUT',
- body: {},
- });
- widget = {
- ...widget,
- displayType: DisplayType.TOP_N,
- queries: [
- {
- conditions: '',
- fields: ['count()'],
- aggregates: ['count()'],
- fieldAliases: [],
- columns: ['project'],
- orderby: '-project',
- name: '',
- },
- ],
- };
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- await selectEvent.select(screen.getByText('Select Dashboard'), 'Test Dashboard');
- userEvent.click(screen.getByText('Add + Stay in Discover'));
- expect(dashboardDetailGetMock).toHaveBeenCalled();
- // mocked widgets response is an empty array, assert this new widget
- // is sent as an update to the dashboard
- await waitFor(() => {
- expect(dashboardDetailPutMock).toHaveBeenCalledWith(
- '/organizations/org-slug/dashboards/1/',
- expect.objectContaining({
- data: expect.objectContaining({
- widgets: [
- {
- description: 'Test description',
- displayType: 'top_n',
- interval: '5m',
- limit: 5,
- queries: [
- {
- aggregates: ['count()'],
- columns: ['project'],
- conditions: '',
- fieldAliases: [],
- fields: ['count()'],
- name: '',
- orderby: '-project',
- },
- ],
- title: 'Test title',
- },
- ],
- }),
- })
- );
- });
- });
- it('disables Add + Stay in Discover when a new dashboard is selected', async () => {
- render(
- <AddToDashboardModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={widget}
- selection={defaultSelection}
- router={initialData.router}
- widgetAsQueryParams={mockWidgetAsQueryParams}
- location={TestStubs.location()}
- />
- );
- await waitFor(() => {
- expect(screen.getByText('Select Dashboard')).toBeEnabled();
- });
- await selectEvent.select(
- screen.getByText('Select Dashboard'),
- '+ Create New Dashboard'
- );
- expect(screen.getByRole('button', {name: 'Add + Stay in Discover'})).toBeDisabled();
- expect(screen.getByRole('button', {name: 'Open in Widget Builder'})).toBeEnabled();
- });
- });
|