123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- import React from 'react';
- import {mountWithTheme} from 'sentry-test/enzyme';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {Client} from 'app/api';
- import WidgetQueries from 'app/views/dashboardsV2/widgetQueries';
- describe('Dashboards > WidgetQueries', function () {
- const initialData = initializeOrg({
- organization: TestStubs.Organization(),
- });
- const multipleQueryWidget = {
- title: 'Errors',
- interval: '5m',
- displayType: 'line',
- queries: [
- {conditions: 'event.type:error', fields: ['count()'], name: 'errors'},
- {conditions: 'event.type:default', fields: ['count()'], name: 'default'},
- ],
- };
- const singleQueryWidget = {
- title: 'Errors',
- interval: '5m',
- displayType: 'line',
- queries: [{conditions: 'event.type:error', fields: ['count()'], name: 'errors'}],
- };
- const tableWidget = {
- title: 'SDK',
- interval: '5m',
- displayType: 'table',
- queries: [{conditions: 'event.type:error', fields: ['sdk.name'], name: 'sdk'}],
- };
- const selection = {
- projects: [1],
- environments: ['prod'],
- datetime: {
- period: '14d',
- },
- };
- const api = new Client();
- afterEach(function () {
- MockApiClient.clearMockResponses();
- });
- it('can send multiple API requests', async function () {
- const errorMock = MockApiClient.addMockResponse(
- {
- url: '/organizations/org-slug/events-stats/',
- body: [],
- },
- {
- predicate(_url, options) {
- return (
- options.query.query === 'event.type:error' ||
- options.query.query === 'event.type:default'
- );
- },
- }
- );
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={multipleQueryWidget}
- organization={initialData.organization}
- selection={selection}
- >
- {() => <div data-test-id="child" />}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 2 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(errorMock).toHaveBeenCalledTimes(2);
- });
- it('sets errorMessage when the first request fails', async function () {
- const okMock = MockApiClient.addMockResponse(
- {
- url: '/organizations/org-slug/events-stats/',
- body: [],
- },
- {
- predicate(_url, options) {
- return options.query.query === 'event.type:error';
- },
- }
- );
- const failMock = MockApiClient.addMockResponse(
- {
- url: '/organizations/org-slug/events-stats/',
- statusCode: 400,
- body: {detail: 'Bad request data'},
- },
- {
- predicate(_url, options) {
- return options.query.query === 'event.type:default';
- },
- }
- );
- let error = '';
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={multipleQueryWidget}
- organization={initialData.organization}
- selection={selection}
- >
- {({errorMessage}) => {
- error = errorMessage;
- return <div data-test-id="child" />;
- }}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 2 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(okMock).toHaveBeenCalledTimes(1);
- expect(failMock).toHaveBeenCalledTimes(1);
- expect(error).toEqual('Bad request data');
- });
- it('adjusts interval based on date window', async function () {
- const errorMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: [],
- });
- const widget = {...singleQueryWidget, interval: '1m'};
- const longSelection = {
- projects: [1],
- environments: ['prod', 'dev'],
- datetime: {
- period: '90d',
- },
- };
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={widget}
- organization={initialData.organization}
- selection={longSelection}
- >
- {() => <div data-test-id="child" />}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- // Child should be rendered and interval bumped up.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(errorMock).toHaveBeenCalledTimes(1);
- expect(errorMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- interval: '4h',
- statsPeriod: '90d',
- environment: ['prod', 'dev'],
- project: [1],
- }),
- })
- );
- });
- it('adjusts interval based on date window 14d', async function () {
- const errorMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: [],
- });
- const widget = {...singleQueryWidget, interval: '1m'};
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={widget}
- organization={initialData.organization}
- selection={selection}
- >
- {() => <div data-test-id="child" />}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- // Child should be rendered and interval bumped up.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(errorMock).toHaveBeenCalledTimes(1);
- expect(errorMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({interval: '30m'}),
- })
- );
- });
- it('can send table result queries', async function () {
- const tableMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- body: {
- meta: {'sdk.name': 'string'},
- data: [{'sdk.name': 'python'}],
- },
- });
- let childProps = undefined;
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={tableWidget}
- organization={initialData.organization}
- selection={selection}
- >
- {props => {
- childProps = props;
- return <div data-test-id="child" />;
- }}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 1 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(tableMock).toHaveBeenCalledTimes(1);
- expect(tableMock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- query: 'event.type:error',
- name: 'SDK',
- field: ['sdk.name'],
- statsPeriod: '14d',
- environment: ['prod'],
- project: [1],
- }),
- })
- );
- expect(childProps.timeseriesResults).toBeUndefined();
- expect(childProps.tableResults[0].data).toHaveLength(1);
- expect(childProps.tableResults[0].meta).toBeDefined();
- });
- it('can send multiple table queries', async function () {
- const firstQuery = MockApiClient.addMockResponse(
- {
- url: '/organizations/org-slug/eventsv2/',
- body: {
- meta: {'sdk.name': 'string'},
- data: [{'sdk.name': 'python'}],
- },
- },
- {
- predicate(_url, options) {
- return options.query.query === 'event.type:error';
- },
- }
- );
- const secondQuery = MockApiClient.addMockResponse(
- {
- url: '/organizations/org-slug/eventsv2/',
- body: {
- meta: {title: 'string'},
- data: [{title: 'ValueError'}],
- },
- },
- {
- predicate(_url, options) {
- return options.query.query === 'title:ValueError';
- },
- }
- );
- const widget = {
- title: 'SDK',
- interval: '5m',
- displayType: 'table',
- queries: [
- {conditions: 'event.type:error', fields: ['sdk.name'], name: 'sdk'},
- {conditions: 'title:ValueError', fields: ['title'], name: 'title'},
- ],
- };
- let childProps = undefined;
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={widget}
- organization={initialData.organization}
- selection={selection}
- >
- {props => {
- childProps = props;
- return <div data-test-id="child" />;
- }}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 2 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(firstQuery).toHaveBeenCalledTimes(1);
- expect(secondQuery).toHaveBeenCalledTimes(1);
- expect(childProps.tableResults).toHaveLength(2);
- expect(childProps.tableResults[0].data[0]['sdk.name']).toBeDefined();
- expect(childProps.tableResults[1].data[0].title).toBeDefined();
- });
- it('can send big number result queries', async function () {
- const tableMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- body: {
- meta: {'sdk.name': 'string'},
- data: [{'sdk.name': 'python'}],
- },
- });
- let childProps = undefined;
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={{
- title: 'SDK',
- interval: '5m',
- displayType: 'big_number',
- queries: [{conditions: 'event.type:error', fields: ['sdk.name'], name: 'sdk'}],
- }}
- organization={initialData.organization}
- selection={selection}
- >
- {props => {
- childProps = props;
- return <div data-test-id="child" />;
- }}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 1 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(tableMock).toHaveBeenCalledTimes(1);
- expect(tableMock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- referrer: 'api.dashboards.bignumberwidget',
- query: 'event.type:error',
- name: 'SDK',
- field: ['sdk.name'],
- statsPeriod: '14d',
- environment: ['prod'],
- project: [1],
- }),
- })
- );
- expect(childProps.timeseriesResults).toBeUndefined();
- expect(childProps.tableResults[0].data).toHaveLength(1);
- expect(childProps.tableResults[0].meta).toBeDefined();
- });
- it('can send world map result queries', async function () {
- const tableMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-geo/',
- body: {
- meta: {'sdk.name': 'string'},
- data: [{'sdk.name': 'python'}],
- },
- });
- let childProps = undefined;
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={{
- title: 'SDK',
- interval: '5m',
- displayType: 'world_map',
- queries: [{conditions: 'event.type:error', fields: ['count()'], name: 'sdk'}],
- }}
- organization={initialData.organization}
- selection={selection}
- >
- {props => {
- childProps = props;
- return <div data-test-id="child" />;
- }}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 1 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(tableMock).toHaveBeenCalledTimes(1);
- expect(tableMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-geo/',
- expect.objectContaining({
- query: expect.objectContaining({
- referrer: 'api.dashboards.worldmapwidget',
- query: 'event.type:error',
- name: 'SDK',
- field: ['count()'],
- statsPeriod: '14d',
- environment: ['prod'],
- project: [1],
- }),
- })
- );
- expect(childProps.timeseriesResults).toBeUndefined();
- expect(childProps.tableResults[0].data).toHaveLength(1);
- expect(childProps.tableResults[0].meta).toBeDefined();
- });
- it('stops loading state once all queries finish even if some fail', async function () {
- const firstQuery = MockApiClient.addMockResponse(
- {
- statusCode: 500,
- url: '/organizations/org-slug/eventsv2/',
- body: {detail: 'it didnt work'},
- },
- {
- predicate(_url, options) {
- return options.query.query === 'event.type:error';
- },
- }
- );
- const secondQuery = MockApiClient.addMockResponse(
- {
- url: '/organizations/org-slug/eventsv2/',
- body: {
- meta: {title: 'string'},
- data: [{title: 'ValueError'}],
- },
- },
- {
- predicate(_url, options) {
- return options.query.query === 'title:ValueError';
- },
- }
- );
- const widget = {
- title: 'SDK',
- interval: '5m',
- displayType: 'table',
- queries: [
- {conditions: 'event.type:error', fields: ['sdk.name'], name: 'sdk'},
- {conditions: 'title:ValueError', fields: ['title'], name: 'title'},
- ],
- };
- let childProps = undefined;
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={widget}
- organization={initialData.organization}
- selection={selection}
- >
- {props => {
- childProps = props;
- return <div data-test-id="child" />;
- }}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 2 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(firstQuery).toHaveBeenCalledTimes(1);
- expect(secondQuery).toHaveBeenCalledTimes(1);
- expect(childProps.loading).toEqual(false);
- });
- it('sets bar charts to 1d interval', async function () {
- const errorMock = MockApiClient.addMockResponse(
- {
- url: '/organizations/org-slug/events-stats/',
- body: [],
- },
- {
- predicate(_url, options) {
- return options.query.interval === '1d';
- },
- }
- );
- const barWidget = {
- ...singleQueryWidget,
- displayType: 'bar',
- // Should be ignored for bars.
- interval: '5m',
- };
- const wrapper = mountWithTheme(
- <WidgetQueries
- api={api}
- widget={barWidget}
- organization={initialData.organization}
- selection={selection}
- >
- {() => <div data-test-id="child" />}
- </WidgetQueries>,
- initialData.routerContext
- );
- await tick();
- await tick();
- // Child should be rendered and 1 requests should be sent.
- expect(wrapper.find('[data-test-id="child"]')).toHaveLength(1);
- expect(errorMock).toHaveBeenCalledTimes(1);
- });
- });
|