12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361 |
- import {LocationFixture} from 'sentry-fixture/locationFixture';
- import {act, renderHook} from 'sentry-test/reactTestingLibrary';
- import type {Column} from 'sentry/utils/discover/fields';
- import {useLocation} from 'sentry/utils/useLocation';
- import {useNavigate} from 'sentry/utils/useNavigate';
- import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
- import {WidgetBuilderProvider} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
- import useWidgetBuilderState, {
- BuilderStateAction,
- serializeFields,
- } from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
- import {FieldValueKind} from 'sentry/views/discover/table/types';
- jest.mock('sentry/utils/useLocation');
- jest.mock('sentry/utils/useNavigate');
- const mockedUsedLocation = jest.mocked(useLocation);
- const mockedUseNavigate = jest.mocked(useNavigate);
- describe('useWidgetBuilderState', () => {
- let mockNavigate!: jest.Mock;
- beforeEach(() => {
- mockNavigate = jest.fn();
- mockedUseNavigate.mockReturnValue(mockNavigate);
- jest.useFakeTimers();
- });
- afterEach(() => {
- jest.useRealTimers();
- jest.clearAllMocks();
- });
- it('returns the widget builder state from the query params', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- title: 'test',
- description: 'lalala this is a description',
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.title).toBe('test');
- expect(result.current.state.description).toBe('lalala this is a description');
- });
- it('sets the new title and description in the query params', () => {
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_TITLE,
- payload: 'new title',
- });
- });
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DESCRIPTION,
- payload: 'new description',
- });
- });
- jest.runAllTimers();
- expect(mockNavigate).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({title: 'new title'}),
- }),
- {replace: true}
- );
- expect(mockNavigate).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({description: 'new description'}),
- }),
- {replace: true}
- );
- });
- describe('display type', () => {
- it('returns the display type from the query params', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {displayType: DisplayType.AREA},
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.displayType).toBe(DisplayType.AREA);
- });
- it('returns a default display type from the query params when the display type is not valid', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {displayType: 'invalid'},
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.displayType).toBe(DisplayType.TABLE);
- });
- it('sets the display type in the query params', () => {
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.AREA,
- });
- });
- jest.runAllTimers();
- expect(mockNavigate).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({displayType: DisplayType.AREA}),
- }),
- {replace: true}
- );
- });
- it('persists the values when going from timeseries to timeseries', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.LINE,
- field: ['event.type'],
- yAxis: ['count()', 'count_unique(user)'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.displayType).toBe(DisplayType.LINE);
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- ]);
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.AREA,
- });
- });
- expect(result.current.state.displayType).toBe(DisplayType.AREA);
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- ]);
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('concatenates the values when going from timeseries to table', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.LINE,
- field: ['event.type'],
- yAxis: ['count()', 'count_unique(user)'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.displayType).toBe(DisplayType.LINE);
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- ]);
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.TABLE,
- });
- });
- expect(result.current.state.displayType).toBe(DisplayType.TABLE);
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('separates the values when going from table to timeseries', () => {
- // remember, this takes up to 3 yAxes
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.TABLE,
- field: [
- 'event.type',
- 'potato',
- 'count()',
- 'count_unique(user)',
- 'count_unique(potato)',
- 'count_unique(thisIsRemoved)',
- ],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.displayType).toBe(DisplayType.TABLE);
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- {field: 'potato', alias: undefined, kind: 'field'},
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'potato', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'thisIsRemoved', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.LINE,
- });
- });
- expect(result.current.state.displayType).toBe(DisplayType.LINE);
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- {field: 'potato', alias: undefined, kind: 'field'},
- ]);
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'potato', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('does not duplicate fields when switching dataset in line chart then display type to table', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.LINE,
- dataset: WidgetType.ERRORS,
- yAxis: ['count()'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.TRANSACTIONS,
- });
- });
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- expect(result.current.state.fields).toEqual([]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.TABLE,
- });
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('does not duplicate fields when changing display from table to chart', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.TABLE,
- dataset: WidgetType.ERRORS,
- field: ['count()'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.SPANS,
- });
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', 'span.duration', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- expect(result.current.state.yAxis).toEqual([]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.LINE,
- });
- });
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', 'span.duration', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('does not duplicate fields when switching dataset in big number then display type to table', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.BIG_NUMBER,
- dataset: WidgetType.ERRORS,
- field: ['count()'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.TRANSACTIONS,
- });
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- expect(result.current.state.yAxis).toEqual([]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.TABLE,
- });
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('sets the aggregate as fields when switching to big number', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.TABLE,
- field: ['event.type', 'count()'],
- sort: ['-count()'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.BIG_NUMBER,
- });
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- expect(result.current.state.sort).toEqual([]);
- });
- it('selects the first filter when switching to big number', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- field: ['event.type', 'count()', 'count_unique(user)'],
- query: ['event.type:test', 'event.type:test2'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.query).toEqual(['event.type:test', 'event.type:test2']);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.BIG_NUMBER,
- });
- });
- expect(result.current.state.query).toEqual(['event.type:test']);
- });
- it('resets selectedAggregate when the display type is switched', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({query: {selectedAggregate: '0'}})
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.selectedAggregate).toBeUndefined();
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.TABLE,
- });
- });
- expect(result.current.state.selectedAggregate).toBeUndefined();
- });
- it('resets thresholds when the display type is switched', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- dataset: WidgetType.ERRORS,
- displayType: DisplayType.BIG_NUMBER,
- thresholds: '{"max_values":{"max1":200,"max2":300},"unit":"milliseconds"}',
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.thresholds).toEqual({
- max_values: {max1: 200, max2: 300},
- unit: 'milliseconds',
- });
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.TABLE,
- });
- });
- expect(result.current.state.thresholds).toBeUndefined();
- });
- });
- describe('dataset', () => {
- it('returns the dataset from the query params', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({query: {dataset: WidgetType.ISSUE}})
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.dataset).toBe(WidgetType.ISSUE);
- });
- it('sets the dataset in the query params', () => {
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.METRICS,
- });
- });
- jest.runAllTimers();
- expect(mockNavigate).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({dataset: WidgetType.METRICS}),
- }),
- {replace: true}
- );
- });
- it('returns errors as the default dataset', () => {
- mockedUsedLocation.mockReturnValue(LocationFixture({query: {dataset: 'invalid'}}));
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.dataset).toBe(WidgetType.ERRORS);
- });
- it('resets the display type to table when the dataset is switched to issues', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {dataset: WidgetType.TRANSACTIONS, displayType: DisplayType.LINE},
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.displayType).toBe(DisplayType.LINE);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.ISSUE,
- });
- });
- expect(result.current.state.displayType).toBe(DisplayType.TABLE);
- });
- it('resets the fields, yAxis, query, and sort when the dataset is switched', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- title: 'This title should persist',
- description: 'This description should persist',
- dataset: WidgetType.TRANSACTIONS,
- field: ['event.type', 'potato', 'count()'],
- yAxis: ['count()', 'count_unique(user)'],
- query: ['event.type = "test"'],
- sort: ['-testField'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.SPANS,
- });
- });
- expect(result.current.state.title).toBe('This title should persist');
- expect(result.current.state.description).toBe('This description should persist');
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', 'span.duration', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- expect(result.current.state.yAxis).toEqual([]);
- expect(result.current.state.query).toEqual(['']);
- expect(result.current.state.sort).toEqual([
- {
- field: 'count(span.duration)',
- kind: 'desc',
- },
- ]);
- });
- it('resets the yAxis when the dataset is switched from anything to issues', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- dataset: WidgetType.TRANSACTIONS,
- yAxis: ['count()', 'count_unique(user)'],
- displayType: DisplayType.LINE,
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.ISSUE,
- });
- });
- expect(result.current.state.yAxis).toEqual([]);
- });
- it('resets the sort when the display type is switched and the sort is not in the new fields', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.LINE,
- field: ['testField', 'testField2'],
- sort: ['-project.name'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'project.name', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.TABLE,
- });
- });
- expect(result.current.state.sort).toEqual([
- {
- field: 'testField',
- kind: 'desc',
- },
- ]);
- });
- it('keeps sort when the sort is in the new fields', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.LINE,
- field: ['testField', 'testField2'],
- sort: ['-testField'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.TABLE,
- });
- });
- expect(result.current.state.sort).toEqual([
- {
- field: 'testField',
- kind: 'desc',
- },
- ]);
- });
- it('resets selectedAggregate when the dataset is switched', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- selectedAggregate: '0',
- displayType: DisplayType.BIG_NUMBER,
- field: ['count_unique(1)', 'count_unique(2)'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.selectedAggregate).toBe(0);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.SPANS,
- });
- });
- expect(result.current.state.selectedAggregate).toBeUndefined();
- });
- it('resets the sort when the dataset is switched for big number widgets', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- dataset: WidgetType.ERRORS,
- displayType: DisplayType.BIG_NUMBER,
- sort: ['-testField'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.TRANSACTIONS,
- });
- });
- expect(result.current.state.sort).toEqual([]);
- });
- it('resets thresholds when the dataset is switched', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- dataset: WidgetType.ERRORS,
- displayType: DisplayType.BIG_NUMBER,
- thresholds: '{"max_values":{"max1":200,"max2":300},"unit":"milliseconds"}',
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.thresholds).toEqual({
- max_values: {max1: 200, max2: 300},
- unit: 'milliseconds',
- });
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.TRANSACTIONS,
- });
- });
- expect(result.current.state.thresholds).toBeUndefined();
- });
- });
- describe('fields', () => {
- it('returns the fields from the query params', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({query: {field: ['event.type', 'potato', 'count()']}})
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- {field: 'potato', alias: undefined, kind: 'field'},
- {
- alias: undefined,
- kind: 'function',
- function: ['count', '', undefined, undefined],
- },
- ]);
- });
- it('decodes both JSON formatted fields and non-JSON formatted fields', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- field: [
- '{"field": "event.type", "alias": "test"}',
- 'p90(transaction.duration)',
- ],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: 'test', kind: 'field'},
- {
- function: ['p90', 'transaction.duration', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('encodes fields to JSON when they have aliases', () => {
- const fields = [
- {field: 'event.type', alias: 'test', kind: FieldValueKind.FIELD},
- {field: 'event.type', alias: undefined, kind: FieldValueKind.FIELD},
- ] as Column[];
- const encodedFields = serializeFields(fields);
- expect(encodedFields).toEqual([
- '{"field":"event.type","alias":"test"}',
- 'event.type',
- ]);
- });
- it('wipes the alias when the dataset is switched', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- dataset: WidgetType.ERRORS,
- displayType: DisplayType.TABLE,
- field: ['{"field":"event.type","alias":"test"}'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: 'test', kind: FieldValueKind.FIELD},
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DATASET,
- payload: WidgetType.TRANSACTIONS,
- });
- });
- expect(result.current.state.fields).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('wipes the alias when the display type is switched', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.TABLE,
- field: ['{"field":"count()","alias":"test"}'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {function: ['count', '', undefined, undefined], alias: 'test', kind: 'function'},
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_DISPLAY_TYPE,
- payload: DisplayType.LINE,
- });
- });
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('resets the sort when the field that is being sorted is removed', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {field: ['testField'], sort: ['-testField']},
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_FIELDS,
- payload: [{field: 'testField2', kind: FieldValueKind.FIELD}],
- });
- });
- expect(result.current.state.sort).toEqual([{field: 'testField2', kind: 'desc'}]);
- });
- it('modifies the sort when the field that is being sorted is modified', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {field: ['testField', 'sortField'], sort: ['-sortField']},
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'sortField', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_FIELDS,
- payload: [
- {field: 'testField', kind: FieldValueKind.FIELD},
- {field: 'newSortField', kind: FieldValueKind.FIELD},
- ],
- });
- });
- expect(result.current.state.sort).toEqual([{field: 'newSortField', kind: 'desc'}]);
- });
- it('does not reset the table sort for issue widgets', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- dataset: WidgetType.ISSUE,
- field: ['testField'],
- sort: ['-notInFields'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'notInFields', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_FIELDS,
- payload: [{field: 'testField', kind: FieldValueKind.FIELD}],
- });
- });
- expect(result.current.state.sort).toEqual([{field: 'notInFields', kind: 'desc'}]);
- });
- it('adds a default sort when adding a grouping for a timeseries chart', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.LINE,
- field: [],
- yAxis: ['count()'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.yAxis).toEqual([
- {function: ['count', '', undefined, undefined], kind: 'function'},
- ]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_FIELDS,
- payload: [{field: 'browser.name', kind: FieldValueKind.FIELD}],
- });
- });
- // The y-axis takes priority
- expect(result.current.state.sort).toEqual([{field: 'count()', kind: 'desc'}]);
- });
- });
- describe('yAxis', () => {
- it('does not conflict with fields when setting the state', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- field: ['event.type', 'potato', 'count()'],
- yAxis: ['count()', 'count_unique(user)'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.fields).toEqual([
- {field: 'event.type', alias: undefined, kind: 'field'},
- {field: 'potato', alias: undefined, kind: 'field'},
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- expect(result.current.state.yAxis).toEqual([
- {
- function: ['count', '', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- {
- function: ['count_unique', 'user', undefined, undefined],
- alias: undefined,
- kind: 'function',
- },
- ]);
- });
- it('clears the sort when the y-axis changes and there is no grouping', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- displayType: DisplayType.LINE,
- field: [],
- yAxis: ['count()'],
- sort: ['-count()'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'count()', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_Y_AXIS,
- payload: [
- {function: ['count_unique', 'user', undefined, undefined], kind: 'function'},
- ],
- });
- });
- expect(result.current.state.sort).toEqual([]);
- });
- });
- describe('sort', () => {
- it('can decode and update sorts', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- sort: ['-testField'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_SORT,
- payload: [{field: 'testField', kind: 'asc'}],
- });
- });
- expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'asc'}]);
- });
- });
- describe('limit', () => {
- it('can decode and update limit', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- limit: '4',
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.limit).toBe(4);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_LIMIT,
- payload: 10,
- });
- });
- expect(result.current.state.limit).toBe(10);
- });
- });
- describe('legendAlias', () => {
- it('can decode and update legendAlias', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- legendAlias: ['test', 'test2'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.legendAlias).toEqual(['test', 'test2']);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_LEGEND_ALIAS,
- payload: ['test3', 'test4'],
- });
- });
- expect(result.current.state.legendAlias).toEqual(['test3', 'test4']);
- });
- });
- describe('selectedAggregate', () => {
- it('can decode and update selectedAggregate', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- selectedAggregate: '0',
- displayType: DisplayType.BIG_NUMBER,
- field: ['count()', 'count_unique(user)'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.selectedAggregate).toBe(0);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_SELECTED_AGGREGATE,
- payload: 1,
- });
- });
- expect(result.current.state.selectedAggregate).toBe(1);
- });
- it('can set selectedAggregate to undefined in the URL', () => {
- mockedUsedLocation.mockReturnValue(
- LocationFixture({
- query: {
- selectedAggregate: '0',
- displayType: DisplayType.BIG_NUMBER,
- field: ['count()', 'count_unique(user)'],
- },
- })
- );
- const {result} = renderHook(() => useWidgetBuilderState(), {
- wrapper: WidgetBuilderProvider,
- });
- expect(result.current.state.selectedAggregate).toBe(0);
- act(() => {
- result.current.dispatch({
- type: BuilderStateAction.SET_SELECTED_AGGREGATE,
- payload: undefined,
- });
- });
- // If selectedAggregate is undefined in the URL, then the widget builder state
- // will set the selectedAggregate to the last aggregate
- expect(result.current.state.selectedAggregate).toBe(1);
- expect(mockNavigate).toHaveBeenCalledWith(
- expect.objectContaining({
- query: expect.objectContaining({selectedAggregate: undefined}),
- }),
- {replace: true}
- );
- });
- });
- });
|