123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599 |
- import ReactEchartsCore from 'echarts-for-react/lib/core';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import {ModalRenderProps} from 'sentry/actionCreators/modal';
- import WidgetViewerModal from 'sentry/components/modals/widgetViewerModal';
- import MemberListStore from 'sentry/stores/memberListStore';
- import PageFiltersStore from 'sentry/stores/pageFiltersStore';
- import space from 'sentry/styles/space';
- import {Series} from 'sentry/types/echarts';
- import {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
- import {AggregationOutputType} from 'sentry/utils/discover/fields';
- import {
- DisplayType,
- Widget,
- WidgetQuery,
- WidgetType,
- } from 'sentry/views/dashboardsV2/types';
- jest.mock('echarts-for-react/lib/core', () => {
- return jest.fn(({style}) => {
- return <div style={{...style, background: 'green'}}>echarts mock</div>;
- });
- });
- jest.mock('sentry/components/tooltip', () => {
- return jest.fn(props => {
- return <div>{props.children}</div>;
- });
- });
- const stubEl = (props: {children?: React.ReactNode}) => <div>{props.children}</div>;
- let eventsMetaMock;
- const waitForMetaToHaveBeenCalled = async () => {
- await waitFor(() => {
- expect(eventsMetaMock).toHaveBeenCalled();
- });
- };
- async function renderModal({
- initialData: {organization, routerContext},
- widget,
- seriesData,
- tableData,
- pageLinks,
- seriesResultsType,
- }: {
- initialData: any;
- widget: any;
- pageLinks?: string;
- seriesData?: Series[];
- seriesResultsType?: Record<string, AggregationOutputType>;
- tableData?: TableDataWithTitle[];
- }) {
- const rendered = render(
- <div style={{padding: space(4)}}>
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={organization}
- widget={widget}
- onEdit={() => undefined}
- seriesData={seriesData}
- tableData={tableData}
- pageLinks={pageLinks}
- seriesResultsType={seriesResultsType}
- />
- </div>,
- {
- context: routerContext,
- organization,
- }
- );
- // Need to wait since WidgetViewerModal will make a request to events-meta
- // for total events count on mount
- if (widget.widgetType === WidgetType.DISCOVER) {
- await waitForMetaToHaveBeenCalled();
- }
- return rendered;
- }
- describe('Modals -> WidgetViewerModal', function () {
- let initialData, initialDataWithFlag;
- beforeEach(() => {
- initialData = initializeOrg({
- organization: {
- features: ['discover-query', 'widget-viewer-modal'],
- apdexThreshold: 400,
- },
- router: {
- location: {query: {}},
- },
- project: 1,
- projects: [],
- });
- initialDataWithFlag = {
- ...initialData,
- organization: {
- ...initialData.organization,
- features: [
- ...initialData.organization.features,
- 'discover-frontend-use-events-endpoint',
- ],
- },
- };
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/projects/',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/releases/',
- body: [],
- });
- eventsMetaMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-meta/',
- body: {count: 33323612},
- });
- PageFiltersStore.init();
- PageFiltersStore.onInitializeUrlState(
- {
- projects: [1, 2],
- environments: ['prod', 'dev'],
- datetime: {start: null, end: null, period: '24h', utc: null},
- },
- new Set()
- );
- });
- afterEach(() => {
- MockApiClient.clearMockResponses();
- PageFiltersStore.teardown();
- });
- describe('Discover Widgets', function () {
- describe('Area Chart Widget', function () {
- let mockQuery: WidgetQuery;
- let additionalMockQuery: WidgetQuery;
- let mockWidget: Widget;
- function mockEventsv2() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- body: {
- data: [
- {
- title: '/organizations/:orgId/dashboards/',
- id: '1',
- count: 1,
- },
- ],
- meta: {
- title: 'string',
- id: 'string',
- count: 1,
- isMetricsData: false,
- },
- },
- });
- }
- function mockEvents() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- body: {
- data: [
- {
- title: '/organizations/:orgId/dashboards/',
- id: '1',
- count: 1,
- },
- ],
- meta: {
- fields: {
- title: 'string',
- id: 'string',
- count: 1,
- },
- isMetricsData: false,
- },
- },
- });
- }
- beforeEach(function () {
- mockQuery = {
- conditions: 'title:/organizations/:orgId/performance/summary/',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: [],
- name: 'Query Name',
- orderby: '',
- };
- additionalMockQuery = {
- conditions: '',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: [],
- name: 'Another Query Name',
- orderby: '',
- };
- mockWidget = {
- id: '1',
- title: 'Test Widget',
- displayType: DisplayType.AREA,
- interval: '5m',
- queries: [mockQuery, additionalMockQuery],
- widgetType: WidgetType.DISCOVER,
- };
- (ReactEchartsCore as jest.Mock).mockClear();
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: {
- data: [
- [[1646100000], [{count: 1}]],
- [[1646120000], [{count: 1}]],
- ],
- start: 1646100000,
- end: 1646120000,
- isMetricsData: false,
- },
- });
- });
- describe('with eventsv2', function () {
- it('renders Edit and Open buttons', async function () {
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('Edit Widget')).toBeInTheDocument();
- expect(screen.getByText('Open in Discover')).toBeInTheDocument();
- });
- it('renders updated table columns and orderby', async function () {
- const eventsv2Mock = mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('title')).toBeInTheDocument();
- expect(
- screen.getByText('/organizations/:orgId/dashboards/')
- ).toBeInTheDocument();
- expect(eventsv2Mock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({sort: ['-count()']}),
- })
- );
- });
- it('renders area chart', async function () {
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('echarts mock')).toBeInTheDocument();
- });
- it('renders Discover area chart widget viewer', async function () {
- mockEventsv2();
- const {container} = await renderModal({initialData, widget: mockWidget});
- expect(container).toSnapshot();
- });
- it('redirects user to Discover when clicking Open in Discover', async function () {
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- userEvent.click(screen.getByText('Open in Discover'));
- expect(initialData.router.push).toHaveBeenCalledWith(
- '/organizations/org-slug/discover/results/?environment=prod&environment=dev&field=count%28%29&name=Test%20Widget&project=1&project=2&query=title%3A%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2F&statsPeriod=24h&yAxis=count%28%29'
- );
- });
- it('zooms into the selected time range', async function () {
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- act(() => {
- // Simulate dataZoom event on chart
- (ReactEchartsCore as jest.Mock).mock.calls[0][0].onEvents.datazoom(
- undefined,
- {
- getModel: () => {
- return {
- _payload: {
- batch: [{startValue: 1646100000000, endValue: 1646120000000}],
- },
- };
- },
- }
- );
- });
- expect(initialData.router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- viewerEnd: '2022-03-01T07:33:20',
- viewerStart: '2022-03-01T02:00:00',
- },
- })
- );
- });
- it('renders multiquery label and selector', async function () {
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- expect(
- screen.getByText(
- 'This widget was built with multiple queries. Table data can only be displayed for one query at a time. To edit any of the queries, edit the widget.'
- )
- ).toBeInTheDocument();
- expect(screen.getByText('Query Name')).toBeInTheDocument();
- });
- it('updates selected query when selected in the query dropdown', async function () {
- mockEventsv2();
- const {rerender} = await renderModal({initialData, widget: mockWidget});
- userEvent.click(screen.getByText('Query Name'));
- userEvent.click(screen.getByText('Another Query Name'));
- expect(initialData.router.replace).toHaveBeenCalledWith({
- query: {query: 1},
- });
- // Need to manually set the new router location and rerender to simulate the dropdown selection click
- initialData.router.location.query = {query: ['1']};
- rerender(
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={mockWidget}
- onEdit={() => undefined}
- />
- );
- await waitForMetaToHaveBeenCalled();
- expect(screen.getByText('Another Query Name')).toBeInTheDocument();
- });
- it('renders the correct discover query link when there are multiple queries in a widget', async function () {
- mockEventsv2();
- initialData.router.location.query = {query: ['1']};
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByRole('button', {name: 'Open in Discover'})).toHaveAttribute(
- 'href',
- '/organizations/org-slug/discover/results/?environment=prod&environment=dev&field=count%28%29&name=Test%20Widget&project=1&project=2&query=&statsPeriod=24h&yAxis=count%28%29'
- );
- });
- it('renders with first legend disabled by default', async function () {
- mockEventsv2();
- // Rerender with first legend disabled
- initialData.router.location.query = {legend: ['Query Name']};
- await renderModal({initialData, widget: mockWidget});
- expect(ReactEchartsCore).toHaveBeenLastCalledWith(
- expect.objectContaining({
- option: expect.objectContaining({
- legend: expect.objectContaining({
- selected: {'Query Name': false},
- }),
- }),
- }),
- {}
- );
- });
- it('renders total results in footer', async function () {
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('33,323,612')).toBeInTheDocument();
- });
- it('renders highlighted query text and multiple queries in select dropdown', async function () {
- mockEventsv2();
- const {container} = await renderModal({
- initialData,
- widget: {
- ...mockWidget,
- queries: [{...mockQuery, name: ''}, additionalMockQuery],
- },
- });
- userEvent.click(screen.getByText('/organizations/:orgId/performance/summary/'));
- expect(container).toSnapshot();
- });
- it('renders widget chart minimap', async function () {
- initialData.organization.features.push('widget-viewer-modal-minimap');
- mockEventsv2();
- await renderModal({
- initialData,
- widget: {
- ...mockWidget,
- queries: [{...mockQuery, name: ''}, additionalMockQuery],
- },
- });
- expect(ReactEchartsCore).toHaveBeenLastCalledWith(
- expect.objectContaining({
- option: expect.objectContaining({
- dataZoom: expect.arrayContaining([
- expect.objectContaining({
- realtime: false,
- showDetail: false,
- end: 100,
- start: 0,
- }),
- ]),
- }),
- }),
- {}
- );
- });
- it('zooming on minimap updates location query and updates echart start and end values', async function () {
- initialData.organization.features.push('widget-viewer-modal-minimap');
- mockEventsv2();
- await renderModal({
- initialData,
- widget: {
- ...mockWidget,
- queries: [{...mockQuery, name: ''}, additionalMockQuery],
- },
- });
- const calls = (ReactEchartsCore as jest.Mock).mock.calls;
- act(() => {
- // Simulate dataZoom event on chart
- calls[calls.length - 1][0].onEvents.datazoom(
- {seriesStart: 1646100000000, seriesEnd: 1646120000000},
- {
- getModel: () => {
- return {
- _payload: {start: 30, end: 70},
- };
- },
- }
- );
- });
- expect(initialData.router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- viewerEnd: '2022-03-01T05:53:20',
- viewerStart: '2022-03-01T03:40:00',
- },
- })
- );
- });
- it('includes group by in widget viewer table', async function () {
- mockEventsv2();
- mockWidget.queries = [
- {
- conditions: 'title:/organizations/:orgId/performance/summary/',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: ['transaction'],
- name: 'Query Name',
- orderby: '-count()',
- },
- ];
- await renderModal({initialData, widget: mockWidget});
- screen.getByText('transaction');
- });
- it('includes order by in widget viewer table if not explicitly selected', async function () {
- mockEventsv2();
- mockWidget.queries = [
- {
- conditions: 'title:/organizations/:orgId/performance/summary/',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: ['transaction'],
- name: 'Query Name',
- orderby: 'count_unique(user)',
- },
- ];
- await renderModal({initialData, widget: mockWidget});
- screen.getByText('count_unique(user)');
- });
- it('includes a custom equation order by in widget viewer table if not explicitly selected', async function () {
- mockEventsv2();
- mockWidget.queries = [
- {
- conditions: 'title:/organizations/:orgId/performance/summary/',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: ['transaction'],
- name: 'Query Name',
- orderby: '-equation|count_unique(user) + 1',
- },
- ];
- await renderModal({initialData, widget: mockWidget});
- screen.getByText('count_unique(user) + 1');
- });
- });
- describe('with events', function () {
- it('renders updated table columns and orderby', async function () {
- const eventsMock = mockEvents();
- await renderModal({initialData: initialDataWithFlag, widget: mockWidget});
- expect(screen.getByText('title')).toBeInTheDocument();
- expect(
- screen.getByText('/organizations/:orgId/dashboards/')
- ).toBeInTheDocument();
- expect(eventsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events/',
- expect.objectContaining({
- query: expect.objectContaining({sort: ['-count()']}),
- })
- );
- });
- it('renders widget chart with y axis formatter using provided seriesResultType', async function () {
- mockEvents();
- await renderModal({
- initialData: initialDataWithFlag,
- widget: mockWidget,
- seriesData: [],
- seriesResultsType: {'count()': 'duration', 'count_unique()': 'duration'},
- });
- const calls = (ReactEchartsCore as jest.Mock).mock.calls;
- const yAxisFormatter =
- calls[calls.length - 1][0].option.yAxis.axisLabel.formatter;
- expect(yAxisFormatter(123)).toEqual('123ms');
- });
- it('renders widget chart with default number y axis formatter when seriesResultType has multiple different types', async function () {
- mockEvents();
- await renderModal({
- initialData: initialDataWithFlag,
- widget: mockWidget,
- seriesData: [],
- seriesResultsType: {'count()': 'duration', 'count_unique()': 'size'},
- });
- const calls = (ReactEchartsCore as jest.Mock).mock.calls;
- const yAxisFormatter =
- calls[calls.length - 1][0].option.yAxis.axisLabel.formatter;
- expect(yAxisFormatter(123)).toEqual('123');
- });
- it('does not allow sorting by transaction name when widget is using metrics', async function () {
- const eventsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- body: {
- data: [
- {
- title: '/organizations/:orgId/dashboards/',
- id: '1',
- count: 1,
- },
- ],
- meta: {
- fields: {
- title: 'string',
- id: 'string',
- count: 1,
- },
- isMetricsData: true,
- },
- },
- });
- await renderModal({
- initialData: initialDataWithFlag,
- widget: mockWidget,
- seriesData: [],
- seriesResultsType: {'count()': 'duration'},
- });
- expect(eventsMock).toHaveBeenCalledTimes(1);
- expect(screen.getByText('title')).toBeInTheDocument();
- userEvent.click(screen.getByText('title'));
- expect(initialData.router.push).not.toHaveBeenCalledWith({
- query: {sort: ['-title']},
- });
- });
- });
- });
- describe('TopN Chart Widget', function () {
- let mockQuery, mockWidget;
- function mockEventsStats() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: {
- data: [
- [[1646100000], [{count: 1}]],
- [[1646120000], [{count: 1}]],
- ],
- start: 1646100000,
- end: 1646120000,
- isMetricsData: false,
- },
- });
- }
- const eventsMockData = [
- {
- 'error.type': ['Test Error 1a', 'Test Error 1b', 'Test Error 1c'],
- count: 10,
- },
- {
- 'error.type': ['Test Error 2'],
- count: 6,
- },
- {
- 'error.type': ['Test Error 3'],
- count: 5,
- },
- {
- 'error.type': ['Test Error 4'],
- count: 4,
- },
- {
- 'error.type': ['Test Error 5'],
- count: 3,
- },
- {
- 'error.type': ['Test Error 6'],
- count: 2,
- },
- ];
- function mockEventsv2() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- match: [MockApiClient.matchQuery({cursor: undefined})],
- headers: {
- Link:
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:10:0>; rel="next"; results="true"; cursor="0:10:0"',
- },
- body: {
- data: eventsMockData,
- meta: {
- 'error.type': 'array',
- count: 'integer',
- },
- },
- });
- }
- function mockEvents() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- match: [MockApiClient.matchQuery({cursor: undefined})],
- headers: {
- Link:
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:10:0>; rel="next"; results="true"; cursor="0:10:0"',
- },
- body: {
- data: eventsMockData,
- meta: {
- fields: {
- 'error.type': 'array',
- count: 'integer',
- },
- },
- },
- });
- }
- beforeEach(function () {
- mockQuery = {
- conditions: 'title:/organizations/:orgId/performance/summary/',
- fields: ['error.type', 'count()'],
- aggregates: ['count()'],
- columns: ['error.type'],
- id: '1',
- name: 'Query Name',
- orderby: '',
- };
- mockWidget = {
- title: 'Test Widget',
- displayType: DisplayType.TOP_N,
- interval: '5m',
- queries: [mockQuery],
- widgetType: WidgetType.DISCOVER,
- };
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- match: [MockApiClient.matchQuery({cursor: '0:10:0'})],
- headers: {
- Link:
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:20:0>; rel="next"; results="true"; cursor="0:20:0"',
- },
- body: {
- data: [
- {
- 'error.type': ['Next Page Test Error'],
- count: 1,
- },
- ],
- meta: {
- 'error.type': 'array',
- count: 'integer',
- },
- },
- });
- });
- describe('with eventsv2', function () {
- it('renders Discover topn chart widget viewer', async function () {
- mockEventsStats();
- mockEventsv2();
- const {container} = await renderModal({initialData, widget: mockWidget});
- expect(container).toSnapshot();
- });
- it('sorts table when a sortable column header is clicked', async function () {
- const eventsStatsMock = mockEventsStats();
- const eventsv2Mock = mockEventsv2();
- const {rerender} = await renderModal({initialData, widget: mockWidget});
- userEvent.click(screen.getByText('count()'));
- expect(initialData.router.push).toHaveBeenCalledWith({
- query: {sort: ['-count()']},
- });
- // Need to manually set the new router location and rerender to simulate the sortable column click
- initialData.router.location.query = {sort: ['-count()']};
- rerender(
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={mockWidget}
- onEdit={() => undefined}
- />
- );
- await waitForMetaToHaveBeenCalled();
- expect(eventsv2Mock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({sort: ['-count()']}),
- })
- );
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({orderby: '-count()'}),
- })
- );
- });
- it('renders pagination buttons', async function () {
- mockEventsStats();
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByRole('button', {name: 'Previous'})).toBeInTheDocument();
- expect(screen.getByRole('button', {name: 'Next'})).toBeInTheDocument();
- });
- it('does not render pagination buttons', async function () {
- mockEventsStats();
- mockEventsv2();
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- headers: {
- Link:
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/eventsv2/?cursor=0:20:0>; rel="next"; results="false"; cursor="0:20:0"',
- },
- body: {
- data: [
- {
- 'error.type': ['No Pagination'],
- count: 1,
- },
- ],
- meta: {
- 'error.type': 'array',
- count: 'integer',
- },
- },
- });
- await renderModal({initialData, widget: mockWidget});
- expect(
- screen.queryByRole('button', {name: 'Previous'})
- ).not.toBeInTheDocument();
- expect(screen.queryByRole('button', {name: 'Next'})).not.toBeInTheDocument();
- });
- it('paginates to the next page', async function () {
- mockEventsStats();
- mockEventsv2();
- const {rerender} = await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('Test Error 1c')).toBeInTheDocument();
- userEvent.click(screen.getByRole('button', {name: 'Next'}));
- expect(initialData.router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {cursor: '0:10:0'},
- })
- );
- // Need to manually set the new router location and rerender to simulate the next page click
- initialData.router.location.query = {cursor: ['0:10:0']};
- rerender(
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={mockWidget}
- onEdit={() => undefined}
- />
- );
- await waitForMetaToHaveBeenCalled();
- expect(await screen.findByText('Next Page Test Error')).toBeInTheDocument();
- });
- it('uses provided seriesData and does not make an events-stats requests', async function () {
- const eventsStatsMock = mockEventsStats();
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget, seriesData: []});
- expect(eventsStatsMock).not.toHaveBeenCalled();
- });
- it('makes events-stats requests when table is sorted', async function () {
- const eventsStatsMock = mockEventsStats();
- mockEventsv2();
- await renderModal({
- initialData,
- widget: mockWidget,
- seriesData: [],
- });
- expect(eventsStatsMock).not.toHaveBeenCalled();
- userEvent.click(screen.getByText('count()'));
- await waitForMetaToHaveBeenCalled();
- expect(eventsStatsMock).toHaveBeenCalledTimes(1);
- });
- it('renders widget chart minimap', async function () {
- mockEventsStats();
- mockEventsv2();
- initialData.organization.features.push('widget-viewer-modal-minimap');
- await renderModal({initialData, widget: mockWidget});
- expect(ReactEchartsCore).toHaveBeenLastCalledWith(
- expect.objectContaining({
- option: expect.objectContaining({
- dataZoom: expect.arrayContaining([
- expect.objectContaining({
- realtime: false,
- showDetail: false,
- end: 100,
- start: 0,
- }),
- ]),
- }),
- }),
- {}
- );
- });
- it('zooming on minimap updates location query and updates echart start and end values', async function () {
- mockEventsStats();
- mockEventsv2();
- initialData.organization.features.push('widget-viewer-modal-minimap');
- await renderModal({initialData, widget: mockWidget});
- const calls = (ReactEchartsCore as jest.Mock).mock.calls;
- act(() => {
- // Simulate dataZoom event on chart
- calls[calls.length - 1][0].onEvents.datazoom(
- {seriesStart: 1646100000000, seriesEnd: 1646120000000},
- {
- getModel: () => {
- return {
- _payload: {start: 30, end: 70},
- };
- },
- }
- );
- });
- expect(initialData.router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- viewerEnd: '2022-03-01T05:53:20',
- viewerStart: '2022-03-01T03:40:00',
- },
- })
- );
- await waitFor(() => {
- expect(ReactEchartsCore).toHaveBeenLastCalledWith(
- expect.objectContaining({
- option: expect.objectContaining({
- dataZoom: expect.arrayContaining([
- expect.objectContaining({
- realtime: false,
- showDetail: false,
- endValue: 1646114000000,
- startValue: 1646106000000,
- }),
- ]),
- }),
- }),
- {}
- );
- });
- });
- });
- describe('with events', function () {
- it('sorts table when a sortable column header is clicked', async function () {
- const eventsStatsMock = mockEventsStats();
- const eventsMock = mockEvents();
- const {rerender} = await renderModal({
- initialData: initialDataWithFlag,
- widget: mockWidget,
- });
- userEvent.click(screen.getByText('count()'));
- expect(initialDataWithFlag.router.push).toHaveBeenCalledWith({
- query: {sort: ['-count()']},
- });
- // Need to manually set the new router location and rerender to simulate the sortable column click
- initialDataWithFlag.router.location.query = {sort: ['-count()']};
- rerender(
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialDataWithFlag.organization}
- widget={mockWidget}
- onEdit={() => undefined}
- />
- );
- await waitForMetaToHaveBeenCalled();
- expect(eventsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events/',
- expect.objectContaining({
- query: expect.objectContaining({sort: ['-count()']}),
- })
- );
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({orderby: '-count()'}),
- })
- );
- });
- });
- });
- describe('World Map Chart Widget', function () {
- let mockQuery, mockWidget;
- const eventsMockData = [
- {
- 'geo.country_code': 'ES',
- p75_measurements_lcp: 2000,
- },
- {
- 'geo.country_code': 'SK',
- p75_measurements_lcp: 3000,
- },
- {
- 'geo.country_code': 'CO',
- p75_measurements_lcp: 4000,
- },
- ];
- function mockEventsGeo() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-geo/',
- body: {
- data: eventsMockData,
- meta: {
- 'geo.country_code': 'string',
- p75_measurements_lcp: 'duration',
- },
- },
- });
- }
- function mockEventsv2() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- body: {
- data: eventsMockData,
- meta: {
- 'geo.country_code': 'string',
- p75_measurements_lcp: 'duration',
- },
- },
- });
- }
- function mockEvents() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- body: {
- data: eventsMockData,
- meta: {
- fields: {
- 'geo.country_code': 'string',
- p75_measurements_lcp: 'duration',
- },
- },
- },
- });
- }
- beforeEach(function () {
- mockQuery = {
- conditions: 'title:/organizations/:orgId/performance/summary/',
- fields: ['p75(measurements.lcp)'],
- aggregates: ['p75(measurements.lcp)'],
- columns: [],
- id: '1',
- name: 'Query Name',
- orderby: '',
- };
- mockWidget = {
- title: 'Test Widget',
- displayType: DisplayType.WORLD_MAP,
- interval: '5m',
- queries: [mockQuery],
- widgetType: WidgetType.DISCOVER,
- };
- });
- describe('with eventsv2', function () {
- it('always queries geo.country_code in the table chart', async function () {
- const eventsv2Mock = mockEventsv2();
- mockEventsGeo();
- await renderModal({initialData, widget: mockWidget});
- expect(eventsv2Mock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- field: ['geo.country_code', 'p75(measurements.lcp)'],
- }),
- })
- );
- expect(await screen.findByText('geo.country_code')).toBeInTheDocument();
- });
- it('renders Discover topn chart widget viewer', async function () {
- mockEventsv2();
- mockEventsGeo();
- const {container} = await renderModal({initialData, widget: mockWidget});
- expect(container).toSnapshot();
- });
- it('uses provided tableData and does not make an eventsv2 requests', async function () {
- const eventsGeoMock = mockEventsGeo();
- mockEventsv2();
- await renderModal({initialData, widget: mockWidget, tableData: []});
- expect(eventsGeoMock).not.toHaveBeenCalled();
- });
- });
- describe('with events', function () {
- it('always queries geo.country_code in the table chart', async function () {
- const eventsMock = mockEvents();
- mockEventsGeo();
- await renderModal({initialData: initialDataWithFlag, widget: mockWidget});
- expect(eventsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events/',
- expect.objectContaining({
- query: expect.objectContaining({
- field: ['geo.country_code', 'p75(measurements.lcp)'],
- }),
- })
- );
- expect(await screen.findByText('geo.country_code')).toBeInTheDocument();
- });
- });
- });
- describe('Table Widget', function () {
- const mockQuery = {
- conditions: 'title:/organizations/:orgId/performance/summary/',
- fields: ['title', 'count()'],
- aggregates: ['count()'],
- columns: ['title'],
- id: '1',
- name: 'Query Name',
- orderby: '',
- };
- const mockWidget = {
- title: 'Test Widget',
- displayType: DisplayType.TABLE,
- interval: '5m',
- queries: [mockQuery],
- widgetType: WidgetType.DISCOVER,
- };
- function mockEventsv2() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- body: {
- data: [
- {
- title: '/organizations/:orgId/dashboards/',
- id: '1',
- count: 1,
- },
- ],
- meta: {
- title: 'string',
- id: 'string',
- count: 1,
- isMetricsData: false,
- },
- },
- });
- }
- function mockEvents() {
- return MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- body: {
- data: [
- {
- title: '/organizations/:orgId/dashboards/',
- id: '1',
- count: 1,
- },
- ],
- meta: {
- fields: {
- title: 'string',
- id: 'string',
- count: 1,
- },
- isMetricsData: false,
- },
- },
- });
- }
- describe('with eventsv2', function () {
- it('makes eventsv2 requests when table is paginated', async function () {
- const eventsv2Mock = mockEventsv2();
- await renderModal({
- initialData,
- widget: mockWidget,
- tableData: [],
- pageLinks:
- '<https://sentry.io>; rel="previous"; results="false"; cursor="0:0:1", <https://sentry.io>; rel="next"; results="true"; cursor="0:20:0"',
- });
- expect(eventsv2Mock).not.toHaveBeenCalled();
- userEvent.click(screen.getByLabelText('Next'));
- await waitFor(() => {
- expect(eventsv2Mock).toHaveBeenCalled();
- });
- });
- });
- describe('with events', function () {
- it('makes events requests when table is paginated', async function () {
- const eventsMock = mockEvents();
- await renderModal({
- initialData: initialDataWithFlag,
- widget: mockWidget,
- tableData: [],
- pageLinks:
- '<https://sentry.io>; rel="previous"; results="false"; cursor="0:0:1", <https://sentry.io>; rel="next"; results="true"; cursor="0:20:0"',
- });
- expect(eventsMock).not.toHaveBeenCalled();
- userEvent.click(screen.getByLabelText('Next'));
- await waitFor(() => {
- expect(eventsMock).toHaveBeenCalled();
- });
- });
- it('disables the Open in Discover button for a custom measurement widget', async function () {
- const customMeasurementWidget = {
- ...mockWidget,
- queries: [
- {
- conditions: '',
- fields: [],
- aggregates: ['p99(measurements.custom.measurement)'],
- columns: ['title'],
- id: '1',
- name: 'Query Name',
- orderby: '',
- },
- ],
- };
- await renderModal({
- initialData: initialDataWithFlag,
- widget: customMeasurementWidget,
- tableData: [],
- pageLinks:
- '<https://sentry.io>; rel="previous"; results="false"; cursor="0:0:1", <https://sentry.io>; rel="next"; results="true"; cursor="0:20:0"',
- });
- userEvent.click(screen.getByText('Open in Discover'));
- expect(initialData.router.push).not.toHaveBeenCalled();
- });
- it('displays table data with units correctly', async function () {
- const eventsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- match: [MockApiClient.matchQuery({cursor: undefined})],
- headers: {
- Link:
- '<http://localhost/api/0/organizations/org-slug/events/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/events/?cursor=0:10:0>; rel="next"; results="true"; cursor="0:10:0"',
- },
- body: {
- data: [
- {
- 'p75(measurements.custom.minute)': 94.87035966318831,
- 'p95(measurements.custom.ratio)': 0.9881980140455187,
- 'p75(measurements.custom.kibibyte)': 217.87035966318834,
- },
- ],
- meta: {
- fields: {
- 'p75(measurements.custom.minute)': 'duration',
- 'p95(measurements.custom.ratio)': 'percentage',
- 'p75(measurements.custom.kibibyte)': 'size',
- },
- units: {
- 'p75(measurements.custom.minute)': 'minute',
- 'p95(measurements.custom.ratio)': null,
- 'p75(measurements.custom.kibibyte)': 'kibibyte',
- },
- isMetricsData: true,
- tips: {},
- },
- },
- });
- await renderModal({
- initialData: initialDataWithFlag,
- widget: {
- title: 'Custom Widget',
- displayType: 'table',
- queries: [
- {
- fields: [
- 'p75(measurements.custom.kibibyte)',
- 'p75(measurements.custom.minute)',
- 'p95(measurements.custom.ratio)',
- ],
- aggregates: [
- 'p75(measurements.custom.kibibyte)',
- 'p75(measurements.custom.minute)',
- 'p95(measurements.custom.ratio)',
- ],
- columns: [],
- orderby: '-p75(measurements.custom.kibibyte)',
- },
- ],
- widgetType: 'discover',
- },
- });
- await waitFor(() => {
- expect(eventsMock).toHaveBeenCalled();
- });
- expect(screen.getByText('217.9 KiB')).toBeInTheDocument();
- expect(screen.getByText('1.58hr')).toBeInTheDocument();
- expect(screen.getByText('98.82%')).toBeInTheDocument();
- });
- });
- });
- });
- describe('Issue Table Widget', function () {
- let issuesMock;
- const mockQuery = {
- conditions: 'is:unresolved',
- fields: ['events', 'status', 'title'],
- columns: ['events', 'status', 'title'],
- aggregates: [],
- id: '1',
- name: 'Query Name',
- orderby: '',
- };
- const mockWidget = {
- id: '1',
- title: 'Issue Widget',
- displayType: DisplayType.TABLE,
- interval: '5m',
- queries: [mockQuery],
- widgetType: WidgetType.ISSUE,
- };
- beforeEach(function () {
- MemberListStore.loadInitialData([]);
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/issues/',
- method: 'GET',
- match: [
- MockApiClient.matchData({
- cursor: '0:10:0',
- }),
- ],
- headers: {
- Link:
- '<http://localhost/api/0/organizations/org-slug/issues/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/issues/?cursor=0:20:0>; rel="next"; results="true"; cursor="0:20:0"',
- },
- body: [
- {
- id: '2',
- title: 'Another Error: Failed',
- project: {
- id: '3',
- },
- status: 'unresolved',
- lifetime: {count: 5},
- count: 3,
- userCount: 1,
- },
- ],
- });
- issuesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/issues/',
- method: 'GET',
- match: [
- MockApiClient.matchData({
- cursor: undefined,
- }),
- ],
- headers: {
- Link:
- '<http://localhost/api/0/organizations/org-slug/issues/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/issues/?cursor=0:10:0>; rel="next"; results="true"; cursor="0:10:0"',
- },
- body: [
- {
- id: '1',
- title: 'Error: Failed',
- project: {
- id: '3',
- },
- status: 'unresolved',
- lifetime: {count: 10},
- count: 6,
- userCount: 3,
- },
- ],
- });
- });
- it('renders widget title', async function () {
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('Issue Widget')).toBeInTheDocument();
- });
- it('renders Edit and Open buttons', async function () {
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('Edit Widget')).toBeInTheDocument();
- expect(screen.getByText('Open in Issues')).toBeInTheDocument();
- });
- it('renders events, status, and title table columns', async function () {
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('title')).toBeInTheDocument();
- expect(await screen.findByText('Error: Failed')).toBeInTheDocument();
- expect(screen.getByText('events')).toBeInTheDocument();
- expect(screen.getByText('6')).toBeInTheDocument();
- expect(screen.getByText('status')).toBeInTheDocument();
- expect(screen.getByText('unresolved')).toBeInTheDocument();
- });
- it('renders Issue table widget viewer', async function () {
- const {container} = await renderModal({initialData, widget: mockWidget});
- await screen.findByText('Error: Failed');
- expect(container).toSnapshot();
- });
- it('redirects user to Issues when clicking Open in Issues', async function () {
- await renderModal({initialData, widget: mockWidget});
- userEvent.click(screen.getByText('Open in Issues'));
- expect(initialData.router.push).toHaveBeenCalledWith(
- '/organizations/org-slug/issues/?environment=prod&environment=dev&project=1&project=2&query=is%3Aunresolved&sort=&statsPeriod=24h'
- );
- });
- it('sorts table when a sortable column header is clicked', async function () {
- const {rerender} = await renderModal({initialData, widget: mockWidget});
- userEvent.click(screen.getByText('events'));
- expect(initialData.router.push).toHaveBeenCalledWith({
- query: {sort: 'freq'},
- });
- // Need to manually set the new router location and rerender to simulate the sortable column click
- initialData.router.location.query = {sort: ['freq']};
- rerender(
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={mockWidget}
- onEdit={() => undefined}
- />
- );
- expect(issuesMock).toHaveBeenCalledWith(
- '/organizations/org-slug/issues/',
- expect.objectContaining({
- data: {
- cursor: undefined,
- environment: ['prod', 'dev'],
- expand: ['owners'],
- limit: 20,
- project: [1, 2],
- query: 'is:unresolved',
- sort: 'date',
- statsPeriod: '24h',
- },
- })
- );
- });
- it('renders pagination buttons', async function () {
- await renderModal({initialData, widget: mockWidget});
- expect(await screen.findByRole('button', {name: 'Previous'})).toBeInTheDocument();
- expect(screen.getByRole('button', {name: 'Next'})).toBeInTheDocument();
- });
- it('paginates to the next page', async function () {
- const {rerender} = await renderModal({initialData, widget: mockWidget});
- expect(await screen.findByText('Error: Failed')).toBeInTheDocument();
- userEvent.click(screen.getByRole('button', {name: 'Next'}));
- expect(issuesMock).toHaveBeenCalledTimes(1);
- expect(initialData.router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {cursor: '0:10:0', page: 1},
- })
- );
- // Need to manually set the new router location and rerender to simulate the next page click
- initialData.router.location.query = {cursor: ['0:10:0']};
- rerender(
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={mockWidget}
- onEdit={() => undefined}
- />
- );
- expect(await screen.findByText('Another Error: Failed')).toBeInTheDocument();
- });
- it('displays with correct table column widths', async function () {
- initialData.router.location.query = {width: ['-1', '-1', '575']};
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByTestId('grid-editable')).toHaveStyle({
- 'grid-template-columns':
- ' minmax(90px, auto) minmax(90px, auto) minmax(575px, auto)',
- });
- });
- it('uses provided tableData and does not make an issues requests', async function () {
- await renderModal({initialData, widget: mockWidget, tableData: []});
- expect(issuesMock).not.toHaveBeenCalled();
- });
- it('makes issues requests when table is sorted', async function () {
- await renderModal({
- initialData,
- widget: mockWidget,
- tableData: [],
- });
- expect(issuesMock).not.toHaveBeenCalled();
- userEvent.click(screen.getByText('events'));
- await waitFor(() => {
- expect(issuesMock).toHaveBeenCalled();
- });
- });
- });
- describe('Release Health Widgets', function () {
- let metricsMock;
- const mockQuery = {
- conditions: '',
- fields: [`sum(session)`],
- columns: [],
- aggregates: [],
- id: '1',
- name: 'Query Name',
- orderby: '',
- };
- const mockWidget = {
- id: '1',
- title: 'Release Widget',
- displayType: DisplayType.LINE,
- interval: '5m',
- queries: [mockQuery],
- widgetType: WidgetType.RELEASE,
- };
- beforeEach(function () {
- metricsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/metrics/data/',
- body: TestStubs.MetricsTotalCountByReleaseIn24h(),
- headers: {
- link:
- '<http://localhost/api/0/organizations/org-slug/metrics/data/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",' +
- '<http://localhost/api/0/organizations/org-slug/metrics/data/?cursor=0:10:0>; rel="next"; results="true"; cursor="0:10:0"',
- },
- });
- });
- it('does a sessions query', async function () {
- jest.useFakeTimers().setSystemTime(new Date('2022-08-02'));
- await renderModal({initialData, widget: mockWidget});
- expect(metricsMock).toHaveBeenCalled();
- });
- it('renders widget title', async function () {
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('Release Widget')).toBeInTheDocument();
- });
- it('renders Edit and Open in Releases buttons', async function () {
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('Edit Widget')).toBeInTheDocument();
- expect(screen.getByText('Open in Releases')).toBeInTheDocument();
- });
- it('Open in Releases button redirects browser', async function () {
- await renderModal({initialData, widget: mockWidget});
- userEvent.click(screen.getByText('Open in Releases'));
- expect(initialData.router.push).toHaveBeenCalledWith(
- '/organizations/org-slug/releases/?environment=prod&environment=dev&project=1&project=2&statsPeriod=24h'
- );
- });
- it('renders table header and body', async function () {
- await renderModal({initialData, widget: mockWidget});
- expect(screen.getByText('release')).toBeInTheDocument();
- expect(await screen.findByText('e102abb2c46e')).toBeInTheDocument();
- expect(screen.getByText('sum(session)')).toBeInTheDocument();
- expect(screen.getByText('6.3k')).toBeInTheDocument();
- });
- it('renders Release widget viewer', async function () {
- const {container} = await renderModal({initialData, widget: mockWidget});
- expect(await screen.findByText('e102abb2c46e')).toBeInTheDocument();
- expect(container).toSnapshot();
- });
- it('renders pagination buttons', async function () {
- await renderModal({
- initialData,
- widget: mockWidget,
- });
- expect(await screen.findByRole('button', {name: 'Previous'})).toBeInTheDocument();
- expect(screen.getByRole('button', {name: 'Next'})).toBeInTheDocument();
- });
- it('does not render pagination buttons when sorting by release', async function () {
- await renderModal({
- initialData,
- widget: {...mockWidget, queries: [{...mockQuery, orderby: 'release'}]},
- });
- expect(screen.queryByRole('button', {name: 'Previous'})).not.toBeInTheDocument();
- expect(screen.queryByRole('button', {name: 'Next'})).not.toBeInTheDocument();
- });
- it('makes a new sessions request after sorting by a table column', async function () {
- const {rerender} = await renderModal({
- initialData,
- widget: mockWidget,
- tableData: [],
- seriesData: [],
- });
- expect(metricsMock).toHaveBeenCalledTimes(1);
- userEvent.click(screen.getByText(`sum(session)`));
- expect(initialData.router.push).toHaveBeenCalledWith({
- query: {sort: '-sum(session)'},
- });
- // Need to manually set the new router location and rerender to simulate the sortable column click
- initialData.router.location.query = {sort: '-sum(session)'};
- rerender(
- <WidgetViewerModal
- Header={stubEl}
- Footer={stubEl as ModalRenderProps['Footer']}
- Body={stubEl as ModalRenderProps['Body']}
- CloseButton={stubEl}
- closeModal={() => undefined}
- organization={initialData.organization}
- widget={mockWidget}
- onEdit={() => undefined}
- seriesData={[]}
- tableData={[]}
- />
- );
- await waitFor(() => {
- expect(metricsMock).toHaveBeenCalledTimes(2);
- });
- });
- });
- });
|