123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- import {mountWithTheme} from 'sentry-test/enzyme';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {getOptionByLabel, selectByLabel} from 'sentry-test/select-new';
- import AddDashboardWidgetModal from 'app/components/modals/addDashboardWidgetModal';
- import TagStore from 'app/stores/tagStore';
- const stubEl = props => <div>{props.children}</div>;
- function mountModal({initialData, onAddWidget, onUpdateWidget, widget}) {
- return mountWithTheme(
- <AddDashboardWidgetModal
- Header={stubEl}
- Footer={stubEl}
- Body={stubEl}
- organization={initialData.organization}
- onAddWidget={onAddWidget}
- onUpdateWidget={onUpdateWidget}
- widget={widget}
- closeModal={() => void 0}
- />,
- initialData.routerContext
- );
- }
- async function clickSubmit(wrapper) {
- // Click on submit.
- const button = wrapper.find('Button[data-test-id="add-widget"] button');
- button.simulate('click');
- // Wait for xhr to complete.
- return tick();
- }
- function getDisplayType(wrapper) {
- return wrapper.find('input[name="displayType"]');
- }
- async function setSearchConditions(el, query) {
- el.find('textarea')
- .simulate('change', {target: {value: query}})
- .getDOMNode()
- .setSelectionRange(query.length, query.length);
- await tick();
- await el.update();
- el.find('textarea').simulate('keydown', {key: 'Enter'});
- }
- describe('Modals -> AddDashboardWidgetModal', function () {
- const initialData = initializeOrg({
- organization: {
- features: ['performance-view', 'discover-query'],
- apdexThreshold: 400,
- },
- });
- const tags = [
- {name: 'browser.name', key: 'browser.name'},
- {name: 'custom-field', key: 'custom-field'},
- ];
- let eventsStatsMock;
- beforeEach(function () {
- TagStore.onLoadTagsSuccess(tags);
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/widgets/',
- method: 'POST',
- statusCode: 200,
- body: [],
- });
- eventsStatsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- body: {data: [{'event.type': 'error'}], meta: {'event.type': 'string'}},
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-geo/',
- body: {data: [], meta: {}},
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/recent-searches/',
- body: [],
- });
- });
- afterEach(() => {
- MockApiClient.clearMockResponses();
- });
- it('can update the title', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- const input = wrapper.find('Input[name="title"] input');
- input.simulate('change', {target: {value: 'Unique Users'}});
- await clickSubmit(wrapper);
- expect(widget.title).toEqual('Unique Users');
- wrapper.unmount();
- });
- it('can add conditions', async function () {
- jest.useFakeTimers();
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // Change the search text on the first query.
- const input = wrapper.find('#smart-search-input').first();
- input.simulate('change', {target: {value: 'color:blue'}}).simulate('blur');
- jest.runAllTimers();
- jest.useRealTimers();
- await clickSubmit(wrapper);
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].conditions).toEqual('color:blue');
- wrapper.unmount();
- });
- it('can choose a field', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // No delete button as there is only one field.
- expect(wrapper.find('IconDelete')).toHaveLength(0);
- selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 0, control: true});
- await clickSubmit(wrapper);
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['p95(transaction.duration)']);
- wrapper.unmount();
- });
- it('can add additional fields', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // Click the add button
- const add = wrapper.find('button[aria-label="Add Overlay"]');
- add.simulate('click');
- wrapper.update();
- // Should be another field input.
- expect(wrapper.find('QueryField')).toHaveLength(2);
- selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 1, control: true});
- await clickSubmit(wrapper);
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['count()', 'p95(transaction.duration)']);
- wrapper.unmount();
- });
- it('can add and delete additional queries', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/event.type/values/',
- body: [{count: 2, name: 'Nvidia 1080ti'}],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/recent-searches/',
- method: 'POST',
- body: [],
- });
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // Set first query search conditions
- await setSearchConditions(
- wrapper.find('SearchConditionsWrapper StyledSearchBar'),
- 'event.type:transaction'
- );
- // Set first query legend alias
- wrapper
- .find('SearchConditionsWrapper input[placeholder="Legend Alias"]')
- .simulate('change', {target: {value: 'Transactions'}});
- // Click the "Add Query" button twice
- const addQuery = wrapper.find('button[aria-label="Add Query"]');
- addQuery.simulate('click');
- wrapper.update();
- addQuery.simulate('click');
- wrapper.update();
- // Expect three search bars
- expect(wrapper.find('StyledSearchBar')).toHaveLength(3);
- // Expect "Add Query" button to be hidden since we're limited to at most 3 search conditions
- expect(wrapper.find('button[aria-label="Add Query"]')).toHaveLength(0);
- // Delete second query
- expect(wrapper.find('button[aria-label="Remove query"]')).toHaveLength(3);
- wrapper.find('button[aria-label="Remove query"]').at(1).simulate('click');
- wrapper.update();
- // Expect "Add Query" button to be shown again
- expect(wrapper.find('button[aria-label="Add Query"]')).toHaveLength(1);
- // Set second query search conditions
- const secondSearchBar = wrapper.find('SearchConditionsWrapper StyledSearchBar').at(1);
- await setSearchConditions(secondSearchBar, 'event.type:error');
- // Set second query legend alias
- wrapper
- .find('SearchConditionsWrapper input[placeholder="Legend Alias"]')
- .at(1)
- .simulate('change', {target: {value: 'Errors'}});
- // Save widget
- await clickSubmit(wrapper);
- expect(widget.queries).toHaveLength(2);
- expect(widget.queries[0]).toMatchObject({
- name: 'Transactions',
- conditions: 'event.type:transaction',
- fields: ['count()'],
- });
- expect(widget.queries[1]).toMatchObject({
- name: 'Errors',
- conditions: 'event.type:error',
- fields: ['count()'],
- });
- wrapper.unmount();
- });
- it('can respond to validation feedback', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/widgets/',
- method: 'POST',
- statusCode: 400,
- body: {
- title: ['This field is required'],
- queries: [{conditions: ['Invalid value']}],
- },
- });
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- await clickSubmit(wrapper);
- await wrapper.update();
- // API request should fail and not add widget.
- expect(widget).toBeUndefined();
- const errors = wrapper.find('FieldErrorReason');
- expect(errors).toHaveLength(2);
- // Nested object error should display
- const conditionError = wrapper.find('WidgetQueriesForm FieldErrorReason');
- expect(conditionError).toHaveLength(1);
- wrapper.unmount();
- });
- it('can edit a widget', async function () {
- let widget = {
- id: '9',
- title: 'Errors over time',
- interval: '5m',
- displayType: 'line',
- queries: [
- {
- id: '9',
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()', 'count_unique(id)'],
- },
- {
- id: '9',
- name: 'csp',
- conditions: 'event.type:csp',
- fields: ['count()', 'count_unique(id)'],
- },
- ],
- };
- const onAdd = jest.fn();
- const wrapper = mountModal({
- initialData,
- widget,
- onAddWidget: onAdd,
- onUpdateWidget: data => {
- widget = data;
- },
- });
- // Should be in edit 'mode'
- const heading = wrapper.find('h4');
- expect(heading.text()).toContain('Edit');
- // Should set widget data up.
- const title = wrapper.find('Input[name="title"]');
- expect(title.props().value).toEqual(widget.title);
- expect(wrapper.find('input[name="displayType"]').props().value).toEqual(
- widget.displayType
- );
- expect(wrapper.find('WidgetQueriesForm')).toHaveLength(1);
- expect(wrapper.find('StyledSearchBar')).toHaveLength(2);
- expect(wrapper.find('QueryField')).toHaveLength(2);
- // Expect events-stats endpoint to be called for each search conditions with
- // the same y-axis parameters
- expect(eventsStatsMock).toHaveBeenNthCalledWith(
- 1,
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- query: 'event.type:error',
- yAxis: ['count()', 'count_unique(id)'],
- }),
- })
- );
- expect(eventsStatsMock).toHaveBeenNthCalledWith(
- 2,
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- query: 'event.type:csp',
- yAxis: ['count()', 'count_unique(id)'],
- }),
- })
- );
- title.simulate('change', {target: {value: 'New title'}});
- await clickSubmit(wrapper);
- expect(onAdd).not.toHaveBeenCalled();
- expect(widget.title).toEqual('New title');
- expect(eventsStatsMock).toHaveBeenCalledTimes(2);
- wrapper.unmount();
- });
- it('renders column inputs for table widgets', async function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- method: 'GET',
- statusCode: 200,
- body: {
- meta: {},
- data: [],
- },
- });
- let widget = {
- id: '9',
- title: 'sdk usage',
- interval: '5m',
- displayType: 'table',
- queries: [
- {
- id: '9',
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['sdk.name', 'count()'],
- },
- ],
- };
- const wrapper = mountModal({
- initialData,
- widget,
- onAddWidget: jest.fn(),
- onUpdateWidget: data => {
- widget = data;
- },
- });
- // Should be in edit 'mode'
- const heading = wrapper.find('h4').first();
- expect(heading.text()).toContain('Edit');
- // Should set widget data up.
- const title = wrapper.find('Input[name="title"]');
- expect(title.props().value).toEqual(widget.title);
- expect(wrapper.find('input[name="displayType"]').props().value).toEqual(
- widget.displayType
- );
- expect(wrapper.find('WidgetQueriesForm')).toHaveLength(1);
- // Should have an orderby select
- expect(wrapper.find('WidgetQueriesForm SelectControl[name="orderby"]')).toHaveLength(
- 1
- );
- // Add a column, and choose a value,
- wrapper.find('button[aria-label="Add a Column"]').simulate('click');
- await wrapper.update();
- selectByLabel(wrapper, 'trace', {name: 'field', at: 2, control: true});
- await wrapper.update();
- await clickSubmit(wrapper);
- // A new field should be added.
- expect(widget.queries[0].fields).toHaveLength(3);
- expect(widget.queries[0].fields[2]).toEqual('trace');
- wrapper.unmount();
- });
- it('uses count() columns if there are no aggregate fields remaining when switching from table to chart', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // No delete button as there is only one field.
- expect(wrapper.find('IconDelete')).toHaveLength(0);
- // Select Table display
- selectByLabel(wrapper, 'Table', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('table');
- // Add field column
- selectByLabel(wrapper, 'event.type', {name: 'field', at: 0, control: true});
- let fieldColumn = wrapper.find('input[name="field"]');
- expect(fieldColumn.props().value).toEqual({
- kind: 'field',
- meta: {dataType: 'string', name: 'event.type'},
- });
- // Select Line chart display
- selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('line');
- // Expect event.type field to be converted to count()
- fieldColumn = wrapper.find('input[name="field"]');
- expect(fieldColumn.props().value).toEqual({
- kind: 'function',
- meta: {name: 'count', parameters: []},
- });
- await clickSubmit(wrapper);
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['count()']);
- wrapper.unmount();
- });
- it('should filter out non-aggregate fields when switching from table to chart', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // No delete button as there is only one field.
- expect(wrapper.find('IconDelete')).toHaveLength(0);
- // Select Table display
- selectByLabel(wrapper, 'Table', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('table');
- // Click the add button
- const add = wrapper.find('button[aria-label="Add a Column"]');
- add.simulate('click');
- wrapper.update();
- // Add columns
- selectByLabel(wrapper, 'event.type', {name: 'field', at: 0, control: true});
- let fieldColumn = wrapper.find('input[name="field"]').at(0);
- expect(fieldColumn.props().value).toEqual({
- kind: 'field',
- meta: {dataType: 'string', name: 'event.type'},
- });
- selectByLabel(wrapper, 'p95(\u2026)', {name: 'field', at: 1, control: true});
- fieldColumn = wrapper.find('input[name="field"]').at(1);
- expect(fieldColumn.props().value).toMatchObject({
- kind: 'function',
- meta: {
- name: 'p95',
- parameters: [{defaultValue: 'transaction.duration', kind: 'column'}],
- },
- });
- // Select Line chart display
- selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('line');
- // Expect event.type field to be converted to count()
- fieldColumn = wrapper.find('input[name="field"]');
- expect(fieldColumn.length).toEqual(1);
- expect(fieldColumn.props().value).toMatchObject({
- kind: 'function',
- meta: {
- name: 'p95',
- parameters: [{defaultValue: 'transaction.duration', kind: 'column'}],
- },
- });
- await clickSubmit(wrapper);
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['p95(transaction.duration)']);
- wrapper.unmount();
- });
- it('should filter non-legal y-axis choices for timeseries widget charts', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // No delete button as there is only one field.
- expect(wrapper.find('IconDelete')).toHaveLength(0);
- selectByLabel(wrapper, 'any(\u2026)', {
- name: 'field',
- at: 0,
- control: true,
- });
- // Expect user.display to not be an available parameter option for any()
- // for line (timeseries) widget charts
- const option = getOptionByLabel(wrapper, 'user.display', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- expect(option.exists()).toEqual(false);
- // Be able to choose a numeric-like option for any()
- selectByLabel(wrapper, 'measurements.lcp', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- await clickSubmit(wrapper);
- expect(widget.displayType).toEqual('line');
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['any(measurements.lcp)']);
- wrapper.unmount();
- });
- it('should not filter y-axis choices for big number widget charts', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // No delete button as there is only one field.
- expect(wrapper.find('IconDelete')).toHaveLength(0);
- // Select Big number display
- selectByLabel(wrapper, 'Big Number', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('big_number');
- selectByLabel(wrapper, 'count_unique(\u2026)', {
- name: 'field',
- at: 0,
- control: true,
- });
- // Be able to choose a non numeric-like option for count_unique()
- selectByLabel(wrapper, 'user.display', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- await clickSubmit(wrapper);
- expect(widget.displayType).toEqual('big_number');
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['count_unique(user.display)']);
- wrapper.unmount();
- });
- it('should filter y-axis choices for world map widget charts', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // No delete button as there is only one field.
- expect(wrapper.find('IconDelete')).toHaveLength(0);
- // Select World Map display
- selectByLabel(wrapper, 'World Map', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('world_map');
- // Choose any()
- selectByLabel(wrapper, 'any(\u2026)', {
- name: 'field',
- at: 0,
- control: true,
- });
- // user.display should be filtered out for any()
- const option = getOptionByLabel(wrapper, 'user.display', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- expect(option.exists()).toEqual(false);
- selectByLabel(wrapper, 'measurements.lcp', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- // Choose count_unique()
- selectByLabel(wrapper, 'count_unique(\u2026)', {
- name: 'field',
- at: 0,
- control: true,
- });
- // user.display not should be filtered out for count_unique()
- selectByLabel(wrapper, 'user.display', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- // Be able to choose a numeric-like option
- selectByLabel(wrapper, 'measurements.lcp', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- await clickSubmit(wrapper);
- expect(widget.displayType).toEqual('world_map');
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['count_unique(measurements.lcp)']);
- wrapper.unmount();
- });
- it('should filter y-axis choices by output type when switching from big number to line chart', async function () {
- let widget = undefined;
- const wrapper = mountModal({
- initialData,
- onAddWidget: data => (widget = data),
- });
- // No delete button as there is only one field.
- expect(wrapper.find('IconDelete')).toHaveLength(0);
- // Select Big Number display
- selectByLabel(wrapper, 'Big Number', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('big_number');
- // Choose any()
- selectByLabel(wrapper, 'any(\u2026)', {
- name: 'field',
- at: 0,
- control: true,
- });
- selectByLabel(wrapper, 'id', {
- name: 'parameter',
- at: 0,
- control: true,
- });
- // Select Line chart display
- selectByLabel(wrapper, 'Line Chart', {name: 'displayType', at: 0, control: true});
- expect(getDisplayType(wrapper).props().value).toEqual('line');
- // Expect event.type field to be converted to count()
- const fieldColumn = wrapper.find('input[name="field"]');
- expect(fieldColumn.length).toEqual(1);
- expect(fieldColumn.props().value).toMatchObject({
- kind: 'function',
- meta: {
- name: 'count',
- parameters: [],
- },
- });
- await clickSubmit(wrapper);
- expect(widget.displayType).toEqual('line');
- expect(widget.queries).toHaveLength(1);
- expect(widget.queries[0].fields).toEqual(['count()']);
- wrapper.unmount();
- });
- });
|