12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629 |
- import selectEvent from 'react-select-event';
- import {urlEncode} from '@sentry/utils';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {mountGlobalModal} from 'sentry-test/modal';
- import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import * as indicators from 'sentry/actionCreators/indicator';
- import * as modals from 'sentry/actionCreators/modal';
- import TagStore from 'sentry/stores/tagStore';
- import {TOP_N} from 'sentry/utils/discover/types';
- import {
- DashboardDetails,
- DashboardWidgetSource,
- DisplayType,
- Widget,
- WidgetType,
- } from 'sentry/views/dashboardsV2/types';
- import WidgetBuilder, {WidgetBuilderProps} from 'sentry/views/dashboardsV2/widgetBuilder';
- const defaultOrgFeatures = [
- 'performance-view',
- 'new-widget-builder-experience-design',
- 'dashboards-edit',
- 'global-views',
- 'dashboard-custom-measurement-widgets',
- ];
- // Mocking worldMapChart to avoid act warnings
- jest.mock('sentry/components/charts/worldMapChart');
- function mockDashboard(dashboard: Partial<DashboardDetails>): DashboardDetails {
- return {
- id: '1',
- title: 'Dashboard',
- createdBy: undefined,
- dateCreated: '2020-01-01T00:00:00.000Z',
- widgets: [],
- projects: [],
- filters: {},
- ...dashboard,
- };
- }
- function renderTestComponent({
- dashboard,
- query,
- orgFeatures,
- onSave,
- params,
- }: {
- dashboard?: WidgetBuilderProps['dashboard'];
- onSave?: WidgetBuilderProps['onSave'];
- orgFeatures?: string[];
- params?: Partial<WidgetBuilderProps['params']>;
- query?: Record<string, any>;
- } = {}) {
- const {organization, router, routerContext} = initializeOrg({
- ...initializeOrg(),
- organization: {
- features: orgFeatures ?? defaultOrgFeatures,
- },
- router: {
- location: {
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- ...query,
- },
- },
- },
- });
- render(
- <WidgetBuilder
- route={{}}
- router={router}
- routes={router.routes}
- routeParams={router.params}
- location={router.location}
- dashboard={{
- id: 'new',
- title: 'Dashboard',
- createdBy: undefined,
- dateCreated: '2020-01-01T00:00:00.000Z',
- widgets: [],
- projects: [],
- filters: {},
- ...dashboard,
- }}
- onSave={onSave ?? jest.fn()}
- params={{
- orgId: organization.slug,
- dashboardId: dashboard?.id ?? 'new',
- ...params,
- }}
- />,
- {
- context: routerContext,
- organization,
- }
- );
- return {router};
- }
- describe('WidgetBuilder', function () {
- const untitledDashboard: DashboardDetails = {
- id: '1',
- title: 'Untitled Dashboard',
- createdBy: undefined,
- dateCreated: '2020-01-01T00:00:00.000Z',
- widgets: [],
- projects: [],
- filters: {},
- };
- const testDashboard: DashboardDetails = {
- id: '2',
- title: 'Test Dashboard',
- createdBy: undefined,
- dateCreated: '2020-01-01T00:00:00.000Z',
- widgets: [],
- projects: [],
- filters: {},
- };
- let eventsStatsMock: jest.Mock | undefined;
- let eventsv2Mock: jest.Mock | undefined;
- let eventsMock: jest.Mock | undefined;
- let sessionsDataMock: jest.Mock | undefined;
- let metricsDataMock: jest.Mock | undefined;
- let tagsMock: jest.Mock | undefined;
- let measurementsMetaMock: jest.Mock | undefined;
- beforeEach(function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/',
- body: [
- {...untitledDashboard, widgetDisplay: [DisplayType.TABLE]},
- {...testDashboard, widgetDisplay: [DisplayType.AREA]},
- ],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/dashboards/widgets/',
- method: 'POST',
- statusCode: 200,
- body: [],
- });
- eventsv2Mock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- method: 'GET',
- statusCode: 200,
- body: {
- meta: {},
- data: [],
- },
- });
- eventsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- method: 'GET',
- statusCode: 200,
- body: {
- meta: {fields: {}},
- data: [],
- },
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/projects/',
- method: 'GET',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/recent-searches/',
- method: 'GET',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/recent-searches/',
- method: 'POST',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/issues/',
- method: 'GET',
- body: [],
- });
- eventsStatsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: [],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/event.type/values/',
- body: [{count: 2, name: 'Nvidia 1080ti'}],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-geo/',
- body: {data: [], meta: {}},
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/users/',
- body: [],
- });
- sessionsDataMock = MockApiClient.addMockResponse({
- method: 'GET',
- url: '/organizations/org-slug/sessions/',
- body: TestStubs.SessionsField({
- field: `sum(session)`,
- }),
- });
- metricsDataMock = MockApiClient.addMockResponse({
- method: 'GET',
- url: '/organizations/org-slug/metrics/data/',
- body: TestStubs.MetricsField({
- field: 'sum(sentry.sessions.session)',
- }),
- });
- tagsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/tags/',
- method: 'GET',
- body: TestStubs.Tags(),
- });
- measurementsMetaMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/measurements-meta/',
- method: 'GET',
- body: {},
- });
- TagStore.reset();
- });
- afterEach(function () {
- MockApiClient.clearMockResponses();
- jest.clearAllMocks();
- jest.useRealTimers();
- });
- describe('with eventsv2', function () {
- it('no feature access', function () {
- renderTestComponent({orgFeatures: []});
- expect(
- screen.getByText("You don't have access to this feature")
- ).toBeInTheDocument();
- });
- it('widget not found', function () {
- const widget: Widget = {
- displayType: DisplayType.AREA,
- interval: '1d',
- queries: [
- {
- name: 'Known Users',
- fields: [],
- columns: [],
- aggregates: [],
- conditions: '',
- orderby: '-time',
- },
- {
- name: 'Anonymous Users',
- fields: [],
- columns: [],
- aggregates: [],
- conditions: '',
- orderby: '-time',
- },
- ],
- title: 'Transactions',
- id: '1',
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- dashboard,
- orgFeatures: ['new-widget-builder-experience-design', 'dashboards-edit'],
- params: {
- widgetIndex: '2', // Out of bounds, only one widget
- },
- });
- expect(
- screen.getByText('The widget you want to edit was not found.')
- ).toBeInTheDocument();
- });
- it('renders a widget not found message if the widget index url is not an integer', function () {
- const widget: Widget = {
- displayType: DisplayType.AREA,
- interval: '1d',
- queries: [
- {
- name: 'Known Users',
- fields: [],
- columns: [],
- aggregates: [],
- conditions: '',
- orderby: '-time',
- },
- ],
- title: 'Transactions',
- id: '1',
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- dashboard,
- orgFeatures: ['new-widget-builder-experience-design', 'dashboards-edit'],
- params: {
- widgetIndex: '0.5', // Invalid index
- },
- });
- expect(
- screen.getByText('The widget you want to edit was not found.')
- ).toBeInTheDocument();
- });
- it('renders', async function () {
- renderTestComponent();
- // Header - Breadcrumbs
- expect(await screen.findByRole('link', {name: 'Dashboards'})).toHaveAttribute(
- 'href',
- '/organizations/org-slug/dashboards/'
- );
- expect(screen.getByRole('link', {name: 'Dashboard'})).toHaveAttribute(
- 'href',
- '/organizations/org-slug/dashboards/new/'
- );
- expect(screen.getByText('Widget Builder')).toBeInTheDocument();
- // Header - Widget Title
- expect(screen.getByRole('heading', {name: 'Custom Widget'})).toBeInTheDocument();
- // Footer - Actions
- expect(screen.getByLabelText('Cancel')).toBeInTheDocument();
- expect(screen.getByLabelText('Add Widget')).toBeInTheDocument();
- // Content - Step 1
- expect(
- screen.getByRole('heading', {name: 'Choose your dataset'})
- ).toBeInTheDocument();
- expect(screen.getByLabelText('Select Errors and Transactions')).toBeChecked();
- // Content - Step 2
- expect(
- screen.getByRole('heading', {name: 'Choose your visualization'})
- ).toBeInTheDocument();
- // Content - Step 3
- expect(
- screen.getByRole('heading', {name: 'Choose your columns'})
- ).toBeInTheDocument();
- // Content - Step 4
- expect(
- screen.getByRole('heading', {name: 'Filter your results'})
- ).toBeInTheDocument();
- // Content - Step 5
- expect(screen.getByRole('heading', {name: 'Sort by a column'})).toBeInTheDocument();
- });
- it('has links back to the new dashboard if creating', async function () {
- // Dashboard has undefined dashboardId when creating from a new dashboard
- // because of route setup
- renderTestComponent({params: {dashboardId: undefined}});
- expect(await screen.findByRole('link', {name: 'Dashboard'})).toHaveAttribute(
- 'href',
- '/organizations/org-slug/dashboards/new/'
- );
- expect(screen.getByLabelText('Cancel')).toHaveAttribute(
- 'href',
- '/organizations/org-slug/dashboards/new/'
- );
- });
- it('renders new design', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- // Switch to line chart for time series
- userEvent.click(await screen.findByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- // Header - Breadcrumbs
- expect(await screen.findByRole('link', {name: 'Dashboards'})).toHaveAttribute(
- 'href',
- '/organizations/org-slug/dashboards/'
- );
- expect(screen.getByRole('link', {name: 'Dashboard'})).toHaveAttribute(
- 'href',
- '/organizations/org-slug/dashboards/new/'
- );
- expect(screen.getByText('Widget Builder')).toBeInTheDocument();
- // Header - Widget Title
- expect(screen.getByRole('heading', {name: 'Custom Widget'})).toBeInTheDocument();
- // Footer - Actions
- expect(screen.getByLabelText('Cancel')).toBeInTheDocument();
- expect(screen.getByLabelText('Add Widget')).toBeInTheDocument();
- // Content - Step 1
- expect(
- screen.getByRole('heading', {name: 'Choose your dataset'})
- ).toBeInTheDocument();
- expect(screen.getByLabelText('Select Errors and Transactions')).toBeChecked();
- // Content - Step 2
- expect(
- screen.getByRole('heading', {name: 'Choose your visualization'})
- ).toBeInTheDocument();
- // Content - Step 3
- expect(
- screen.getByRole('heading', {name: 'Choose what to plot in the y-axis'})
- ).toBeInTheDocument();
- // Content - Step 4
- expect(
- screen.getByRole('heading', {name: 'Filter your results'})
- ).toBeInTheDocument();
- // Content - Step 5
- expect(
- screen.getByRole('heading', {name: 'Group your results'})
- ).toBeInTheDocument();
- });
- it('can update the title', async function () {
- renderTestComponent({
- query: {source: DashboardWidgetSource.DISCOVERV2},
- });
- const customWidgetLabels = await screen.findAllByText('Custom Widget');
- // EditableText and chart title
- expect(customWidgetLabels).toHaveLength(2);
- userEvent.click(customWidgetLabels[0]);
- userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
- userEvent.paste(
- screen.getByRole('textbox', {name: 'Widget title'}),
- 'Unique Users'
- );
- userEvent.keyboard('{enter}');
- expect(screen.queryByText('Custom Widget')).not.toBeInTheDocument();
- expect(screen.getAllByText('Unique Users')).toHaveLength(2);
- });
- it('can add query conditions', async function () {
- const {router} = renderTestComponent({
- query: {source: DashboardWidgetSource.DISCOVERV2},
- dashboard: testDashboard,
- });
- userEvent.type(
- await screen.findByRole('textbox', {name: 'Search events'}),
- 'color:blue{enter}'
- );
- userEvent.click(screen.getByText('Add Widget'));
- await waitFor(() => {
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/dashboard/2/',
- query: {
- displayType: 'table',
- interval: '5m',
- title: 'Custom Widget',
- queryNames: [''],
- queryConditions: ['color:blue'],
- queryFields: ['count()'],
- queryOrderby: '-count()',
- start: null,
- end: null,
- statsPeriod: '24h',
- utc: false,
- project: [],
- environment: [],
- },
- })
- );
- });
- });
- it('can choose a field', async function () {
- const {router} = renderTestComponent({
- query: {source: DashboardWidgetSource.DISCOVERV2},
- dashboard: testDashboard,
- });
- expect(await screen.findAllByText('Custom Widget')).toHaveLength(2);
- // No delete button as there is only one query.
- expect(screen.queryByLabelText('Remove query')).not.toBeInTheDocument();
- // 1 in the table header, 1 in the column selector, 1 in the sort field
- const countFields = screen.getAllByText('count()');
- expect(countFields).toHaveLength(3);
- await selectEvent.select(countFields[1], ['last_seen()']);
- userEvent.click(screen.getByText('Add Widget'));
- await waitFor(() => {
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/dashboard/2/',
- query: {
- displayType: 'table',
- interval: '5m',
- title: 'Custom Widget',
- queryNames: [''],
- queryConditions: [''],
- queryFields: ['last_seen()'],
- queryOrderby: '-last_seen()',
- start: null,
- end: null,
- statsPeriod: '24h',
- utc: false,
- project: [],
- environment: [],
- },
- })
- );
- });
- });
- it('can add additional fields', async function () {
- const handleSave = jest.fn();
- renderTestComponent({onSave: handleSave});
- userEvent.click(await screen.findByText('Table'));
- // Select line chart display
- userEvent.click(screen.getByText('Line Chart'));
- // Click the add overlay button
- userEvent.click(screen.getByLabelText('Add Overlay'));
- await selectEvent.select(screen.getByText('(Required)'), ['count_unique(…)']);
- userEvent.click(screen.getByLabelText('Add Widget'));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- title: 'Custom Widget',
- displayType: DisplayType.LINE,
- interval: '5m',
- widgetType: WidgetType.DISCOVER,
- queries: [
- {
- conditions: '',
- fields: ['count()', 'count_unique(user)'],
- aggregates: ['count()', 'count_unique(user)'],
- fieldAliases: [],
- columns: [],
- orderby: '',
- name: '',
- },
- ],
- }),
- ]);
- });
- expect(handleSave).toHaveBeenCalledTimes(1);
- });
- it('can add equation fields', async function () {
- const handleSave = jest.fn();
- renderTestComponent({onSave: handleSave});
- userEvent.click(await screen.findByText('Table'));
- // Select line chart display
- userEvent.click(screen.getByText('Line Chart'));
- // Click the add an equation button
- userEvent.click(screen.getByLabelText('Add an Equation'));
- expect(screen.getByPlaceholderText('Equation')).toBeInTheDocument();
- userEvent.paste(screen.getByPlaceholderText('Equation'), 'count() + 100');
- userEvent.click(screen.getByLabelText('Add Widget'));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- title: 'Custom Widget',
- displayType: DisplayType.LINE,
- interval: '5m',
- widgetType: WidgetType.DISCOVER,
- queries: [
- {
- name: '',
- fields: ['count()', 'equation|count() + 100'],
- aggregates: ['count()', 'equation|count() + 100'],
- columns: [],
- fieldAliases: [],
- conditions: '',
- orderby: '',
- },
- ],
- }),
- ]);
- });
- expect(handleSave).toHaveBeenCalledTimes(1);
- });
- it('can respond to validation feedback', async function () {
- jest.spyOn(indicators, 'addErrorMessage');
- renderTestComponent();
- userEvent.click(await screen.findByText('Table'));
- const customWidgetLabels = await screen.findAllByText('Custom Widget');
- // EditableText and chart title
- expect(customWidgetLabels).toHaveLength(2);
- userEvent.click(customWidgetLabels[0]);
- userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
- userEvent.keyboard('{enter}');
- expect(indicators.addErrorMessage).toHaveBeenCalledWith('Widget title is required');
- });
- it('sets up widget data in edit correctly', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.LINE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- {
- name: 'csp',
- conditions: 'event.type:csp',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({dashboard, params: {widgetIndex: '0'}});
- await screen.findByText('Line Chart');
- // Should be in edit 'mode'
- expect(await screen.findByText('Update Widget')).toBeInTheDocument();
- // Should set widget data up.
- expect(screen.getByText('Update Widget')).toBeInTheDocument();
- // Filters
- expect(
- screen.getAllByPlaceholderText('Search for events, users, tags, and more')
- ).toHaveLength(2);
- expect(screen.getByText('event.type:csp')).toBeInTheDocument();
- expect(screen.getByText('event.type:error')).toBeInTheDocument();
- // Y-axis
- expect(screen.getAllByRole('button', {name: 'Remove query'})).toHaveLength(2);
- expect(screen.getByText('count()')).toBeInTheDocument();
- expect(screen.getByText('count_unique(…)')).toBeInTheDocument();
- expect(screen.getByText('id')).toBeInTheDocument();
- // 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)'],
- }),
- })
- );
- });
- it('can edit a widget', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.LINE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- {
- name: 'csp',
- conditions: 'event.type:csp',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- const handleSave = jest.fn();
- renderTestComponent({onSave: handleSave, dashboard, params: {widgetIndex: '0'}});
- await screen.findByText('Line Chart');
- // Should be in edit 'mode'
- expect(await screen.findByText('Update Widget')).toBeInTheDocument();
- const customWidgetLabels = await screen.findAllByText(widget.title);
- // EditableText and chart title
- expect(customWidgetLabels).toHaveLength(2);
- userEvent.click(customWidgetLabels[0]);
- userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
- userEvent.paste(screen.getByRole('textbox', {name: 'Widget title'}), 'New Title');
- userEvent.click(screen.getByRole('button', {name: 'Update Widget'}));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- ...widget,
- title: 'New Title',
- }),
- ]);
- });
- expect(handleSave).toHaveBeenCalledTimes(1);
- });
- it('renders column inputs for table widgets', async function () {
- const widget: Widget = {
- id: '0',
- title: 'sdk usage',
- interval: '5m',
- displayType: DisplayType.TABLE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['sdk.name', 'count()'],
- columns: ['sdk.name'],
- aggregates: ['count()'],
- orderby: '',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({dashboard, params: {widgetIndex: '0'}});
- // Should be in edit 'mode'
- expect(await screen.findByText('Update Widget')).toBeInTheDocument();
- // Should set widget data up.
- expect(screen.getByRole('heading', {name: widget.title})).toBeInTheDocument();
- expect(screen.getByText('Table')).toBeInTheDocument();
- expect(screen.getByLabelText('Search events')).toBeInTheDocument();
- // Should have an orderby select
- expect(screen.getByText('Sort by a column')).toBeInTheDocument();
- // Add a column, and choose a value,
- expect(screen.getByLabelText('Add a Column')).toBeInTheDocument();
- });
- it('can save table widgets', async function () {
- const widget: Widget = {
- id: '0',
- title: 'sdk usage',
- interval: '5m',
- displayType: DisplayType.TABLE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['sdk.name', 'count()'],
- columns: ['sdk.name'],
- aggregates: ['count()'],
- orderby: '-count()',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- const handleSave = jest.fn();
- renderTestComponent({dashboard, onSave: handleSave, params: {widgetIndex: '0'}});
- // Should be in edit 'mode'
- expect(await screen.findByText('Update Widget')).toBeInTheDocument();
- // Add a column, and choose a value,
- userEvent.click(screen.getByLabelText('Add a Column'));
- await selectEvent.select(screen.getByText('(Required)'), 'trace');
- // Save widget
- userEvent.click(screen.getByLabelText('Update Widget'));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- id: '0',
- title: 'sdk usage',
- displayType: DisplayType.TABLE,
- interval: '5m',
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['sdk.name', 'count()', 'trace'],
- aggregates: ['count()'],
- columns: ['sdk.name', 'trace'],
- orderby: '-count()',
- fieldAliases: ['', '', ''],
- },
- ],
- widgetType: WidgetType.DISCOVER,
- }),
- ]);
- });
- expect(handleSave).toHaveBeenCalledTimes(1);
- });
- it('should properly query for table fields', async function () {
- const defaultWidgetQuery = {
- name: '',
- fields: ['title', 'count()'],
- columns: ['title'],
- aggregates: ['count()'],
- conditions: '',
- orderby: '',
- };
- const defaultTableColumns = ['title', 'count()', 'count_unique(user)', 'epm()'];
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.LINE,
- defaultTableColumns,
- },
- });
- expect(await screen.findByText('Line Chart')).toBeInTheDocument();
- userEvent.click(screen.getByText('Line Chart'));
- userEvent.click(screen.getByText('Table'));
- await waitFor(() => {
- expect(eventsv2Mock).toHaveBeenLastCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- field: defaultTableColumns,
- }),
- })
- );
- });
- });
- it('should use defaultWidgetQuery Y-Axis and Conditions if given a defaultWidgetQuery', async function () {
- const defaultWidgetQuery = {
- name: '',
- fields: ['count()', 'failure_count()', 'count_unique(user)'],
- columns: [],
- aggregates: ['count()', 'failure_count()', 'count_unique(user)'],
- conditions: 'tag:value',
- orderby: '',
- };
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- },
- });
- expect(await screen.findByText('tag:value')).toBeInTheDocument();
- // Table display, column, and sort field
- expect(screen.getAllByText('count()')).toHaveLength(3);
- // Table display and column
- expect(screen.getAllByText('failure_count()')).toHaveLength(2);
- // Table display
- expect(screen.getByText('count_unique(user)')).toBeInTheDocument();
- // Column
- expect(screen.getByText('count_unique(…)')).toBeInTheDocument();
- // Column
- expect(screen.getByText('user')).toBeInTheDocument();
- });
- it('uses displayType if given a displayType', async function () {
- renderTestComponent({
- query: {
- displayType: DisplayType.BAR,
- },
- });
- expect(await screen.findByText('Bar Chart')).toBeInTheDocument();
- });
- it('deletes the widget when the modal is confirmed', async () => {
- const handleSave = jest.fn();
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.LINE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- {
- name: 'csp',
- conditions: 'event.type:csp',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({onSave: handleSave, dashboard, params: {widgetIndex: '0'}});
- userEvent.click(await screen.findByText('Delete'));
- await mountGlobalModal();
- userEvent.click(await screen.findByText('Confirm'));
- await waitFor(() => {
- // The only widget was deleted
- expect(handleSave).toHaveBeenCalledWith([]);
- });
- expect(handleSave).toHaveBeenCalledTimes(1);
- });
- it('persists the page filter period when updating a widget', async () => {
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.LINE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- const {router} = renderTestComponent({
- dashboard,
- params: {orgId: 'org-slug', widgetIndex: '0'},
- query: {statsPeriod: '90d'},
- });
- await screen.findByText('Update Widget');
- await screen.findByText('90D');
- expect(screen.getByTestId('page-filter-timerange-selector')).toBeEnabled();
- userEvent.click(screen.getByText('Update Widget'));
- await waitFor(() => {
- expect(router.push).toHaveBeenLastCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/dashboard/1/',
- query: expect.objectContaining({
- statsPeriod: '90d',
- }),
- })
- );
- });
- });
- it('renders page filters in the filter step', async () => {
- const mockReleases = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/releases/',
- body: [TestStubs.Release()],
- });
- renderTestComponent({
- params: {orgId: 'org-slug'},
- query: {statsPeriod: '90d'},
- orgFeatures: [...defaultOrgFeatures, 'dashboards-top-level-filter'],
- });
- await screen.findByText('90D');
- expect(screen.getByTestId('page-filter-timerange-selector')).toBeDisabled();
- expect(screen.getByTestId('page-filter-environment-selector')).toBeDisabled();
- expect(screen.getByTestId('page-filter-project-selector-loading')).toBeDisabled();
- await waitFor(() => {
- expect(mockReleases).toHaveBeenCalled();
- });
- expect(screen.getByRole('button', {name: /all releases/i})).toBeDisabled();
- });
- it('appends dashboard filters to widget builder fetch data request', async () => {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/releases/',
- body: [TestStubs.Release()],
- });
- const mock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/eventsv2/',
- body: [],
- });
- renderTestComponent({
- dashboard: {
- id: 'new',
- title: 'Dashboard',
- createdBy: undefined,
- dateCreated: '2020-01-01T00:00:00.000Z',
- widgets: [],
- projects: [],
- filters: {release: ['abc@1.2.0']},
- },
- params: {orgId: 'org-slug'},
- query: {statsPeriod: '90d'},
- orgFeatures: [...defaultOrgFeatures, 'dashboards-top-level-filter'],
- });
- await waitFor(() => {
- expect(mock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- query: ' release:abc@1.2.0 ',
- }),
- })
- );
- });
- });
- it('does not error when query conditions field is blurred', async function () {
- jest.useFakeTimers();
- const widget: Widget = {
- id: '0',
- title: 'sdk usage',
- interval: '5m',
- displayType: DisplayType.BAR,
- queries: [
- {
- name: 'filled in',
- conditions: 'event.type:error',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '-count()',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- const handleSave = jest.fn();
- renderTestComponent({dashboard, onSave: handleSave, params: {widgetIndex: '0'}});
- userEvent.click(await screen.findByLabelText('Add Query'));
- // Triggering the onBlur of the new field should not error
- userEvent.click(
- screen.getAllByPlaceholderText('Search for events, users, tags, and more')[1]
- );
- userEvent.keyboard('{esc}');
- act(() => {
- // Run all timers because the handleBlur contains a setTimeout
- jest.runAllTimers();
- });
- });
- it('does not wipe column changes when filters are modified', async function () {
- jest.useFakeTimers();
- // widgetIndex: undefined means creating a new widget
- renderTestComponent({params: {widgetIndex: undefined}});
- userEvent.click(await screen.findByLabelText('Add a Column'));
- await selectEvent.select(screen.getByText('(Required)'), /project/);
- // Triggering the onBlur of the filter should not error
- userEvent.click(
- screen.getByPlaceholderText('Search for events, users, tags, and more')
- );
- userEvent.keyboard('{enter}');
- act(() => {
- // Run all timers because the handleBlur contains a setTimeout
- jest.runAllTimers();
- });
- expect(await screen.findAllByText('project')).toHaveLength(2);
- });
- it('renders fields with commas properly', async () => {
- const defaultWidgetQuery = {
- conditions: '',
- fields: ['equation|count_if(transaction.duration,equals,300)*2'],
- aggregates: ['equation|count_if(transaction.duration,equals,300)*2'],
- columns: [],
- orderby: '',
- name: '',
- };
- const defaultTableColumns = [
- 'count_if(transaction.duration,equals,300)',
- 'equation|count_if(transaction.duration,equals,300)*2',
- ];
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- defaultTableColumns,
- yAxis: ['equation|count_if(transaction.duration,equals,300)*2'],
- },
- });
- expect(
- await screen.findByText('count_if(transaction.duration,equals,300)*2')
- ).toBeInTheDocument();
- });
- it('sets the correct fields for a top n widget', async () => {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'performance-view'],
- query: {
- displayType: DisplayType.TOP_N,
- },
- });
- // Top N now opens as Area Chart
- await screen.findByText('Area Chart');
- // Add a group by
- userEvent.click(screen.getByText('Add Overlay'));
- await selectEvent.select(screen.getByText('Select group'), /project/);
- // Change the y-axis
- await selectEvent.select(screen.getAllByText('count()')[0], 'eps()');
- await waitFor(() => {
- expect(eventsStatsMock).toHaveBeenLastCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- query: '',
- yAxis: ['eps()'],
- field: ['project', 'eps()'],
- topEvents: TOP_N,
- orderby: '-eps()',
- }),
- })
- );
- });
- });
- it('fetches tags when tag store is empty', function () {
- renderTestComponent();
- expect(tagsMock).toHaveBeenCalled();
- });
- it('does not fetch tags when tag store is not empty', function () {
- TagStore.loadTagsSuccess(TestStubs.Tags());
- renderTestComponent();
- expect(tagsMock).not.toHaveBeenCalled();
- });
- it('excludes the Other series when grouping and using multiple y-axes', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- userEvent.click(screen.getByText('Add Overlay'));
- await selectEvent.select(screen.getByText('(Required)'), /count_unique/);
- await waitFor(() => {
- expect(eventsStatsMock).toBeCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({excludeOther: '1'}),
- })
- );
- });
- });
- it('excludes the Other series when grouping and using multiple queries', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- userEvent.click(screen.getByText('Add Query'));
- await waitFor(() => {
- expect(eventsStatsMock).toBeCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({excludeOther: '1'}),
- })
- );
- });
- });
- it('includes Other series when there is only one query and one y-axis', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- await waitFor(() => {
- expect(eventsStatsMock).toBeCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.not.objectContaining({excludeOther: '1'}),
- })
- );
- });
- });
- it('decreases the limit when more y-axes and queries are added', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- screen.getByText('Limit to 5 results');
- userEvent.click(screen.getByText('Add Query'));
- userEvent.click(screen.getByText('Add Overlay'));
- await screen.findByText('Limit to 2 results');
- });
- it('alerts the user if there are unsaved changes', async function () {
- const {router} = renderTestComponent();
- const alertMock = jest.fn();
- const setRouteLeaveHookMock = jest.spyOn(router, 'setRouteLeaveHook');
- setRouteLeaveHookMock.mockImplementationOnce((_route, _callback) => {
- alertMock();
- });
- const customWidgetLabels = await screen.findAllByText('Custom Widget');
- // EditableText and chart title
- expect(customWidgetLabels).toHaveLength(2);
- // Change title text
- userEvent.click(customWidgetLabels[0]);
- userEvent.clear(screen.getByRole('textbox', {name: 'Widget title'}));
- userEvent.paste(
- screen.getByRole('textbox', {name: 'Widget title'}),
- 'Unique Users'
- );
- userEvent.keyboard('{enter}');
- // Click Cancel
- userEvent.click(screen.getByText('Cancel'));
- // Assert an alert was triggered
- expect(alertMock).toHaveBeenCalled();
- });
- it('does not trigger alert dialog if no changes', async function () {
- const {router} = renderTestComponent();
- const alertMock = jest.fn();
- const setRouteLeaveHookMock = jest.spyOn(router, 'setRouteLeaveHook');
- setRouteLeaveHookMock.mockImplementationOnce((_route, _callback) => {
- alertMock();
- });
- await screen.findAllByText('Custom Widget');
- // Click Cancel
- userEvent.click(screen.getByText('Cancel'));
- // Assert an alert was triggered
- expect(alertMock).not.toHaveBeenCalled();
- });
- describe('Sort by selectors', function () {
- it('renders', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- expect(await screen.findByText('Sort by a column')).toBeInTheDocument();
- expect(
- screen.getByText("Choose one of the columns you've created to sort by.")
- ).toBeInTheDocument();
- // Selector "sortDirection"
- expect(screen.getByText('High to low')).toBeInTheDocument();
- // Selector "sortBy"
- expect(screen.getAllByText('count()')).toHaveLength(3);
- });
- it('ordering by column uses field form when selecting orderby', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Test Widget',
- interval: '5m',
- displayType: DisplayType.TABLE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: ['project'],
- orderby: '-project',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- await waitFor(async () => {
- expect(await screen.findAllByText('project')).toHaveLength(3);
- });
- await selectEvent.select(screen.getAllByText('project')[2], 'count()');
- await waitFor(() => {
- expect(eventsv2Mock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- sort: ['-count()'],
- }),
- })
- );
- });
- });
- it('sortBy defaults to the first field value when changing display type to table', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.LINE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- {
- name: 'csp',
- conditions: 'event.type:csp',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- // Click on the displayType selector
- userEvent.click(await screen.findByText('Line Chart'));
- // Choose the table visualization
- userEvent.click(screen.getByText('Table'));
- expect(await screen.findByText('Sort by a column')).toBeInTheDocument();
- // Selector "sortDirection"
- expect(screen.getByText('High to low')).toBeInTheDocument();
- // Selector "sortBy"
- expect(screen.getAllByText('count()')).toHaveLength(3);
- });
- it('can update selectors values', async function () {
- const handleSave = jest.fn();
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.TABLE,
- queries: [
- {
- name: '',
- conditions: '',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '-count()',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- dashboard,
- onSave: handleSave,
- params: {
- widgetIndex: '0',
- },
- });
- expect(await screen.findByText('Sort by a column')).toBeInTheDocument();
- // Selector "sortDirection"
- expect(screen.getByText('High to low')).toBeInTheDocument();
- // Selector "sortBy"
- expect(screen.getAllByText('count()')).toHaveLength(3);
- await selectEvent.select(screen.getAllByText('count()')[2], 'count_unique(id)');
- // Wait for the Builder update the widget values
- await waitFor(() => {
- expect(screen.getAllByText('count()')).toHaveLength(2);
- });
- // Now count_unique(id) is selected in the "sortBy" selector
- expect(screen.getAllByText('count_unique(id)')).toHaveLength(2);
- await selectEvent.select(screen.getByText('High to low'), 'Low to high');
- // Saves the widget
- userEvent.click(screen.getByText('Update Widget'));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- queries: [expect.objectContaining({orderby: 'count_unique(id)'})],
- }),
- ]);
- });
- });
- it('sortBy defaults to the first field value when coming from discover', async function () {
- const defaultWidgetQuery = {
- name: '',
- fields: ['title', 'count()', 'count_unique(user)', 'epm()', 'count()'],
- columns: ['title'],
- aggregates: ['count()', 'count_unique(user)', 'epm()', 'count()'],
- conditions: 'tag:value',
- orderby: '',
- };
- const {router} = renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.TABLE,
- defaultTableColumns: ['title', 'count()', 'count_unique(user)', 'epm()'],
- },
- });
- expect(await screen.findByText('Sort by a column')).toBeInTheDocument();
- // Selector "sortDirection"
- expect(screen.getByText('Low to high')).toBeInTheDocument();
- // Selector "sortBy"
- expect(screen.getAllByText('title')).toHaveLength(2);
- // Saves the widget
- userEvent.click(screen.getByText('Add Widget'));
- await waitFor(() => {
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({queryOrderby: 'count()'}),
- })
- );
- });
- });
- it('sortBy is only visible on tabular visualizations or when there is a groupBy value selected on time-series visualizations', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- // Sort by shall be visible on table visualization
- expect(await screen.findByText('Sort by a column')).toBeInTheDocument();
- // Update visualization to be a time-series
- userEvent.click(screen.getByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- // Time-series visualizations display GroupBy step
- expect(await screen.findByText('Group your results')).toBeInTheDocument();
- // Do not show sortBy when empty columns (groupBys) are added
- userEvent.click(screen.getByText('Add Group'));
- expect(screen.getAllByText('Select group')).toHaveLength(2);
- // SortBy step shall not be visible
- expect(screen.queryByText('Sort by a y-axis')).not.toBeInTheDocument();
- // Select GroupBy value
- await selectEvent.select(screen.getAllByText('Select group')[0], 'project');
- // Now that at least one groupBy value is selected, the SortBy step shall be visible
- expect(screen.getByText('Sort by a y-axis')).toBeInTheDocument();
- // Remove selected GroupBy value
- userEvent.click(screen.getAllByLabelText('Remove group')[0]);
- // SortBy step shall no longer be visible
- expect(screen.queryByText('Sort by a y-axis')).not.toBeInTheDocument();
- });
- it('allows for sorting by a custom equation', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], 'Custom Equation');
- userEvent.paste(
- screen.getByPlaceholderText('Enter Equation'),
- 'count_unique(user) * 2'
- );
- userEvent.keyboard('{enter}');
- await waitFor(() => {
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- field: expect.arrayContaining(['equation|count_unique(user) * 2']),
- orderby: '-equation[0]',
- }),
- })
- );
- });
- }, 10000);
- it('persists the state when toggling between sorting options', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], 'Custom Equation');
- userEvent.paste(
- screen.getByPlaceholderText('Enter Equation'),
- 'count_unique(user) * 2'
- );
- userEvent.keyboard('{enter}');
- // Switch away from the Custom Equation
- expect(screen.getByText('project')).toBeInTheDocument();
- await selectEvent.select(screen.getByText('Custom Equation'), 'project');
- expect(screen.getAllByText('project')).toHaveLength(2);
- // Switch back, the equation should still be visible
- await selectEvent.select(screen.getAllByText('project')[1], 'Custom Equation');
- expect(screen.getByPlaceholderText('Enter Equation')).toHaveValue(
- 'count_unique(user) * 2'
- );
- });
- it('persists the state when updating y-axes', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], 'Custom Equation');
- userEvent.paste(
- screen.getByPlaceholderText('Enter Equation'),
- 'count_unique(user) * 2'
- );
- userEvent.keyboard('{enter}');
- // Add a y-axis
- userEvent.click(screen.getByText('Add Overlay'));
- // The equation should still be visible
- expect(screen.getByPlaceholderText('Enter Equation')).toHaveValue(
- 'count_unique(user) * 2'
- );
- });
- it('displays the custom equation if the widget has it saved', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Test Widget',
- interval: '5m',
- displayType: DisplayType.LINE,
- queries: [
- {
- name: '',
- conditions: '',
- fields: ['count()', 'project'],
- aggregates: ['count()'],
- columns: ['project'],
- orderby: '-equation|count_unique(user) * 2',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- params: {
- widgetIndex: '0',
- },
- dashboard,
- });
- expect(await screen.findByPlaceholderText('Enter Equation')).toHaveValue(
- 'count_unique(user) * 2'
- );
- });
- it('displays Operators in the input dropdown', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], 'Custom Equation');
- selectEvent.openMenu(screen.getByPlaceholderText('Enter Equation'));
- expect(screen.getByText('Operators')).toBeInTheDocument();
- expect(screen.queryByText('Fields')).not.toBeInTheDocument();
- });
- it('hides Custom Equation input and resets orderby when switching to table', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], 'Custom Equation');
- userEvent.paste(
- screen.getByPlaceholderText('Enter Equation'),
- 'count_unique(user) * 2'
- );
- userEvent.keyboard('{enter}');
- // Switch the display type to Table
- userEvent.click(screen.getByText('Line Chart'));
- userEvent.click(screen.getByText('Table'));
- expect(screen.getAllByText('count()')).toHaveLength(2);
- expect(screen.queryByPlaceholderText('Enter Equation')).not.toBeInTheDocument();
- await waitFor(() => {
- expect(eventsv2Mock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- sort: ['-count()'],
- }),
- })
- );
- });
- });
- it('does not show the Custom Equation input if the only y-axis left is an empty equation', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- userEvent.click(screen.getByText('Add an Equation'));
- userEvent.click(screen.getAllByLabelText('Remove this Y-Axis')[0]);
- expect(screen.queryByPlaceholderText('Enter Equation')).not.toBeInTheDocument();
- });
- it('persists a sort by a grouping when changing y-axes', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- // Change the sort option to a grouping field, and then change a y-axis
- await selectEvent.select(screen.getAllByText('count()')[1], 'project');
- await selectEvent.select(screen.getAllByText('count()')[0], /count_unique/);
- // project should appear in the group by field, as well as the sort field
- expect(screen.getAllByText('project')).toHaveLength(2);
- });
- it('persists sort by a y-axis when grouping changes', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- userEvent.click(await screen.findByText('Add Overlay'));
- await selectEvent.select(screen.getByText('Select group'), 'project');
- // Change the sort by to count_unique
- await selectEvent.select(screen.getAllByText('count()')[1], /count_unique/);
- // Change the grouping
- await selectEvent.select(screen.getByText('project'), 'environment');
- // count_unique(user) should still be the sorting field
- expect(screen.getByText(/count_unique/)).toBeInTheDocument();
- expect(screen.getByText('user')).toBeInTheDocument();
- });
- it('does not remove the Custom Equation field if a grouping is updated', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- await selectEvent.select(screen.getAllByText('count()')[1], 'Custom Equation');
- userEvent.paste(
- screen.getByPlaceholderText('Enter Equation'),
- 'count_unique(user) * 2'
- );
- userEvent.keyboard('{enter}');
- userEvent.click(screen.getByText('Add Group'));
- expect(screen.getByPlaceholderText('Enter Equation')).toHaveValue(
- 'count_unique(user) * 2'
- );
- });
- it.each`
- directionPrefix | expectedOrderSelection | displayType
- ${'-'} | ${'High to low'} | ${DisplayType.TABLE}
- ${''} | ${'Low to high'} | ${DisplayType.TABLE}
- ${'-'} | ${'High to low'} | ${DisplayType.LINE}
- ${''} | ${'Low to high'} | ${DisplayType.LINE}
- `(
- `opens a widget with the '$expectedOrderSelection' sort order when the widget was saved with that direction`,
- async function ({directionPrefix, expectedOrderSelection}) {
- const widget: Widget = {
- id: '1',
- title: 'Test Widget',
- interval: '5m',
- displayType: DisplayType.LINE,
- queries: [
- {
- name: '',
- conditions: '',
- fields: ['count_unique(user)'],
- aggregates: ['count_unique(user)'],
- columns: ['project'],
- orderby: `${directionPrefix}count_unique(user)`,
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- await screen.findByText(expectedOrderSelection);
- }
- );
- it('saved widget with aggregate alias as orderby should persist alias when y-axes change', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Test Widget',
- interval: '5m',
- displayType: DisplayType.TABLE,
- queries: [
- {
- name: '',
- conditions: '',
- fields: ['project', 'count_unique(user)'],
- aggregates: ['count_unique(user)'],
- columns: ['project'],
- orderby: 'count_unique(user)',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- await screen.findByText('Sort by a column');
- // Assert for length 2 since one in the table header and one in sort by
- expect(screen.getAllByText('count_unique(user)')).toHaveLength(2);
- userEvent.click(screen.getByText('Add a Column'));
- // The sort by should still have count_unique(user)
- await waitFor(() =>
- expect(screen.getAllByText('count_unique(user)')).toHaveLength(2)
- );
- });
- it('will reset the sort field when going from line to table when sorting by a value not in fields', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], /count_unique/);
- userEvent.click(screen.getByText('Line Chart'));
- userEvent.click(screen.getByText('Table'));
- // 1 for table header, 1 for column selection, and 1 for sorting
- await waitFor(() => {
- expect(screen.getAllByText('count()')).toHaveLength(3);
- });
- });
- it('equations in y-axis appear in sort by field for grouped timeseries', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- displayType: DisplayType.LINE,
- },
- });
- userEvent.click(await screen.findByText('Add an Equation'));
- userEvent.paste(screen.getByPlaceholderText('Equation'), 'count() * 100');
- userEvent.keyboard('{enter}');
- await selectEvent.select(screen.getByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], 'count() * 100');
- });
- it('does not reset the orderby when ordered by an equation in table', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.TABLE,
- queries: [
- {
- name: '',
- conditions: '',
- fields: [
- 'count()',
- 'count_unique(id)',
- 'equation|count() + count_unique(id)',
- ],
- aggregates: [
- 'count()',
- 'count_unique(id)',
- 'equation|count() + count_unique(id)',
- ],
- columns: [],
- orderby: '-equation[0]',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- await screen.findByText('Sort by a column');
- // 1 in the column selector, 1 in the sort by field
- expect(screen.getAllByText('count() + count_unique(id)')).toHaveLength(2);
- });
- });
- describe('Widget creation coming from other verticals', function () {
- it('redirects correctly when creating a new dashboard', async function () {
- const {router} = renderTestComponent({
- query: {source: DashboardWidgetSource.DISCOVERV2},
- });
- userEvent.click(await screen.findByText('Add Widget'));
- await waitFor(() => {
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/dashboards/new/',
- query: {
- displayType: 'table',
- interval: '5m',
- title: 'Custom Widget',
- queryNames: [''],
- queryConditions: [''],
- queryFields: ['count()'],
- queryOrderby: '-count()',
- start: null,
- end: null,
- statsPeriod: '24h',
- utc: false,
- project: [],
- environment: [],
- },
- })
- );
- });
- });
- it('redirects correctly when choosing an existing dashboard', async function () {
- const {router} = renderTestComponent({
- query: {source: DashboardWidgetSource.DISCOVERV2},
- dashboard: testDashboard,
- });
- userEvent.click(await screen.findByText('Add Widget'));
- await waitFor(() => {
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/dashboard/2/',
- query: {
- displayType: 'table',
- interval: '5m',
- title: 'Custom Widget',
- queryNames: [''],
- queryConditions: [''],
- queryFields: ['count()'],
- queryOrderby: '-count()',
- start: null,
- end: null,
- statsPeriod: '24h',
- utc: false,
- project: [],
- environment: [],
- },
- })
- );
- });
- });
- it('shows the correct orderby when switching from a line chart to table', async function () {
- const defaultWidgetQuery = {
- name: '',
- fields: ['count_unique(user)'],
- columns: [],
- aggregates: ['count_unique(user)'],
- conditions: '',
- orderby: 'count_unique(user)',
- };
- const defaultTableColumns = ['title', 'count_unique(user)'];
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.LINE,
- defaultTableColumns,
- },
- });
- userEvent.click(await screen.findByText('Line Chart'));
- userEvent.click(screen.getByText('Table'));
- expect(screen.getByText('count_unique(user)')).toBeInTheDocument();
- await waitFor(() => {
- expect(eventsv2Mock).toHaveBeenLastCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- field: defaultTableColumns,
- sort: ['count_unique(user)'],
- }),
- })
- );
- });
- });
- it('does not send request with orderby if a timeseries chart without grouping', async function () {
- const defaultWidgetQuery = {
- name: '',
- fields: ['count_unique(user)'],
- columns: [],
- aggregates: ['count_unique(user)'],
- conditions: '',
- orderby: 'count_unique(user)',
- };
- const defaultTableColumns = ['title', 'count_unique(user)'];
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.LINE,
- defaultTableColumns,
- },
- });
- await waitFor(() => {
- expect(eventsStatsMock).toHaveBeenLastCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- orderby: '',
- }),
- })
- );
- });
- });
- });
- it('opens top-N widgets as area display', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Errors over time',
- interval: '5m',
- displayType: DisplayType.TOP_N,
- queries: [
- {
- name: '',
- conditions: '',
- fields: ['count()', 'count_unique(id)'],
- aggregates: ['count()', 'count_unique(id)'],
- columns: [],
- orderby: '-count()',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- expect(await screen.findByText('Area Chart')).toBeInTheDocument();
- });
- it('Update table header values (field alias)', async function () {
- const handleSave = jest.fn();
- renderTestComponent({
- onSave: handleSave,
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Table');
- userEvent.paste(screen.getByPlaceholderText('Alias'), 'First Alias');
- userEvent.click(screen.getByLabelText('Add a Column'));
- userEvent.paste(screen.getAllByPlaceholderText('Alias')[1], 'Second Alias');
- userEvent.click(screen.getByText('Add Widget'));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- queries: [
- expect.objectContaining({fieldAliases: ['First Alias', 'Second Alias']}),
- ],
- }),
- ]);
- });
- });
- it('does not wipe equation aliases when a column alias is updated', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Table');
- userEvent.click(screen.getByText('Add an Equation'));
- userEvent.paste(screen.getAllByPlaceholderText('Alias')[1], 'This should persist');
- userEvent.type(screen.getAllByPlaceholderText('Alias')[0], 'A');
- expect(await screen.findByText('This should persist')).toBeInTheDocument();
- });
- it('does not wipe equation aliases when a column selection is made', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Table');
- userEvent.click(screen.getByText('Add an Equation'));
- userEvent.paste(screen.getAllByPlaceholderText('Alias')[1], 'This should persist');
- // 1 for the table, 1 for the the column selector, 1 for the sort
- await waitFor(() => expect(screen.getAllByText('count()')).toHaveLength(3));
- await selectEvent.select(screen.getAllByText('count()')[1], /count_unique/);
- expect(screen.getByText('This should persist')).toBeInTheDocument();
- });
- it('copies over the orderby from the previous query if adding another', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- userEvent.click(await screen.findByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- await selectEvent.select(screen.getByText('Select group'), 'project');
- await selectEvent.select(screen.getAllByText('count()')[1], 'count_unique(…)');
- MockApiClient.clearMockResponses();
- eventsStatsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events-stats/',
- body: [],
- });
- userEvent.click(screen.getByText('Add Query'));
- // Assert on two calls, one for each query
- const expectedArgs = expect.objectContaining({
- query: expect.objectContaining({
- orderby: '-count_unique(user)',
- }),
- });
- expect(eventsStatsMock).toHaveBeenNthCalledWith(
- 1,
- '/organizations/org-slug/events-stats/',
- expectedArgs
- );
- expect(eventsStatsMock).toHaveBeenNthCalledWith(
- 2,
- '/organizations/org-slug/events-stats/',
- expectedArgs
- );
- });
- describe('Issue Widgets', function () {
- it('sets widgetType to issues', async function () {
- const handleSave = jest.fn();
- renderTestComponent({onSave: handleSave});
- userEvent.click(
- await screen.findByText('Issues (States, Assignment, Time, etc.)')
- );
- userEvent.click(screen.getByLabelText('Add Widget'));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- title: 'Custom Widget',
- displayType: DisplayType.TABLE,
- interval: '5m',
- widgetType: WidgetType.ISSUE,
- queries: [
- {
- conditions: '',
- fields: ['issue', 'assignee', 'title'],
- columns: ['issue', 'assignee', 'title'],
- aggregates: [],
- fieldAliases: [],
- name: '',
- orderby: 'date',
- },
- ],
- }),
- ]);
- });
- expect(handleSave).toHaveBeenCalledTimes(1);
- });
- it('render issues dataset disabled when the display type is not set to table', async function () {
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- },
- });
- userEvent.click(await screen.findByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- expect(
- screen.getByRole('radio', {
- name: 'Select Errors and Transactions',
- })
- ).toBeEnabled();
- expect(
- screen.getByRole('radio', {
- name: 'Select Issues (States, Assignment, Time, etc.)',
- })
- ).toBeDisabled();
- });
- it('disables moving and deleting issue column', async function () {
- renderTestComponent();
- userEvent.click(
- await screen.findByText('Issues (States, Assignment, Time, etc.)')
- );
- expect(screen.getByText('issue')).toBeInTheDocument();
- expect(screen.getByText('assignee')).toBeInTheDocument();
- expect(screen.getByText('title')).toBeInTheDocument();
- expect(screen.getAllByLabelText('Remove column')).toHaveLength(2);
- expect(screen.getAllByLabelText('Drag to reorder')).toHaveLength(3);
- userEvent.click(screen.getAllByLabelText('Remove column')[1]);
- userEvent.click(screen.getAllByLabelText('Remove column')[0]);
- expect(screen.getByText('issue')).toBeInTheDocument();
- expect(screen.queryByText('assignee')).not.toBeInTheDocument();
- expect(screen.queryByText('title')).not.toBeInTheDocument();
- expect(screen.queryByLabelText('Remove column')).not.toBeInTheDocument();
- expect(screen.queryByLabelText('Drag to reorder')).not.toBeInTheDocument();
- });
- it('issue query does not work on default search bar', async function () {
- renderTestComponent();
- const input = (await screen.findByPlaceholderText(
- 'Search for events, users, tags, and more'
- )) as HTMLTextAreaElement;
- userEvent.paste(input, 'bookmarks', {
- clipboardData: {getData: () => ''},
- } as unknown as React.ClipboardEvent<HTMLTextAreaElement>);
- input.setSelectionRange(9, 9);
- expect(await screen.findByText('No items found')).toBeInTheDocument();
- });
- it('renders with an issues search bar when selected in dataset selection', async function () {
- renderTestComponent();
- userEvent.click(
- await screen.findByText('Issues (States, Assignment, Time, etc.)')
- );
- const input = (await screen.findByPlaceholderText(
- 'Search for issues, status, assigned, and more'
- )) as HTMLTextAreaElement;
- userEvent.paste(input, 'is:', {
- clipboardData: {getData: () => ''},
- } as unknown as React.ClipboardEvent<HTMLTextAreaElement>);
- input.setSelectionRange(3, 3);
- expect(await screen.findByText('resolved')).toBeInTheDocument();
- });
- it('Update table header values (field alias)', async function () {
- const handleSave = jest.fn();
- renderTestComponent({
- onSave: handleSave,
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Table');
- userEvent.click(screen.getByText('Issues (States, Assignment, Time, etc.)'));
- userEvent.paste(screen.getAllByPlaceholderText('Alias')[0], 'First Alias');
- userEvent.click(screen.getByText('Add Widget'));
- await waitFor(() => {
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- queries: [
- expect.objectContaining({
- fieldAliases: ['First Alias', '', ''],
- }),
- ],
- }),
- ]);
- });
- });
- });
- describe('Release Widgets', function () {
- const releaseHealthFeatureFlags = [
- ...defaultOrgFeatures,
- 'new-widget-builder-experience-design',
- 'dashboards-releases',
- ];
- it('does not show the Release Health dataset if there is no dashboards-releases flag', async function () {
- renderTestComponent({
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- expect(await screen.findByText('Errors and Transactions')).toBeInTheDocument();
- expect(
- screen.queryByText('Releases (sessions, crash rates)')
- ).not.toBeInTheDocument();
- });
- it('shows the Release Health dataset if there is the dashboards-releases flag', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(await screen.findByText('Errors and Transactions')).toBeInTheDocument();
- expect(screen.getByText('Releases (sessions, crash rates)')).toBeInTheDocument();
- });
- it('maintains the selected dataset when display type is changed', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- expect(screen.getByLabelText(/releases/i)).not.toBeChecked();
- userEvent.click(screen.getByLabelText(/releases/i));
- await waitFor(() => expect(screen.getByLabelText(/releases/i)).toBeChecked());
- userEvent.click(screen.getByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- await waitFor(() => expect(screen.getByLabelText(/releases/i)).toBeChecked());
- });
- it('displays releases tags', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- userEvent.click(screen.getByLabelText(/releases/i));
- expect(screen.getByText('crash_free_rate(…)')).toBeInTheDocument();
- expect(screen.getByText('session')).toBeInTheDocument();
- userEvent.click(screen.getByText('crash_free_rate(…)'));
- expect(screen.getByText('count_unique(…)')).toBeInTheDocument();
- expect(screen.getByText('release')).toBeInTheDocument();
- expect(screen.getByText('environment')).toBeInTheDocument();
- expect(screen.getByText('session.status')).toBeInTheDocument();
- userEvent.click(screen.getByText('count_unique(…)'));
- expect(screen.getByText('user')).toBeInTheDocument();
- });
- it('does not display tags as params', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- userEvent.click(screen.getByLabelText(/releases/i));
- expect(screen.getByText('crash_free_rate(…)')).toBeInTheDocument();
- await selectEvent.select(
- screen.getByText('crash_free_rate(…)'),
- 'count_unique(…)'
- );
- userEvent.click(screen.getByText('user'));
- expect(screen.queryByText('release')).not.toBeInTheDocument();
- expect(screen.queryByText('environment')).not.toBeInTheDocument();
- expect(screen.queryByText('session.status')).not.toBeInTheDocument();
- });
- it('does not allow sort by when session.status is selected', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- userEvent.click(screen.getByLabelText(/releases/i));
- expect(screen.getByText('High to low')).toBeEnabled();
- expect(screen.getByText('crash_free_rate(session)')).toBeInTheDocument();
- userEvent.click(screen.getByLabelText('Add a Column'));
- await selectEvent.select(screen.getByText('(Required)'), 'session.status');
- expect(screen.getByRole('textbox', {name: 'Sort direction'})).toBeDisabled();
- expect(screen.getByRole('textbox', {name: 'Sort by'})).toBeDisabled();
- });
- it('does not allow sort on tags except release', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- userEvent.click(screen.getByLabelText(/releases/i));
- expect(screen.getByText('High to low')).toBeEnabled();
- expect(screen.getByText('crash_free_rate(session)')).toBeInTheDocument();
- userEvent.click(screen.getByLabelText('Add a Column'));
- await selectEvent.select(screen.getByText('(Required)'), 'release');
- userEvent.click(screen.getByLabelText('Add a Column'));
- await selectEvent.select(screen.getByText('(Required)'), 'environment');
- expect(await screen.findByText('Sort by a column')).toBeInTheDocument();
- // Selector "sortDirection"
- expect(screen.getByText('High to low')).toBeInTheDocument();
- // Selector "sortBy"
- userEvent.click(screen.getAllByText('crash_free_rate(session)')[1]);
- // release exists in sort by selector
- expect(screen.getAllByText('release')).toHaveLength(3);
- // environment does not exist in sort by selector
- expect(screen.getAllByText('environment')).toHaveLength(2);
- });
- it('makes the appropriate sessions call', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- userEvent.click(screen.getByLabelText(/releases/i));
- userEvent.click(screen.getByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- await waitFor(() =>
- expect(metricsDataMock).toHaveBeenLastCalledWith(
- `/organizations/org-slug/metrics/data/`,
- expect.objectContaining({
- query: expect.objectContaining({
- environment: [],
- field: [`session.crash_free_rate`],
- groupBy: [],
- interval: '5m',
- project: [],
- statsPeriod: '24h',
- }),
- })
- )
- );
- });
- it('calls the session endpoint with the right limit', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- userEvent.click(screen.getByLabelText(/releases/i));
- userEvent.click(screen.getByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getByText('Limit to 5 results')).toBeInTheDocument();
- await waitFor(() =>
- expect(metricsDataMock).toHaveBeenLastCalledWith(
- `/organizations/org-slug/metrics/data/`,
- expect.objectContaining({
- query: expect.objectContaining({
- environment: [],
- field: ['session.crash_free_rate'],
- groupBy: ['project_id'],
- interval: '5m',
- orderBy: '-session.crash_free_rate',
- per_page: 5,
- project: [],
- statsPeriod: '24h',
- }),
- })
- )
- );
- });
- it('calls sessions api when session.status is selected as a groupby', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- userEvent.click(screen.getByLabelText(/releases/i));
- userEvent.click(screen.getByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- await selectEvent.select(
- await screen.findByText('Select group'),
- 'session.status'
- );
- expect(screen.getByText('Limit to 5 results')).toBeInTheDocument();
- await waitFor(() =>
- expect(sessionsDataMock).toHaveBeenLastCalledWith(
- `/organizations/org-slug/sessions/`,
- expect.objectContaining({
- query: expect.objectContaining({
- environment: [],
- field: ['crash_free_rate(session)'],
- groupBy: ['session.status'],
- interval: '5m',
- project: [],
- statsPeriod: '24h',
- }),
- })
- )
- );
- });
- it('displays the correct options for area chart', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- expect(
- await screen.findByText('Releases (sessions, crash rates)')
- ).toBeInTheDocument();
- // change dataset to releases
- userEvent.click(screen.getByLabelText(/releases/i));
- userEvent.click(screen.getByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- expect(screen.getByText('crash_free_rate(…)')).toBeInTheDocument();
- expect(screen.getByText(`session`)).toBeInTheDocument();
- userEvent.click(screen.getByText('crash_free_rate(…)'));
- expect(screen.getByText('count_unique(…)')).toBeInTheDocument();
- userEvent.click(screen.getByText('count_unique(…)'));
- expect(screen.getByText('user')).toBeInTheDocument();
- });
- it('sets widgetType to release', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- userEvent.click(await screen.findByText('Releases (sessions, crash rates)'));
- expect(metricsDataMock).toHaveBeenCalled();
- expect(screen.getByLabelText(/Releases/i)).toBeChecked();
- });
- it('does not display "add an equation" button', async function () {
- const widget: Widget = {
- title: 'Release Widget',
- displayType: DisplayType.TABLE,
- widgetType: WidgetType.RELEASE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['sdk.name', 'count()'],
- columns: ['sdk.name'],
- aggregates: ['count()'],
- orderby: '-sdk.name',
- },
- ],
- interval: '1d',
- id: '1',
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- // Select line chart display
- userEvent.click(await screen.findByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- await waitFor(() =>
- expect(screen.queryByLabelText('Add an Equation')).not.toBeInTheDocument()
- );
- });
- it('render release dataset disabled when the display type is world map', async function () {
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- },
- orgFeatures: releaseHealthFeatureFlags,
- });
- userEvent.click(await screen.findByText('Table'));
- userEvent.click(screen.getByText('World Map'));
- await waitFor(() => expect(screen.getByLabelText(/Releases/)).toBeDisabled());
- expect(
- screen.getByRole('radio', {
- name: 'Select Errors and Transactions',
- })
- ).toBeEnabled();
- expect(
- screen.getByRole('radio', {
- name: 'Select Issues (States, Assignment, Time, etc.)',
- })
- ).toBeDisabled();
- });
- it('renders with a release search bar', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- userEvent.type(
- await screen.findByPlaceholderText('Search for events, users, tags, and more'),
- 'session.status:'
- );
- await waitFor(() => {
- expect(screen.getByText("isn't supported here.")).toBeInTheDocument();
- });
- userEvent.click(screen.getByText('Releases (sessions, crash rates)'));
- userEvent.click(
- screen.getByPlaceholderText(
- 'Search for release version, session status, and more'
- )
- );
- expect(await screen.findByText('environment')).toBeInTheDocument();
- expect(screen.getByText('project')).toBeInTheDocument();
- expect(screen.getByText('release')).toBeInTheDocument();
- });
- it('adds a function when the only column chosen in a table is a tag', async function () {
- renderTestComponent({
- orgFeatures: releaseHealthFeatureFlags,
- });
- userEvent.click(await screen.findByText('Releases (sessions, crash rates)'));
- await selectEvent.select(screen.getByText('crash_free_rate(…)'), 'environment');
- // 1 in the table header, 1 in the column selector, and 1 in the sort by
- expect(screen.getAllByText(/crash_free_rate/)).toHaveLength(3);
- expect(screen.getAllByText('environment')).toHaveLength(2);
- });
- });
- describe('Widget Library', function () {
- it('renders', async function () {
- renderTestComponent();
- expect(await screen.findByText('Widget Library')).toBeInTheDocument();
- });
- it('only opens the modal when the query data is changed', async function () {
- const mockModal = jest.spyOn(modals, 'openWidgetBuilderOverwriteModal');
- renderTestComponent();
- await screen.findByText('Widget Library');
- userEvent.click(screen.getByText('Duration Distribution'));
- // Widget Library, Builder title, and Chart title
- expect(await screen.findAllByText('Duration Distribution')).toHaveLength(3);
- // Confirm modal doesn't open because no changes were made
- expect(mockModal).not.toHaveBeenCalled();
- userEvent.click(screen.getAllByLabelText('Remove this Y-Axis')[0]);
- userEvent.click(screen.getByText('High Throughput Transactions'));
- // Should not have overwritten widget data, and confirm modal should open
- expect(await screen.findAllByText('Duration Distribution')).toHaveLength(3);
- expect(mockModal).toHaveBeenCalled();
- });
- });
- describe('group by field', function () {
- it('does not contain functions as options', async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Group your results');
- expect(screen.getByText('Select group')).toBeInTheDocument();
- userEvent.click(screen.getByText('Select group'));
- // Only one f(x) field set in the y-axis selector
- expect(screen.getByText('f(x)')).toBeInTheDocument();
- });
- it('adds more fields when Add Group is clicked', async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Group your results');
- userEvent.click(screen.getByText('Add Group'));
- expect(await screen.findAllByText('Select group')).toHaveLength(2);
- });
- it("doesn't reset group by when changing y-axis", async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- userEvent.click(screen.getAllByText('count()')[0], undefined, {skipHover: true});
- userEvent.click(screen.getByText(/count_unique/), undefined, {skipHover: true});
- expect(await screen.findByText('project')).toBeInTheDocument();
- });
- it("doesn't erase the selection when switching to another time series", async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- userEvent.click(screen.getByText('Line Chart'));
- userEvent.click(screen.getByText('Area Chart'));
- expect(await screen.findByText('project')).toBeInTheDocument();
- });
- it('sends a top N request when a grouping is selected', async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- userEvent.click(await screen.findByText('Group your results'));
- userEvent.type(screen.getByText('Select group'), 'project{enter}');
- await waitFor(() =>
- expect(eventsStatsMock).toHaveBeenNthCalledWith(
- 2,
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- query: '',
- yAxis: ['count()'],
- field: ['project', 'count()'],
- topEvents: TOP_N,
- orderby: '-count()',
- }),
- })
- )
- );
- });
- it('allows deleting groups until there is one left', async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Group your results');
- userEvent.click(screen.getByText('Add Group'));
- expect(screen.getAllByLabelText('Remove group')).toHaveLength(2);
- userEvent.click(screen.getAllByLabelText('Remove group')[1]);
- await waitFor(() =>
- expect(screen.queryByLabelText('Remove group')).not.toBeInTheDocument()
- );
- });
- it("display 'remove' and 'drag to reorder' buttons", async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await screen.findByText('Select group');
- expect(screen.queryByLabelText('Remove group')).not.toBeInTheDocument();
- await selectEvent.select(screen.getByText('Select group'), 'project');
- expect(screen.getByLabelText('Remove group')).toBeInTheDocument();
- expect(screen.queryByLabelText('Drag to reorder')).not.toBeInTheDocument();
- userEvent.click(screen.getByText('Add Group'));
- expect(screen.getAllByLabelText('Remove group')).toHaveLength(2);
- expect(screen.getAllByLabelText('Drag to reorder')).toHaveLength(2);
- });
- it.todo(
- 'Since simulate drag and drop with RTL is not recommended because of browser layout, remember to create acceptance test for this'
- );
- });
- describe('limit field', function () {
- it('renders if groupBy value is present', async function () {
- const handleSave = jest.fn();
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- onSave: handleSave,
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getByText('Limit to 5 results')).toBeInTheDocument();
- userEvent.click(screen.getByText('Add Widget'));
- await waitFor(() =>
- expect(handleSave).toHaveBeenCalledWith([
- expect.objectContaining({
- limit: 5,
- }),
- ])
- );
- });
- it('update value', async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- userEvent.click(screen.getByText('Limit to 5 results'));
- userEvent.click(screen.getByText('Limit to 2 results'));
- await waitFor(() =>
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- query: '',
- yAxis: ['count()'],
- field: ['project', 'count()'],
- topEvents: 2,
- orderby: '-count()',
- }),
- })
- )
- );
- });
- it('gets removed if no groupBy value', async function () {
- renderTestComponent({
- query: {displayType: 'line'},
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getByText('Limit to 5 results')).toBeInTheDocument();
- userEvent.click(screen.getByLabelText('Remove group'));
- await waitFor(() =>
- expect(screen.queryByText('Limit to 5 results')).not.toBeInTheDocument()
- );
- });
- it('applies a limit when switching from a table to timeseries chart with grouping', async function () {
- const widget: Widget = {
- displayType: DisplayType.TABLE,
- interval: '1d',
- queries: [
- {
- name: 'Test Widget',
- fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
- columns: ['project'],
- aggregates: ['count()', 'count_unique(user)', 'epm()'],
- conditions: '',
- orderby: '',
- },
- ],
- title: 'Transactions',
- id: '1',
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- dashboard,
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- params: {
- widgetIndex: '0',
- },
- });
- userEvent.click(await screen.findByText('Table'));
- userEvent.click(screen.getByText('Line Chart'));
- expect(screen.getByText('Limit to 3 results')).toBeInTheDocument();
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- topEvents: 3,
- }),
- })
- );
- });
- it('persists the limit when switching between timeseries charts', async function () {
- const widget: Widget = {
- displayType: DisplayType.AREA,
- interval: '1d',
- queries: [
- {
- name: 'Test Widget',
- fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
- columns: ['project'],
- aggregates: ['count()', 'count_unique(user)', 'epm()'],
- conditions: '',
- orderby: '',
- },
- ],
- title: 'Transactions',
- id: '1',
- limit: 1,
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- dashboard,
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- params: {
- widgetIndex: '0',
- },
- });
- userEvent.click(await screen.findByText('Area Chart'));
- userEvent.click(screen.getByText('Line Chart'));
- expect(screen.getByText('Limit to 1 result')).toBeInTheDocument();
- expect(eventsStatsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events-stats/',
- expect.objectContaining({
- query: expect.objectContaining({
- topEvents: 1,
- }),
- })
- );
- });
- it('unsets the limit when going from timeseries to table', async function () {
- const widget: Widget = {
- displayType: DisplayType.AREA,
- interval: '1d',
- queries: [
- {
- name: 'Test Widget',
- fields: ['count()', 'count_unique(user)', 'epm()', 'project'],
- columns: ['project'],
- aggregates: ['count()', 'count_unique(user)', 'epm()'],
- conditions: '',
- orderby: '',
- },
- ],
- title: 'Transactions',
- id: '1',
- limit: 1,
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- dashboard,
- orgFeatures: [...defaultOrgFeatures, 'new-widget-builder-experience-design'],
- params: {
- widgetIndex: '0',
- },
- });
- userEvent.click(await screen.findByText('Area Chart'));
- userEvent.click(screen.getByText('Table'));
- expect(screen.queryByText('Limit to 1 result')).not.toBeInTheDocument();
- expect(eventsv2Mock).toHaveBeenCalledWith(
- '/organizations/org-slug/eventsv2/',
- expect.objectContaining({
- query: expect.objectContaining({
- topEvents: undefined,
- }),
- })
- );
- });
- });
- });
- describe('with events', function () {
- it('should properly query for table fields', async function () {
- const defaultWidgetQuery = {
- name: '',
- fields: ['title', 'count()'],
- columns: ['title'],
- aggregates: ['count()'],
- conditions: '',
- orderby: '',
- };
- const defaultTableColumns = ['title', 'count()', 'count_unique(user)', 'epm()'];
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.LINE,
- defaultTableColumns,
- },
- orgFeatures: [...defaultOrgFeatures, 'discover-frontend-use-events-endpoint'],
- });
- expect(await screen.findByText('Line Chart')).toBeInTheDocument();
- userEvent.click(screen.getByText('Line Chart'));
- userEvent.click(screen.getByText('Table'));
- await waitFor(() => {
- expect(eventsMock).toHaveBeenLastCalledWith(
- '/organizations/org-slug/events/',
- expect.objectContaining({
- query: expect.objectContaining({
- field: defaultTableColumns,
- }),
- })
- );
- });
- });
- describe('Sort by selectors', function () {
- it('ordering by column uses field form when selecting orderby', async function () {
- const widget: Widget = {
- id: '1',
- title: 'Test Widget',
- interval: '5m',
- displayType: DisplayType.TABLE,
- queries: [
- {
- name: 'errors',
- conditions: 'event.type:error',
- fields: ['count()'],
- aggregates: ['count()'],
- columns: ['project'],
- orderby: '-project',
- },
- ],
- };
- const dashboard = mockDashboard({widgets: [widget]});
- renderTestComponent({
- orgFeatures: [
- ...defaultOrgFeatures,
- 'new-widget-builder-experience-design',
- 'discover-frontend-use-events-endpoint',
- ],
- dashboard,
- params: {
- widgetIndex: '0',
- },
- });
- await waitFor(async () => {
- expect(await screen.findAllByText('project')).toHaveLength(3);
- });
- await selectEvent.select(screen.getAllByText('project')[2], 'count()');
- await waitFor(() => {
- expect(eventsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events/',
- expect.objectContaining({
- query: expect.objectContaining({
- sort: ['-count()'],
- }),
- })
- );
- });
- });
- it('hides Custom Equation input and resets orderby when switching to table', async function () {
- renderTestComponent({
- orgFeatures: [
- ...defaultOrgFeatures,
- 'new-widget-builder-experience-design',
- 'discover-frontend-use-events-endpoint',
- ],
- query: {
- source: DashboardWidgetSource.DASHBOARDS,
- displayType: DisplayType.LINE,
- },
- });
- await selectEvent.select(await screen.findByText('Select group'), 'project');
- expect(screen.getAllByText('count()')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], 'Custom Equation');
- userEvent.paste(
- screen.getByPlaceholderText('Enter Equation'),
- 'count_unique(user) * 2'
- );
- userEvent.keyboard('{enter}');
- // Switch the display type to Table
- userEvent.click(screen.getByText('Line Chart'));
- userEvent.click(screen.getByText('Table'));
- expect(screen.getAllByText('count()')).toHaveLength(2);
- expect(screen.queryByPlaceholderText('Enter Equation')).not.toBeInTheDocument();
- await waitFor(() => {
- expect(eventsMock).toHaveBeenCalledWith(
- '/organizations/org-slug/events/',
- expect.objectContaining({
- query: expect.objectContaining({
- sort: ['-count()'],
- }),
- })
- );
- });
- });
- });
- describe('Widget creation coming from other verticals', function () {
- it('shows the correct orderby when switching from a line chart to table', async function () {
- const defaultWidgetQuery = {
- name: '',
- fields: ['count_unique(user)'],
- columns: [],
- aggregates: ['count_unique(user)'],
- conditions: '',
- orderby: 'count_unique(user)',
- };
- const defaultTableColumns = ['title', 'count_unique(user)'];
- renderTestComponent({
- orgFeatures: [
- ...defaultOrgFeatures,
- 'new-widget-builder-experience-design',
- 'discover-frontend-use-events-endpoint',
- ],
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.LINE,
- defaultTableColumns,
- },
- });
- userEvent.click(await screen.findByText('Line Chart'));
- userEvent.click(screen.getByText('Table'));
- expect(screen.getByText('count_unique(user)')).toBeInTheDocument();
- await waitFor(() => {
- expect(eventsMock).toHaveBeenLastCalledWith(
- '/organizations/org-slug/events/',
- expect.objectContaining({
- query: expect.objectContaining({
- field: defaultTableColumns,
- sort: ['count_unique(user)'],
- }),
- })
- );
- });
- });
- });
- describe('Custom Performance Metrics', function () {
- it('can choose a custom measurement', async function () {
- measurementsMetaMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/measurements-meta/',
- method: 'GET',
- body: {'measurements.custom.measurement': {functions: ['p99']}},
- });
- eventsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- method: 'GET',
- statusCode: 200,
- body: {
- meta: {
- fields: {'p99(measurements.total.db.calls)': 'duration'},
- isMetricsData: true,
- },
- data: [{'p99(measurements.total.db.calls)': 10}],
- },
- });
- const {router} = renderTestComponent({
- query: {source: DashboardWidgetSource.DISCOVERV2},
- dashboard: testDashboard,
- orgFeatures: [...defaultOrgFeatures, 'discover-frontend-use-events-endpoint'],
- });
- expect(await screen.findAllByText('Custom Widget')).toHaveLength(2);
- // 1 in the table header, 1 in the column selector, 1 in the sort field
- const countFields = screen.getAllByText('count()');
- expect(countFields).toHaveLength(3);
- await selectEvent.select(countFields[1], ['p99(…)']);
- await selectEvent.select(screen.getByText('transaction.duration'), [
- 'measurements.custom.measurement',
- ]);
- userEvent.click(screen.getByText('Add Widget'));
- await waitFor(() => {
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- pathname: '/organizations/org-slug/dashboard/2/',
- query: {
- displayType: 'table',
- interval: '5m',
- title: 'Custom Widget',
- queryNames: [''],
- queryConditions: [''],
- queryFields: ['p99(measurements.custom.measurement)'],
- queryOrderby: '-p99(measurements.custom.measurement)',
- start: null,
- end: null,
- statsPeriod: '24h',
- utc: false,
- project: [],
- environment: [],
- },
- })
- );
- });
- });
- it('raises an alert banner and disables saving widget if widget result is not metrics data and widget is using custom measurements', async function () {
- eventsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- method: 'GET',
- statusCode: 200,
- body: {
- meta: {
- fields: {'p99(measurements.custom.measurement)': 'duration'},
- isMetricsData: false,
- },
- data: [{'p99(measurements.custom.measurement)': 10}],
- },
- });
- const defaultWidgetQuery = {
- name: '',
- fields: ['p99(measurements.custom.measurement)'],
- columns: [],
- aggregates: ['p99(measurements.custom.measurement)'],
- conditions: 'user:test.user@sentry.io',
- orderby: '',
- };
- const defaultTableColumns = ['p99(measurements.custom.measurement)'];
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.TABLE,
- defaultTableColumns,
- },
- orgFeatures: [
- ...defaultOrgFeatures,
- 'discover-frontend-use-events-endpoint',
- 'dashboards-mep',
- ],
- });
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- await waitFor(() => {
- expect(eventsMock).toHaveBeenCalled();
- });
- screen.getByText('You have inputs that are incompatible with');
- expect(screen.getByText('Add Widget').closest('button')).toBeDisabled();
- });
- it('raises an alert banner if widget result is not metrics data', async function () {
- eventsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- method: 'GET',
- statusCode: 200,
- body: {
- meta: {
- fields: {'p99(measurements.lcp)': 'duration'},
- isMetricsData: false,
- },
- data: [{'p99(measurements.lcp)': 10}],
- },
- });
- const defaultWidgetQuery = {
- name: '',
- fields: ['p99(measurements.lcp)'],
- columns: [],
- aggregates: ['p99(measurements.lcp)'],
- conditions: 'user:test.user@sentry.io',
- orderby: '',
- };
- const defaultTableColumns = ['p99(measurements.lcp)'];
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.TABLE,
- defaultTableColumns,
- },
- orgFeatures: [
- ...defaultOrgFeatures,
- 'discover-frontend-use-events-endpoint',
- 'dashboards-mep',
- ],
- });
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- await waitFor(() => {
- expect(eventsMock).toHaveBeenCalled();
- });
- screen.getByText('Your selection is only applicable to');
- });
- it('does not raise an alert banner if widget result is not metrics data but widget contains error fields', async function () {
- eventsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/events/',
- method: 'GET',
- statusCode: 200,
- body: {
- meta: {
- fields: {'p99(measurements.lcp)': 'duration'},
- isMetricsData: false,
- },
- data: [{'p99(measurements.lcp)': 10}],
- },
- });
- const defaultWidgetQuery = {
- name: '',
- fields: ['p99(measurements.lcp)'],
- columns: ['error.handled'],
- aggregates: ['p99(measurements.lcp)'],
- conditions: 'user:test.user@sentry.io',
- orderby: '',
- };
- const defaultTableColumns = ['p99(measurements.lcp)'];
- renderTestComponent({
- query: {
- source: DashboardWidgetSource.DISCOVERV2,
- defaultWidgetQuery: urlEncode(defaultWidgetQuery),
- displayType: DisplayType.TABLE,
- defaultTableColumns,
- },
- orgFeatures: [
- ...defaultOrgFeatures,
- 'discover-frontend-use-events-endpoint',
- 'dashboards-mep',
- ],
- });
- await waitFor(() => {
- expect(measurementsMetaMock).toHaveBeenCalled();
- });
- await waitFor(() => {
- expect(eventsMock).toHaveBeenCalled();
- });
- expect(
- screen.queryByText('Your selection is only applicable to')
- ).not.toBeInTheDocument();
- });
- it('only displays custom measurements in supported functions', async function () {
- measurementsMetaMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/measurements-meta/',
- method: 'GET',
- body: {
- 'measurements.custom.measurement': {functions: ['p99']},
- 'measurements.another.custom.measurement': {functions: ['p95']},
- },
- });
- renderTestComponent({
- query: {source: DashboardWidgetSource.DISCOVERV2},
- dashboard: testDashboard,
- orgFeatures: [...defaultOrgFeatures, 'discover-frontend-use-events-endpoint'],
- });
- expect(await screen.findAllByText('Custom Widget')).toHaveLength(2);
- await selectEvent.select(screen.getAllByText('count()')[1], ['p99(…)']);
- userEvent.click(screen.getByText('transaction.duration'));
- screen.getByText('measurements.custom.measurement');
- expect(
- screen.queryByText('measurements.another.custom.measurement')
- ).not.toBeInTheDocument();
- await selectEvent.select(screen.getAllByText('p99(…)')[0], ['p95(…)']);
- userEvent.click(screen.getByText('transaction.duration'));
- screen.getByText('measurements.another.custom.measurement');
- expect(
- screen.queryByText('measurements.custom.measurement')
- ).not.toBeInTheDocument();
- });
- });
- });
- });
|