123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
- import {openAddToDashboardModal} from 'sentry/actionCreators/modal';
- import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
- import {
- PageParamsProvider,
- useSetExploreMode,
- useSetExploreVisualizes,
- } from 'sentry/views/explore/contexts/pageParamsContext';
- import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
- import {useAddToDashboard} from 'sentry/views/explore/hooks/useAddToDashboard';
- import {ChartType} from 'sentry/views/insights/common/components/chart';
- jest.mock('sentry/actionCreators/modal');
- describe('AddToDashboardButton', () => {
- let setMode: ReturnType<typeof useSetExploreMode>;
- let setVisualizes: ReturnType<typeof useSetExploreVisualizes>;
- function TestPage({visualizeIndex}: {visualizeIndex: number}) {
- setMode = useSetExploreMode();
- setVisualizes = useSetExploreVisualizes();
- const {addToDashboard} = useAddToDashboard();
- return (
- <button onClick={() => addToDashboard(visualizeIndex)}>Add to Dashboard</button>
- );
- }
- beforeEach(() => {
- jest.clearAllMocks();
- });
- it('opens the dashboard modal with the correct query for samples mode', async () => {
- render(
- <PageParamsProvider>
- <TestPage visualizeIndex={0} />
- </PageParamsProvider>
- );
- await userEvent.click(screen.getByText('Add to Dashboard'));
- // The table columns are encoded as the fields for the defaultWidgetQuery
- expect(openAddToDashboardModal).toHaveBeenCalledWith(
- expect.objectContaining({
- // For Add + Stay on Page
- widget: {
- title: 'Custom Widget',
- displayType: DisplayType.LINE,
- interval: undefined,
- limit: undefined,
- widgetType: WidgetType.SPANS,
- queries: [
- {
- aggregates: ['avg(span.duration)'],
- columns: [],
- fields: ['avg(span.duration)'],
- conditions: '',
- orderby: '-timestamp',
- name: '',
- },
- ],
- },
- // For Open in Widget Builder
- widgetAsQueryParams: expect.objectContaining({
- dataset: WidgetType.SPANS,
- defaultTableColumns: [
- 'id',
- 'span.op',
- 'span.description',
- 'span.duration',
- 'transaction',
- 'timestamp',
- ],
- defaultTitle: 'Custom Widget',
- defaultWidgetQuery:
- 'name=&aggregates=avg(span.duration)&columns=&fields=avg(span.duration)&conditions=&orderby=-timestamp',
- displayType: DisplayType.LINE,
- field: [
- 'id',
- 'span.op',
- 'span.description',
- 'span.duration',
- 'transaction',
- 'timestamp',
- ],
- }),
- })
- );
- });
- it('opens the dashboard modal with the correct query based on the visualize index', async () => {
- render(
- <PageParamsProvider>
- <TestPage visualizeIndex={1} />
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- act(() =>
- setVisualizes([
- {
- yAxes: ['avg(span.duration)'],
- chartType: ChartType.LINE,
- },
- {
- yAxes: ['max(span.duration)'],
- chartType: ChartType.LINE,
- },
- ])
- );
- await userEvent.click(screen.getByText('Add to Dashboard'));
- // The group by and the yAxes are encoded as the fields for the defaultTableQuery
- expect(openAddToDashboardModal).toHaveBeenCalledWith(
- expect.objectContaining({
- // For Add + Stay on Page
- widget: {
- title: 'Custom Widget',
- displayType: DisplayType.LINE,
- interval: undefined,
- limit: undefined,
- widgetType: WidgetType.SPANS,
- queries: [
- {
- aggregates: ['max(span.duration)'],
- columns: [],
- fields: ['max(span.duration)'],
- conditions: '',
- orderby: '-timestamp',
- name: '',
- },
- ],
- },
- // For Open in Widget Builder
- widgetAsQueryParams: expect.objectContaining({
- dataset: WidgetType.SPANS,
- defaultTableColumns: [
- 'id',
- 'span.op',
- 'span.description',
- 'span.duration',
- 'transaction',
- 'timestamp',
- ],
- defaultTitle: 'Custom Widget',
- defaultWidgetQuery:
- 'name=&aggregates=max(span.duration)&columns=&fields=max(span.duration)&conditions=&orderby=-timestamp',
- displayType: DisplayType.LINE,
- field: [
- 'id',
- 'span.op',
- 'span.description',
- 'span.duration',
- 'transaction',
- 'timestamp',
- ],
- }),
- })
- );
- });
- it('uses the yAxes for the aggregate mode', async () => {
- render(
- <PageParamsProvider>
- <TestPage visualizeIndex={0} />
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- act(() => setMode(Mode.AGGREGATE));
- await userEvent.click(screen.getByText('Add to Dashboard'));
- expect(openAddToDashboardModal).toHaveBeenCalledWith(
- expect.objectContaining({
- // For Add + Stay on Page
- widget: {
- title: 'Custom Widget',
- displayType: DisplayType.LINE,
- interval: undefined,
- limit: undefined,
- widgetType: WidgetType.SPANS,
- queries: [
- {
- aggregates: ['avg(span.duration)'],
- columns: [],
- fields: ['avg(span.duration)'],
- conditions: '',
- orderby: '-avg(span.duration)',
- name: '',
- },
- ],
- },
- // For Open in Widget Builder
- widgetAsQueryParams: expect.objectContaining({
- dataset: WidgetType.SPANS,
- defaultTableColumns: ['span.op', 'avg(span.duration)'],
- defaultTitle: 'Custom Widget',
- defaultWidgetQuery:
- 'name=&aggregates=avg(span.duration)&columns=&fields=avg(span.duration)&conditions=&orderby=-avg(span.duration)',
- displayType: DisplayType.LINE,
- field: ['span.op', 'avg(span.duration)'],
- }),
- })
- );
- });
- it('takes the first 3 yAxes', async () => {
- render(
- <PageParamsProvider>
- <TestPage visualizeIndex={0} />
- </PageParamsProvider>,
- {disableRouterMocks: true}
- );
- act(() => setMode(Mode.AGGREGATE));
- act(() =>
- setVisualizes([
- {
- yAxes: [
- 'avg(span.duration)',
- 'max(span.duration)',
- 'min(span.duration)',
- 'p90(span.duration)',
- ],
- chartType: ChartType.LINE,
- },
- ])
- );
- await userEvent.click(screen.getByText('Add to Dashboard'));
- expect(openAddToDashboardModal).toHaveBeenCalledWith(
- expect.objectContaining({
- // For Add + Stay on Page
- widget: {
- title: 'Custom Widget',
- displayType: DisplayType.LINE,
- interval: undefined,
- limit: undefined,
- widgetType: WidgetType.SPANS,
- queries: [
- {
- aggregates: [
- 'avg(span.duration)',
- 'max(span.duration)',
- 'min(span.duration)',
- ],
- columns: [],
- fields: ['avg(span.duration)', 'max(span.duration)', 'min(span.duration)'],
- conditions: '',
- orderby: '-avg(span.duration)',
- name: '',
- },
- ],
- },
- // For Open in Widget Builder
- widgetAsQueryParams: expect.objectContaining({
- dataset: WidgetType.SPANS,
- defaultTableColumns: [
- 'span.op',
- 'avg(span.duration)',
- 'max(span.duration)',
- 'min(span.duration)',
- ],
- defaultTitle: 'Custom Widget',
- defaultWidgetQuery:
- 'name=&aggregates=avg(span.duration)%2Cmax(span.duration)%2Cmin(span.duration)&columns=&fields=avg(span.duration)%2Cmax(span.duration)%2Cmin(span.duration)&conditions=&orderby=-avg(span.duration)',
- displayType: DisplayType.LINE,
- field: [
- 'span.op',
- 'avg(span.duration)',
- 'max(span.duration)',
- 'min(span.duration)',
- ],
- }),
- })
- );
- });
- });
|