eventView.spec.jsx 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591
  1. import {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  2. import EventView, {
  3. isAPIPayloadSimilar,
  4. pickRelevantLocationQueryStrings,
  5. } from 'sentry/utils/discover/eventView';
  6. import {
  7. CHART_AXIS_OPTIONS,
  8. DISPLAY_MODE_OPTIONS,
  9. DisplayModes,
  10. } from 'sentry/utils/discover/types';
  11. const generateFields = fields =>
  12. fields.map(field => ({
  13. field,
  14. }));
  15. const generateSorts = sorts =>
  16. sorts.map(sortName => ({
  17. field: sortName,
  18. kind: 'desc',
  19. }));
  20. describe('EventView constructor', function () {
  21. it('instantiates default values', function () {
  22. const eventView = new EventView({});
  23. expect(eventView).toMatchObject({
  24. id: undefined,
  25. name: undefined,
  26. fields: [],
  27. sorts: [],
  28. query: '',
  29. project: [],
  30. start: undefined,
  31. end: undefined,
  32. statsPeriod: undefined,
  33. environment: [],
  34. yAxis: undefined,
  35. display: undefined,
  36. });
  37. });
  38. });
  39. describe('EventView.fromLocation()', function () {
  40. it('maps query strings', function () {
  41. const location = {
  42. query: {
  43. id: '42',
  44. name: 'best query',
  45. field: ['count()', 'id'],
  46. widths: ['123', '456'],
  47. sort: ['title', '-count'],
  48. query: 'event.type:transaction',
  49. project: [123],
  50. team: ['myteams', '1', '2'],
  51. start: '2019-10-01T00:00:00',
  52. end: '2019-10-02T00:00:00',
  53. statsPeriod: '14d',
  54. environment: ['staging'],
  55. yAxis: 'p95',
  56. display: 'previous',
  57. },
  58. };
  59. const eventView = EventView.fromLocation(location);
  60. expect(eventView).toMatchObject({
  61. id: '42',
  62. name: 'best query',
  63. fields: [
  64. {field: 'count()', width: 123},
  65. {field: 'id', width: 456},
  66. ],
  67. sorts: generateSorts(['count']),
  68. query: 'event.type:transaction',
  69. project: [123],
  70. team: ['myteams', 1, 2],
  71. start: undefined,
  72. end: undefined,
  73. statsPeriod: '14d',
  74. environment: ['staging'],
  75. yAxis: 'p95',
  76. display: 'previous',
  77. });
  78. });
  79. it('includes first valid statsPeriod', function () {
  80. const location = {
  81. query: {
  82. id: '42',
  83. name: 'best query',
  84. field: ['count()', 'id'],
  85. widths: ['123', '456'],
  86. sort: ['title', '-count'],
  87. query: 'event.type:transaction',
  88. project: [123],
  89. start: '2019-10-01T00:00:00',
  90. end: '2019-10-02T00:00:00',
  91. statsPeriod: ['invalid', '28d'],
  92. environment: ['staging'],
  93. },
  94. };
  95. const eventView = EventView.fromLocation(location);
  96. expect(eventView).toMatchObject({
  97. id: '42',
  98. name: 'best query',
  99. fields: [
  100. {field: 'count()', width: 123},
  101. {field: 'id', width: 456},
  102. ],
  103. sorts: generateSorts(['count']),
  104. query: 'event.type:transaction',
  105. project: [123],
  106. start: undefined,
  107. end: undefined,
  108. statsPeriod: '28d',
  109. environment: ['staging'],
  110. });
  111. });
  112. it('includes start and end', function () {
  113. const location = {
  114. query: {
  115. id: '42',
  116. name: 'best query',
  117. field: ['count()', 'id'],
  118. widths: ['123', '456'],
  119. sort: ['title', '-count'],
  120. query: 'event.type:transaction',
  121. project: [123],
  122. start: '2019-10-01T00:00:00',
  123. end: '2019-10-02T00:00:00',
  124. environment: ['staging'],
  125. },
  126. };
  127. const eventView = EventView.fromLocation(location);
  128. expect(eventView).toMatchObject({
  129. id: '42',
  130. name: 'best query',
  131. fields: [
  132. {field: 'count()', width: 123},
  133. {field: 'id', width: 456},
  134. ],
  135. sorts: generateSorts(['count']),
  136. query: 'event.type:transaction',
  137. project: [123],
  138. start: '2019-10-01T00:00:00.000',
  139. end: '2019-10-02T00:00:00.000',
  140. environment: ['staging'],
  141. });
  142. });
  143. it('generates event view when there are no query strings', function () {
  144. const location = {
  145. query: {},
  146. };
  147. const eventView = EventView.fromLocation(location);
  148. expect(eventView).toMatchObject({
  149. id: void 0,
  150. name: void 0,
  151. fields: [],
  152. sorts: [],
  153. query: '',
  154. project: [],
  155. start: void 0,
  156. end: void 0,
  157. statsPeriod: '14d',
  158. environment: [],
  159. yAxis: void 0,
  160. });
  161. });
  162. });
  163. describe('EventView.fromSavedQuery()', function () {
  164. it('maps basic properties of saved query', function () {
  165. const saved = {
  166. id: '42',
  167. name: 'best query',
  168. fields: ['count()', 'id'],
  169. query: 'event.type:transaction',
  170. projects: [123],
  171. teams: ['myteams', 1],
  172. range: '14d',
  173. start: '2019-10-01T00:00:00',
  174. end: '2019-10-02T00:00:00',
  175. orderby: '-id',
  176. environment: ['staging'],
  177. display: 'previous',
  178. };
  179. const eventView = EventView.fromSavedQuery(saved);
  180. expect(eventView).toMatchObject({
  181. id: saved.id,
  182. name: saved.name,
  183. fields: [
  184. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  185. {field: 'id', width: COL_WIDTH_UNDEFINED},
  186. ],
  187. sorts: [{field: 'id', kind: 'desc'}],
  188. query: 'event.type:transaction',
  189. project: [123],
  190. team: ['myteams', 1],
  191. start: undefined,
  192. end: undefined,
  193. // statsPeriod has precedence
  194. statsPeriod: '14d',
  195. environment: ['staging'],
  196. yAxis: undefined,
  197. display: 'previous',
  198. });
  199. const eventView2 = EventView.fromSavedQuery({
  200. ...saved,
  201. range: undefined,
  202. });
  203. expect(eventView2).toMatchObject({
  204. id: saved.id,
  205. name: saved.name,
  206. fields: [
  207. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  208. {field: 'id', width: COL_WIDTH_UNDEFINED},
  209. ],
  210. sorts: [{field: 'id', kind: 'desc'}],
  211. query: 'event.type:transaction',
  212. project: [123],
  213. team: ['myteams', 1],
  214. start: '2019-10-01T00:00:00.000',
  215. end: '2019-10-02T00:00:00.000',
  216. statsPeriod: undefined,
  217. environment: ['staging'],
  218. });
  219. });
  220. it('maps saved query with no conditions', function () {
  221. const saved = {
  222. orderby: '-count',
  223. name: 'foo bar',
  224. fields: ['release', 'count()'],
  225. widths: [111, 222],
  226. dateCreated: '2019-10-30T06:13:17.632078Z',
  227. environment: ['dev', 'production'],
  228. version: 2,
  229. createdBy: '1',
  230. dateUpdated: '2019-10-30T06:13:17.632096Z',
  231. id: '5',
  232. projects: [1],
  233. yAxis: 'count()',
  234. };
  235. const eventView = EventView.fromSavedQuery(saved);
  236. const expected = {
  237. id: '5',
  238. name: 'foo bar',
  239. fields: [
  240. {field: 'release', width: 111},
  241. {field: 'count()', width: 222},
  242. ],
  243. sorts: generateSorts(['count']),
  244. query: '',
  245. project: [1],
  246. environment: ['dev', 'production'],
  247. yAxis: 'count()',
  248. };
  249. expect(eventView).toMatchObject(expected);
  250. });
  251. it('maps properties from v2 saved query', function () {
  252. const saved = {
  253. name: 'best query',
  254. fields: ['count()', 'title'],
  255. range: '14d',
  256. start: '',
  257. end: '',
  258. };
  259. const eventView = EventView.fromSavedQuery(saved);
  260. expect(eventView.fields).toEqual([
  261. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  262. {field: 'title', width: COL_WIDTH_UNDEFINED},
  263. ]);
  264. expect(eventView.name).toEqual(saved.name);
  265. expect(eventView.statsPeriod).toEqual('14d');
  266. expect(eventView.start).toEqual(undefined);
  267. expect(eventView.end).toEqual(undefined);
  268. });
  269. it('saved queries are equal when start and end datetime differ in format', function () {
  270. const saved = {
  271. orderby: '-count_timestamp',
  272. end: '2019-10-23T19:27:04+0000',
  273. name: 'release query',
  274. fields: ['release', 'count(timestamp)'],
  275. dateCreated: '2019-10-30T05:10:23.718937Z',
  276. environment: ['dev', 'production'],
  277. start: '2019-10-20T21:02:51+0000',
  278. version: 2,
  279. createdBy: '1',
  280. dateUpdated: '2019-10-30T07:25:58.291917Z',
  281. id: '3',
  282. projects: [1],
  283. };
  284. const eventView = EventView.fromSavedQuery(saved);
  285. const eventView2 = EventView.fromSavedQuery({
  286. ...saved,
  287. start: '2019-10-20T21:02:51Z',
  288. end: '2019-10-23T19:27:04Z',
  289. });
  290. expect(eventView.isEqualTo(eventView2)).toBe(true);
  291. const eventView3 = EventView.fromSavedQuery({
  292. ...saved,
  293. start: '2019-10-20T21:02:51Z',
  294. });
  295. expect(eventView.isEqualTo(eventView3)).toBe(true);
  296. const eventView4 = EventView.fromSavedQuery({
  297. ...saved,
  298. end: '2019-10-23T19:27:04Z',
  299. });
  300. expect(eventView.isEqualTo(eventView4)).toBe(true);
  301. });
  302. it('saved queries are not equal when datetime selection are invalid', function () {
  303. const saved = {
  304. orderby: '-count_timestamp',
  305. end: '2019-10-23T19:27:04+0000',
  306. name: 'release query',
  307. fields: ['release', 'count(timestamp)'],
  308. dateCreated: '2019-10-30T05:10:23.718937Z',
  309. environment: ['dev', 'production'],
  310. start: '2019-10-20T21:02:51+0000',
  311. version: 2,
  312. createdBy: '1',
  313. dateUpdated: '2019-10-30T07:25:58.291917Z',
  314. id: '3',
  315. projects: [1],
  316. };
  317. const eventView = EventView.fromSavedQuery(saved);
  318. const eventView2 = EventView.fromSavedQuery({
  319. ...saved,
  320. start: '',
  321. });
  322. expect(eventView.isEqualTo(eventView2)).toBe(false);
  323. const eventView3 = EventView.fromSavedQuery({
  324. ...saved,
  325. end: '',
  326. });
  327. expect(eventView.isEqualTo(eventView3)).toBe(false);
  328. // this is expected since datetime (start and end) are normalized
  329. expect(eventView2.isEqualTo(eventView3)).toBe(true);
  330. });
  331. it('saved queries with undefined yAxis are defaulted to count() when comparing with isEqualTo', function () {
  332. const saved = {
  333. orderby: '-count_timestamp',
  334. end: '2019-10-23T19:27:04+0000',
  335. name: 'release query',
  336. fields: ['release', 'count(timestamp)'],
  337. dateCreated: '2019-10-30T05:10:23.718937Z',
  338. environment: ['dev', 'production'],
  339. start: '2019-10-20T21:02:51+0000',
  340. version: 2,
  341. createdBy: '1',
  342. dateUpdated: '2019-10-30T07:25:58.291917Z',
  343. id: '3',
  344. projects: [1],
  345. };
  346. const eventView = EventView.fromSavedQuery(saved);
  347. const eventView2 = EventView.fromSavedQuery({
  348. ...saved,
  349. yAxis: 'count()',
  350. });
  351. expect(eventView.isEqualTo(eventView2)).toBe(true);
  352. });
  353. it('uses the first yAxis from the SavedQuery', function () {
  354. const saved = {
  355. id: '42',
  356. name: 'best query',
  357. fields: ['count()', 'id'],
  358. query: 'event.type:transaction',
  359. projects: [123],
  360. teams: ['myteams', 1],
  361. range: '14d',
  362. start: '2019-10-01T00:00:00',
  363. end: '2019-10-02T00:00:00',
  364. orderby: '-id',
  365. environment: ['staging'],
  366. display: 'previous',
  367. yAxis: ['count()'],
  368. };
  369. const eventView = EventView.fromSavedQuery(saved);
  370. expect(eventView).toMatchObject({
  371. id: saved.id,
  372. name: saved.name,
  373. fields: [
  374. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  375. {field: 'id', width: COL_WIDTH_UNDEFINED},
  376. ],
  377. sorts: [{field: 'id', kind: 'desc'}],
  378. query: 'event.type:transaction',
  379. project: [123],
  380. team: ['myteams', 1],
  381. start: undefined,
  382. end: undefined,
  383. statsPeriod: '14d',
  384. environment: ['staging'],
  385. yAxis: 'count()',
  386. display: 'previous',
  387. });
  388. });
  389. it('preserves utc with start/end', function () {
  390. const saved = {
  391. name: 'best query',
  392. query: 'event.type:transaction',
  393. fields: ['count()', 'title'],
  394. start: '2019-10-20T21:02:51+0000',
  395. end: '2019-10-23T19:27:04+0000',
  396. utc: 'true',
  397. };
  398. const eventView = EventView.fromSavedQuery(saved);
  399. expect(eventView).toMatchObject({
  400. id: saved.id,
  401. name: saved.name,
  402. fields: [
  403. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  404. {field: 'title', width: COL_WIDTH_UNDEFINED},
  405. ],
  406. query: 'event.type:transaction',
  407. start: '2019-10-20T21:02:51.000',
  408. end: '2019-10-23T19:27:04.000',
  409. utc: 'true',
  410. });
  411. });
  412. });
  413. describe('EventView.fromNewQueryWithLocation()', function () {
  414. const prebuiltQuery = {
  415. id: undefined,
  416. name: 'All Events',
  417. query: '',
  418. projects: [],
  419. fields: ['title', 'event.type', 'project', 'user', 'timestamp'],
  420. orderby: '-timestamp',
  421. version: 2,
  422. };
  423. it('maps basic properties of a prebuilt query', function () {
  424. const location = {
  425. query: {
  426. statsPeriod: '99d',
  427. },
  428. };
  429. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery, location);
  430. expect(eventView).toMatchObject({
  431. id: undefined,
  432. name: 'All Events',
  433. fields: [
  434. {field: 'title'},
  435. {field: 'event.type'},
  436. {field: 'project'},
  437. {field: 'user'},
  438. {field: 'timestamp'},
  439. ],
  440. sorts: [{field: 'timestamp', kind: 'desc'}],
  441. query: '',
  442. project: [],
  443. start: undefined,
  444. end: undefined,
  445. // statsPeriod has precedence
  446. statsPeriod: '99d',
  447. environment: [],
  448. yAxis: undefined,
  449. });
  450. });
  451. it('merges global selection values', function () {
  452. const location = {
  453. query: {
  454. statsPeriod: '99d',
  455. project: ['456'],
  456. environment: ['prod'],
  457. },
  458. };
  459. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery, location);
  460. expect(eventView).toMatchObject({
  461. id: undefined,
  462. name: 'All Events',
  463. fields: [
  464. {field: 'title'},
  465. {field: 'event.type'},
  466. {field: 'project'},
  467. {field: 'user'},
  468. {field: 'timestamp'},
  469. ],
  470. sorts: [{field: 'timestamp', kind: 'desc'}],
  471. query: '',
  472. project: [456],
  473. start: undefined,
  474. end: undefined,
  475. statsPeriod: '99d',
  476. environment: ['prod'],
  477. yAxis: undefined,
  478. });
  479. });
  480. it('new query takes precedence over global selection values', function () {
  481. const location = {
  482. query: {
  483. statsPeriod: '99d',
  484. project: ['456'],
  485. environment: ['prod'],
  486. },
  487. };
  488. const prebuiltQuery2 = {
  489. ...prebuiltQuery,
  490. range: '42d',
  491. projects: [987],
  492. environment: ['staging'],
  493. };
  494. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery2, location);
  495. expect(eventView).toMatchObject({
  496. id: undefined,
  497. name: 'All Events',
  498. fields: [
  499. {field: 'title'},
  500. {field: 'event.type'},
  501. {field: 'project'},
  502. {field: 'user'},
  503. {field: 'timestamp'},
  504. ],
  505. sorts: [{field: 'timestamp', kind: 'desc'}],
  506. query: '',
  507. project: [987],
  508. start: undefined,
  509. end: undefined,
  510. statsPeriod: '42d',
  511. environment: ['staging'],
  512. yAxis: undefined,
  513. });
  514. // also test start and end
  515. const location2 = {
  516. query: {
  517. start: '2019-10-01T00:00:00',
  518. end: '2019-10-02T00:00:00',
  519. project: ['456'],
  520. environment: ['prod'],
  521. },
  522. };
  523. const prebuiltQuery3 = {
  524. ...prebuiltQuery,
  525. start: '2019-10-01T00:00:00',
  526. end: '2019-10-02T00:00:00',
  527. projects: [987],
  528. environment: ['staging'],
  529. };
  530. const eventView2 = EventView.fromNewQueryWithLocation(prebuiltQuery3, location2);
  531. expect(eventView2).toMatchObject({
  532. id: undefined,
  533. name: 'All Events',
  534. fields: [
  535. {field: 'title'},
  536. {field: 'event.type'},
  537. {field: 'project'},
  538. {field: 'user'},
  539. {field: 'timestamp'},
  540. ],
  541. sorts: [{field: 'timestamp', kind: 'desc'}],
  542. query: '',
  543. project: [987],
  544. start: '2019-10-01T00:00:00.000',
  545. end: '2019-10-02T00:00:00.000',
  546. statsPeriod: undefined,
  547. environment: ['staging'],
  548. yAxis: undefined,
  549. });
  550. });
  551. });
  552. describe('EventView.fromSavedQueryOrLocation()', function () {
  553. it('maps basic properties of saved query', function () {
  554. const saved = {
  555. id: '42',
  556. name: 'best query',
  557. fields: ['count()', 'id'],
  558. query: 'event.type:transaction',
  559. projects: [123],
  560. range: '14d',
  561. start: '2019-10-01T00:00:00',
  562. end: '2019-10-02T00:00:00',
  563. orderby: '-id',
  564. environment: ['staging'],
  565. display: 'previous',
  566. };
  567. const location = {
  568. query: {
  569. statsPeriod: '14d',
  570. project: ['123'],
  571. team: ['myteams', '1', '2'],
  572. environment: ['staging'],
  573. },
  574. };
  575. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  576. expect(eventView).toMatchObject({
  577. id: saved.id,
  578. name: saved.name,
  579. fields: [
  580. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  581. {field: 'id', width: COL_WIDTH_UNDEFINED},
  582. ],
  583. sorts: [{field: 'id', kind: 'desc'}],
  584. query: 'event.type:transaction',
  585. project: [123],
  586. team: ['myteams', 1, 2],
  587. start: undefined,
  588. end: undefined,
  589. // statsPeriod has precedence
  590. statsPeriod: '14d',
  591. environment: ['staging'],
  592. yAxis: undefined,
  593. display: 'previous',
  594. });
  595. const savedQuery2 = {...saved, range: undefined};
  596. const location2 = {
  597. query: {
  598. project: ['123'],
  599. environment: ['staging'],
  600. start: '2019-10-01T00:00:00',
  601. end: '2019-10-02T00:00:00',
  602. },
  603. };
  604. const eventView2 = EventView.fromSavedQueryOrLocation(savedQuery2, location2);
  605. expect(eventView2).toMatchObject({
  606. id: saved.id,
  607. name: saved.name,
  608. fields: [
  609. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  610. {field: 'id', width: COL_WIDTH_UNDEFINED},
  611. ],
  612. sorts: [{field: 'id', kind: 'desc'}],
  613. query: 'event.type:transaction',
  614. project: [123],
  615. start: '2019-10-01T00:00:00.000',
  616. end: '2019-10-02T00:00:00.000',
  617. statsPeriod: undefined,
  618. environment: ['staging'],
  619. });
  620. });
  621. it('overrides saved query params with location params', function () {
  622. const saved = {
  623. id: '42',
  624. name: 'best query',
  625. fields: ['count()', 'id'],
  626. query: 'event.type:transaction',
  627. projects: [123],
  628. range: '14d',
  629. start: '2019-10-01T00:00:00',
  630. end: '2019-10-02T00:00:00',
  631. orderby: '-id',
  632. environment: ['staging'],
  633. display: 'previous',
  634. };
  635. const location = {
  636. query: {
  637. id: '42',
  638. statsPeriod: '7d',
  639. project: ['3'],
  640. },
  641. };
  642. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  643. expect(eventView).toMatchObject({
  644. id: saved.id,
  645. name: saved.name,
  646. fields: [
  647. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  648. {field: 'id', width: COL_WIDTH_UNDEFINED},
  649. ],
  650. sorts: [{field: 'id', kind: 'desc'}],
  651. query: 'event.type:transaction',
  652. project: [3],
  653. start: undefined,
  654. end: undefined,
  655. // statsPeriod has precedence
  656. statsPeriod: '7d',
  657. environment: [],
  658. yAxis: undefined,
  659. display: 'previous',
  660. });
  661. });
  662. it('maps saved query with no conditions', function () {
  663. const saved = {
  664. orderby: '-count',
  665. name: 'foo bar',
  666. fields: ['release', 'count()'],
  667. widths: [111, 222],
  668. dateCreated: '2019-10-30T06:13:17.632078Z',
  669. query: '',
  670. environment: [],
  671. version: 2,
  672. createdBy: '1',
  673. dateUpdated: '2019-10-30T06:13:17.632096Z',
  674. id: '5',
  675. yAxis: 'count()',
  676. };
  677. const location = {
  678. query: {
  679. id: '5',
  680. project: [1],
  681. },
  682. };
  683. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  684. const expected = {
  685. id: '5',
  686. name: 'foo bar',
  687. fields: [
  688. {field: 'release', width: 111},
  689. {field: 'count()', width: 222},
  690. ],
  691. sorts: generateSorts(['count']),
  692. query: '',
  693. project: [1],
  694. yAxis: 'count()',
  695. };
  696. expect(eventView).toMatchObject(expected);
  697. });
  698. it('maps query with cleared conditions', function () {
  699. const saved = {
  700. id: '42',
  701. name: 'best query',
  702. fields: ['count()', 'id'],
  703. query: 'event.type:transaction',
  704. projects: [123],
  705. range: '14d',
  706. start: '2019-10-01T00:00:00',
  707. end: '2019-10-02T00:00:00',
  708. orderby: '-id',
  709. environment: ['staging'],
  710. display: 'previous',
  711. };
  712. const location = {
  713. query: {
  714. id: '42',
  715. statsPeriod: '7d',
  716. },
  717. };
  718. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  719. expect(eventView).toMatchObject({
  720. id: saved.id,
  721. name: saved.name,
  722. fields: [
  723. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  724. {field: 'id', width: COL_WIDTH_UNDEFINED},
  725. ],
  726. sorts: [{field: 'id', kind: 'desc'}],
  727. query: 'event.type:transaction',
  728. start: undefined,
  729. end: undefined,
  730. // statsPeriod has precedence
  731. statsPeriod: '7d',
  732. environment: [],
  733. yAxis: undefined,
  734. display: 'previous',
  735. });
  736. const location2 = {
  737. query: {
  738. id: '42',
  739. statsPeriod: '7d',
  740. query: '',
  741. },
  742. };
  743. const eventView2 = EventView.fromSavedQueryOrLocation(saved, location2);
  744. expect(eventView2).toMatchObject({
  745. id: saved.id,
  746. name: saved.name,
  747. fields: [
  748. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  749. {field: 'id', width: COL_WIDTH_UNDEFINED},
  750. ],
  751. sorts: [{field: 'id', kind: 'desc'}],
  752. query: '',
  753. start: undefined,
  754. end: undefined,
  755. // statsPeriod has precedence
  756. statsPeriod: '7d',
  757. environment: [],
  758. yAxis: undefined,
  759. display: 'previous',
  760. });
  761. });
  762. it('event views are equal when start and end datetime differ in format', function () {
  763. const saved = {
  764. orderby: '-count_timestamp',
  765. end: '2019-10-23T19:27:04+0000',
  766. name: 'release query',
  767. fields: ['release', 'count(timestamp)'],
  768. dateCreated: '2019-10-30T05:10:23.718937Z',
  769. environment: ['dev', 'production'],
  770. start: '2019-10-20T21:02:51+0000',
  771. version: 2,
  772. createdBy: '1',
  773. dateUpdated: '2019-10-30T07:25:58.291917Z',
  774. id: '3',
  775. };
  776. const location = {
  777. query: {
  778. id: '3',
  779. start: '2019-10-20T21:02:51+0000',
  780. end: '2019-10-23T19:27:04+0000',
  781. },
  782. };
  783. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  784. const location2 = {
  785. query: {
  786. id: '3',
  787. start: '2019-10-20T21:02:51Z',
  788. end: '2019-10-23T19:27:04Z',
  789. },
  790. };
  791. const eventView2 = EventView.fromSavedQueryOrLocation(saved, location2);
  792. expect(eventView.isEqualTo(eventView2)).toBe(true);
  793. const location3 = {
  794. query: {
  795. id: '3',
  796. start: '2019-10-20T21:02:51Z',
  797. end: '2019-10-23T19:27:04+0000',
  798. },
  799. };
  800. const eventView3 = EventView.fromSavedQueryOrLocation(saved, location3);
  801. expect(eventView.isEqualTo(eventView3)).toBe(true);
  802. const location4 = {
  803. query: {
  804. id: '3',
  805. start: '2019-10-20T21:02:51+0000',
  806. end: '2019-10-23T19:27:04Z',
  807. },
  808. };
  809. const eventView4 = EventView.fromSavedQueryOrLocation(saved, location4);
  810. expect(eventView.isEqualTo(eventView4)).toBe(true);
  811. });
  812. it('event views are not equal when datetime selection are invalid', function () {
  813. const saved = {
  814. orderby: '-count_timestamp',
  815. end: '2019-10-23T19:27:04+0000',
  816. name: 'release query',
  817. fields: ['release', 'count(timestamp)'],
  818. dateCreated: '2019-10-30T05:10:23.718937Z',
  819. environment: ['dev', 'production'],
  820. start: '2019-10-20T21:02:51+0000',
  821. version: 2,
  822. createdBy: '1',
  823. dateUpdated: '2019-10-30T07:25:58.291917Z',
  824. id: '3',
  825. projects: [1],
  826. };
  827. const location = {
  828. query: {
  829. id: '3',
  830. end: '2019-10-23T19:27:04+0000',
  831. start: '2019-10-20T21:02:51+0000',
  832. },
  833. };
  834. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  835. const location2 = {
  836. query: {
  837. id: '3',
  838. end: '2019-10-23T19:27:04+0000',
  839. start: '',
  840. },
  841. };
  842. const eventView2 = EventView.fromSavedQueryOrLocation(saved, location2);
  843. expect(eventView.isEqualTo(eventView2)).toBe(false);
  844. const location3 = {
  845. query: {
  846. id: '3',
  847. end: '',
  848. start: '2019-10-20T21:02:51+0000',
  849. },
  850. };
  851. const eventView3 = EventView.fromSavedQueryOrLocation(saved, location3);
  852. expect(eventView.isEqualTo(eventView3)).toBe(false);
  853. // this is expected since datetime (start and end) are normalized
  854. expect(eventView2.isEqualTo(eventView3)).toBe(true);
  855. });
  856. it('uses the first yAxis from the SavedQuery', function () {
  857. const saved = {
  858. id: '42',
  859. name: 'best query',
  860. fields: ['count()', 'id'],
  861. query: 'event.type:transaction',
  862. projects: [123],
  863. range: '14d',
  864. start: '2019-10-01T00:00:00',
  865. end: '2019-10-02T00:00:00',
  866. orderby: '-id',
  867. environment: ['staging'],
  868. display: 'previous',
  869. yAxis: ['count()', 'failure_count()'],
  870. };
  871. const location = {
  872. query: {
  873. statsPeriod: '14d',
  874. project: ['123'],
  875. team: ['myteams', '1', '2'],
  876. environment: ['staging'],
  877. },
  878. };
  879. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  880. expect(eventView).toMatchObject({
  881. id: saved.id,
  882. name: saved.name,
  883. fields: [
  884. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  885. {field: 'id', width: COL_WIDTH_UNDEFINED},
  886. ],
  887. sorts: [{field: 'id', kind: 'desc'}],
  888. query: 'event.type:transaction',
  889. project: [123],
  890. team: ['myteams', 1, 2],
  891. start: undefined,
  892. end: undefined,
  893. statsPeriod: '14d',
  894. environment: ['staging'],
  895. yAxis: 'count()',
  896. display: 'previous',
  897. });
  898. });
  899. it('filters out invalid teams', function () {
  900. const eventView = EventView.fromSavedQueryOrLocation(undefined, {
  901. query: {
  902. statsPeriod: '14d',
  903. project: ['123'],
  904. team: ['myteams', '1', 'unassigned'],
  905. environment: ['staging'],
  906. },
  907. });
  908. expect(eventView.team).toEqual(['myteams', 1]);
  909. });
  910. });
  911. describe('EventView.generateQueryStringObject()', function () {
  912. it('skips empty values', function () {
  913. const eventView = new EventView({
  914. fields: generateFields(['id', 'title']),
  915. sorts: [],
  916. project: [],
  917. environment: '',
  918. statsPeriod: '',
  919. start: null,
  920. end: undefined,
  921. yAxis: undefined,
  922. display: 'previous',
  923. });
  924. const expected = {
  925. id: undefined,
  926. name: undefined,
  927. field: ['id', 'title'],
  928. widths: [],
  929. sort: [],
  930. query: '',
  931. project: [],
  932. environment: [],
  933. display: 'previous',
  934. yAxis: 'count()',
  935. };
  936. expect(eventView.generateQueryStringObject()).toEqual(expected);
  937. });
  938. it('generates query string object', function () {
  939. const state = {
  940. id: '1234',
  941. name: 'best query',
  942. fields: [
  943. {field: 'count()', width: 123},
  944. {field: 'project.id', width: 456},
  945. ],
  946. sorts: generateSorts(['count']),
  947. query: 'event.type:error',
  948. project: [42],
  949. start: '2019-10-01T00:00:00',
  950. end: '2019-10-02T00:00:00',
  951. statsPeriod: '14d',
  952. environment: ['staging'],
  953. yAxis: 'count()',
  954. display: 'releases',
  955. interval: '1m',
  956. };
  957. const eventView = new EventView(state);
  958. const expected = {
  959. id: '1234',
  960. name: 'best query',
  961. field: ['count()', 'project.id'],
  962. widths: [123, 456],
  963. sort: ['-count'],
  964. query: 'event.type:error',
  965. project: [42],
  966. start: '2019-10-01T00:00:00',
  967. end: '2019-10-02T00:00:00',
  968. statsPeriod: '14d',
  969. environment: ['staging'],
  970. yAxis: 'count()',
  971. display: 'releases',
  972. interval: '1m',
  973. };
  974. expect(eventView.generateQueryStringObject()).toEqual(expected);
  975. });
  976. it('encodes fields', function () {
  977. const eventView = new EventView({
  978. fields: [{field: 'id'}, {field: 'title'}],
  979. sorts: [],
  980. });
  981. const query = eventView.generateQueryStringObject();
  982. expect(query.field).toEqual(['id', 'title']);
  983. });
  984. it('returns a copy of data preventing mutation', function () {
  985. const eventView = new EventView({
  986. fields: [{field: 'id'}, {field: 'title'}],
  987. sorts: [],
  988. });
  989. const query = eventView.generateQueryStringObject();
  990. query.field.push('newthing');
  991. // Getting the query again should return the original values.
  992. const secondQuery = eventView.generateQueryStringObject();
  993. expect(secondQuery.field).toEqual(['id', 'title']);
  994. expect(query).not.toEqual(secondQuery);
  995. });
  996. });
  997. describe('EventView.getEventsAPIPayload()', function () {
  998. it('generates the API payload', function () {
  999. const eventView = new EventView({
  1000. id: 34,
  1001. name: 'amazing query',
  1002. fields: generateFields(['id']),
  1003. sorts: generateSorts(['id']),
  1004. query: 'event.type:csp',
  1005. project: [567],
  1006. environment: ['prod'],
  1007. yAxis: 'users',
  1008. display: 'releases',
  1009. });
  1010. expect(eventView.getEventsAPIPayload({})).toEqual({
  1011. field: ['id'],
  1012. per_page: 50,
  1013. sort: '-id',
  1014. query: 'event.type:csp',
  1015. project: ['567'],
  1016. environment: ['prod'],
  1017. statsPeriod: '14d',
  1018. });
  1019. });
  1020. it('does not append query conditions in location', function () {
  1021. const eventView = new EventView({
  1022. fields: generateFields(['id']),
  1023. sorts: [],
  1024. query: 'event.type:csp',
  1025. });
  1026. const location = {
  1027. query: {
  1028. query: 'TypeError',
  1029. },
  1030. };
  1031. expect(eventView.getEventsAPIPayload(location).query).toEqual('event.type:csp');
  1032. });
  1033. it('only includes at most one sort key', function () {
  1034. const eventView = new EventView({
  1035. fields: generateFields(['count()', 'title']),
  1036. sorts: generateSorts(['title', 'count']),
  1037. query: 'event.type:csp',
  1038. });
  1039. const location = {
  1040. query: {},
  1041. };
  1042. expect(eventView.getEventsAPIPayload(location).sort).toEqual('-title');
  1043. });
  1044. it('only includes sort keys that are defined in fields', function () {
  1045. const eventView = new EventView({
  1046. fields: generateFields(['title', 'count()']),
  1047. sorts: generateSorts(['project', 'count']),
  1048. query: 'event.type:csp',
  1049. });
  1050. const location = {
  1051. query: {},
  1052. };
  1053. expect(eventView.getEventsAPIPayload(location).sort).toEqual('-count');
  1054. });
  1055. it('only includes relevant query strings', function () {
  1056. const eventView = new EventView({
  1057. fields: generateFields(['title', 'count()']),
  1058. sorts: generateSorts(['project', 'count']),
  1059. query: 'event.type:csp',
  1060. });
  1061. const location = {
  1062. query: {
  1063. start: '2020-08-12 12:13:14',
  1064. end: '2020-08-26 12:13:14',
  1065. utc: 'true',
  1066. statsPeriod: '14d',
  1067. cursor: 'some cursor',
  1068. yAxis: 'count()',
  1069. // irrelevant query strings
  1070. bestCountry: 'canada',
  1071. project: '1234',
  1072. environment: ['staging'],
  1073. },
  1074. };
  1075. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1076. project: [],
  1077. environment: [],
  1078. utc: 'true',
  1079. statsPeriod: '14d',
  1080. field: ['title', 'count()'],
  1081. per_page: 50,
  1082. query: 'event.type:csp',
  1083. sort: '-count',
  1084. cursor: 'some cursor',
  1085. });
  1086. });
  1087. it('includes default coerced statsPeriod when omitted or is invalid', function () {
  1088. const eventView = new EventView({
  1089. fields: generateFields(['title', 'count()']),
  1090. sorts: generateSorts(['project', 'count']),
  1091. query: 'event.type:csp',
  1092. project: [1234],
  1093. environment: ['staging'],
  1094. });
  1095. const location = {
  1096. query: {
  1097. start: '',
  1098. end: '',
  1099. utc: 'true',
  1100. // invalid statsPeriod string
  1101. statsPeriod: 'invalid',
  1102. cursor: 'some cursor',
  1103. },
  1104. };
  1105. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1106. project: ['1234'],
  1107. environment: ['staging'],
  1108. utc: 'true',
  1109. statsPeriod: '14d',
  1110. field: ['title', 'count()'],
  1111. per_page: 50,
  1112. query: 'event.type:csp',
  1113. sort: '-count',
  1114. cursor: 'some cursor',
  1115. });
  1116. const location2 = {
  1117. query: {
  1118. start: '',
  1119. end: '',
  1120. utc: 'true',
  1121. // statsPeriod is omitted here
  1122. cursor: 'some cursor',
  1123. },
  1124. };
  1125. expect(eventView.getEventsAPIPayload(location2)).toEqual({
  1126. project: ['1234'],
  1127. environment: ['staging'],
  1128. utc: 'true',
  1129. statsPeriod: '14d',
  1130. field: ['title', 'count()'],
  1131. per_page: 50,
  1132. query: 'event.type:csp',
  1133. sort: '-count',
  1134. cursor: 'some cursor',
  1135. });
  1136. });
  1137. it('includes default coerced statsPeriod when either start or end is only provided', function () {
  1138. const eventView = new EventView({
  1139. fields: generateFields(['title', 'count()']),
  1140. sorts: generateSorts(['project', 'count']),
  1141. query: 'event.type:csp',
  1142. project: [1234],
  1143. environment: ['staging'],
  1144. });
  1145. const location = {
  1146. query: {
  1147. start: '',
  1148. utc: 'true',
  1149. statsPeriod: 'invalid',
  1150. cursor: 'some cursor',
  1151. },
  1152. };
  1153. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1154. project: ['1234'],
  1155. environment: ['staging'],
  1156. utc: 'true',
  1157. statsPeriod: '14d',
  1158. field: ['title', 'count()'],
  1159. per_page: 50,
  1160. query: 'event.type:csp',
  1161. sort: '-count',
  1162. cursor: 'some cursor',
  1163. });
  1164. const location2 = {
  1165. query: {
  1166. end: '',
  1167. utc: 'true',
  1168. statsPeriod: 'invalid',
  1169. cursor: 'some cursor',
  1170. },
  1171. };
  1172. expect(eventView.getEventsAPIPayload(location2)).toEqual({
  1173. project: ['1234'],
  1174. environment: ['staging'],
  1175. utc: 'true',
  1176. statsPeriod: '14d',
  1177. field: ['title', 'count()'],
  1178. per_page: 50,
  1179. query: 'event.type:csp',
  1180. sort: '-count',
  1181. cursor: 'some cursor',
  1182. });
  1183. });
  1184. it('includes start and end', function () {
  1185. const eventView = new EventView({
  1186. fields: generateFields(['title', 'count()']),
  1187. sorts: generateSorts(['count']),
  1188. query: 'event.type:csp',
  1189. start: '2019-10-01T00:00:00',
  1190. end: '2019-10-02T00:00:00',
  1191. environment: [],
  1192. project: [],
  1193. });
  1194. const location = {
  1195. query: {
  1196. // these should not be part of the API payload
  1197. statsPeriod: '55d',
  1198. period: '55d',
  1199. },
  1200. };
  1201. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1202. field: ['title', 'count()'],
  1203. sort: '-count',
  1204. query: 'event.type:csp',
  1205. start: '2019-10-01T00:00:00.000',
  1206. end: '2019-10-02T00:00:00.000',
  1207. per_page: 50,
  1208. project: [],
  1209. environment: [],
  1210. });
  1211. });
  1212. it("an eventview's date selection has higher precedence than the date selection in the query string", function () {
  1213. const initialState = {
  1214. fields: generateFields(['title', 'count()']),
  1215. sorts: generateSorts(['count']),
  1216. query: 'event.type:csp',
  1217. environment: [],
  1218. project: [],
  1219. };
  1220. const output = {
  1221. field: ['title', 'count()'],
  1222. sort: '-count',
  1223. query: 'event.type:csp',
  1224. per_page: 50,
  1225. project: [],
  1226. environment: [],
  1227. };
  1228. // eventview's statsPeriod has highest precedence
  1229. let eventView = new EventView({
  1230. ...initialState,
  1231. statsPeriod: '90d',
  1232. start: '2019-10-01T00:00:00',
  1233. end: '2019-10-02T00:00:00',
  1234. });
  1235. let location = {
  1236. query: {
  1237. // these should not be part of the API payload
  1238. statsPeriod: '55d',
  1239. period: '30d',
  1240. start: '2020-10-01T00:00:00',
  1241. end: '2020-10-02T00:00:00',
  1242. },
  1243. };
  1244. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1245. ...output,
  1246. statsPeriod: '90d',
  1247. });
  1248. // eventview's start/end has higher precedence than the date selection in the query string
  1249. eventView = new EventView({
  1250. ...initialState,
  1251. start: '2019-10-01T00:00:00',
  1252. end: '2019-10-02T00:00:00',
  1253. });
  1254. location = {
  1255. query: {
  1256. // these should not be part of the API payload
  1257. statsPeriod: '55d',
  1258. period: '30d',
  1259. start: '2020-10-01T00:00:00',
  1260. end: '2020-10-02T00:00:00',
  1261. },
  1262. };
  1263. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1264. ...output,
  1265. start: '2019-10-01T00:00:00.000',
  1266. end: '2019-10-02T00:00:00.000',
  1267. });
  1268. // the date selection in the query string should be applied as expected
  1269. eventView = new EventView(initialState);
  1270. location = {
  1271. query: {
  1272. statsPeriod: '55d',
  1273. period: '30d',
  1274. start: '2020-10-01T00:00:00',
  1275. end: '2020-10-02T00:00:00',
  1276. },
  1277. };
  1278. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1279. ...output,
  1280. statsPeriod: '55d',
  1281. });
  1282. location = {
  1283. query: {
  1284. period: '30d',
  1285. start: '2020-10-01T00:00:00',
  1286. end: '2020-10-02T00:00:00',
  1287. },
  1288. };
  1289. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1290. ...output,
  1291. statsPeriod: '30d',
  1292. });
  1293. location = {
  1294. query: {
  1295. start: '2020-10-01T00:00:00',
  1296. end: '2020-10-02T00:00:00',
  1297. },
  1298. };
  1299. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1300. ...output,
  1301. start: '2020-10-01T00:00:00.000',
  1302. end: '2020-10-02T00:00:00.000',
  1303. });
  1304. });
  1305. });
  1306. describe('EventView.getFacetsAPIPayload()', function () {
  1307. it('only includes relevant query strings', function () {
  1308. const eventView = new EventView({
  1309. fields: generateFields(['title', 'count()']),
  1310. sorts: generateSorts(['project', 'count']),
  1311. query: 'event.type:csp',
  1312. });
  1313. const location = {
  1314. query: {
  1315. start: '',
  1316. end: '',
  1317. utc: 'true',
  1318. statsPeriod: '14d',
  1319. // irrelevant query strings
  1320. bestCountry: 'canada',
  1321. cursor: 'some cursor',
  1322. sort: 'the world',
  1323. project: '1234',
  1324. environment: ['staging'],
  1325. display: 'releases',
  1326. },
  1327. };
  1328. expect(eventView.getFacetsAPIPayload(location)).toEqual({
  1329. project: [],
  1330. environment: [],
  1331. utc: 'true',
  1332. statsPeriod: '14d',
  1333. query: 'event.type:csp',
  1334. });
  1335. });
  1336. });
  1337. describe('EventView.toNewQuery()', function () {
  1338. const state = {
  1339. id: '1234',
  1340. name: 'best query',
  1341. fields: [
  1342. {field: 'count()', width: 123},
  1343. {field: 'project.id', width: 456},
  1344. ],
  1345. sorts: generateSorts(['count']),
  1346. query: 'event.type:error',
  1347. project: [42],
  1348. start: '2019-10-01T00:00:00',
  1349. end: '2019-10-02T00:00:00',
  1350. statsPeriod: '14d',
  1351. environment: ['staging'],
  1352. display: 'releases',
  1353. };
  1354. it('outputs the right fields', function () {
  1355. const eventView = new EventView(state);
  1356. const output = eventView.toNewQuery();
  1357. const expected = {
  1358. version: 2,
  1359. id: '1234',
  1360. name: 'best query',
  1361. fields: ['count()', 'project.id'],
  1362. widths: ['123', '456'],
  1363. orderby: '-count',
  1364. query: 'event.type:error',
  1365. projects: [42],
  1366. start: '2019-10-01T00:00:00',
  1367. end: '2019-10-02T00:00:00',
  1368. range: '14d',
  1369. environment: ['staging'],
  1370. display: 'releases',
  1371. };
  1372. expect(output).toEqual(expected);
  1373. });
  1374. it('omits query when query is an empty string', function () {
  1375. const modifiedState = {
  1376. ...state,
  1377. };
  1378. modifiedState.query = '';
  1379. const eventView = new EventView(modifiedState);
  1380. const output = eventView.toNewQuery();
  1381. const expected = {
  1382. version: 2,
  1383. id: '1234',
  1384. name: 'best query',
  1385. fields: ['count()', 'project.id'],
  1386. widths: ['123', '456'],
  1387. orderby: '-count',
  1388. projects: [42],
  1389. start: '2019-10-01T00:00:00',
  1390. end: '2019-10-02T00:00:00',
  1391. range: '14d',
  1392. environment: ['staging'],
  1393. display: 'releases',
  1394. };
  1395. expect(output).toEqual(expected);
  1396. });
  1397. it('omits query when query is not defined', function () {
  1398. const modifiedState = {
  1399. ...state,
  1400. };
  1401. delete modifiedState.query;
  1402. const eventView = new EventView(modifiedState);
  1403. const output = eventView.toNewQuery();
  1404. const expected = {
  1405. version: 2,
  1406. id: '1234',
  1407. name: 'best query',
  1408. fields: ['count()', 'project.id'],
  1409. widths: ['123', '456'],
  1410. orderby: '-count',
  1411. projects: [42],
  1412. start: '2019-10-01T00:00:00',
  1413. end: '2019-10-02T00:00:00',
  1414. range: '14d',
  1415. environment: ['staging'],
  1416. display: 'releases',
  1417. };
  1418. expect(output).toEqual(expected);
  1419. });
  1420. });
  1421. describe('EventView.isValid()', function () {
  1422. it('event view is valid when there is at least one field', function () {
  1423. const eventView = new EventView({
  1424. fields: [{field: 'count()'}, {field: 'project.id'}],
  1425. sorts: [],
  1426. project: [],
  1427. });
  1428. expect(eventView.isValid()).toBe(true);
  1429. });
  1430. it('event view is not valid when there are no fields', function () {
  1431. const eventView = new EventView({
  1432. fields: [],
  1433. sorts: [],
  1434. project: [],
  1435. });
  1436. expect(eventView.isValid()).toBe(false);
  1437. });
  1438. });
  1439. describe('EventView.getWidths()', function () {
  1440. it('returns widths', function () {
  1441. const eventView = new EventView({
  1442. fields: [
  1443. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1444. {field: 'project.id', width: 2020},
  1445. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1446. {field: 'time', width: 420},
  1447. {field: 'lcp', width: 69},
  1448. {field: 'lcp', width: COL_WIDTH_UNDEFINED},
  1449. {field: 'fcp', width: COL_WIDTH_UNDEFINED},
  1450. {field: 'cls', width: COL_WIDTH_UNDEFINED},
  1451. ],
  1452. sorts: [],
  1453. project: [],
  1454. });
  1455. expect(eventView.getWidths()).toEqual([
  1456. COL_WIDTH_UNDEFINED,
  1457. 2020,
  1458. COL_WIDTH_UNDEFINED,
  1459. 420,
  1460. 69,
  1461. ]);
  1462. });
  1463. });
  1464. describe('EventView.getFields()', function () {
  1465. it('returns fields', function () {
  1466. const eventView = new EventView({
  1467. fields: [{field: 'count()'}, {field: 'project.id'}],
  1468. sorts: [],
  1469. project: [],
  1470. });
  1471. expect(eventView.getFields()).toEqual(['count()', 'project.id']);
  1472. });
  1473. });
  1474. describe('EventView.numOfColumns()', function () {
  1475. it('returns correct number of columns', function () {
  1476. // has columns
  1477. const eventView = new EventView({
  1478. fields: [{field: 'count()'}, {field: 'project.id'}],
  1479. sorts: [],
  1480. project: [],
  1481. });
  1482. expect(eventView.numOfColumns()).toBe(2);
  1483. // has no columns
  1484. const eventView2 = new EventView({
  1485. fields: [],
  1486. sorts: [],
  1487. project: [],
  1488. });
  1489. expect(eventView2.numOfColumns()).toBe(0);
  1490. });
  1491. });
  1492. describe('EventView.getDays()', function () {
  1493. it('returns the right number of days for statsPeriod', function () {
  1494. const eventView = new EventView({
  1495. statsPeriod: '14d',
  1496. });
  1497. expect(eventView.getDays()).toBe(14);
  1498. const eventView2 = new EventView({
  1499. statsPeriod: '12h',
  1500. });
  1501. expect(eventView2.getDays()).toBe(0.5);
  1502. });
  1503. it('returns the right number of days for start/end', function () {
  1504. const eventView = new EventView({
  1505. start: '2019-10-01T00:00:00',
  1506. end: '2019-10-02T00:00:00',
  1507. });
  1508. expect(eventView.getDays()).toBe(1);
  1509. const eventView2 = new EventView({
  1510. start: '2019-10-01T00:00:00',
  1511. end: '2019-10-15T00:00:00',
  1512. });
  1513. expect(eventView2.getDays()).toBe(14);
  1514. });
  1515. });
  1516. describe('EventView.clone()', function () {
  1517. it('returns a unique instance', function () {
  1518. const state = {
  1519. id: '1234',
  1520. name: 'best query',
  1521. fields: [{field: 'count()'}, {field: 'project.id'}],
  1522. sorts: generateSorts(['count']),
  1523. query: 'event.type:error',
  1524. project: [42],
  1525. start: '2019-10-01T00:00:00',
  1526. end: '2019-10-02T00:00:00',
  1527. statsPeriod: '14d',
  1528. environment: ['staging'],
  1529. interval: '5m',
  1530. display: 'releases',
  1531. };
  1532. const eventView = new EventView(state);
  1533. const eventView2 = eventView.clone();
  1534. expect(eventView2 !== eventView).toBeTruthy();
  1535. expect(eventView).toMatchObject(state);
  1536. expect(eventView2).toMatchObject(state);
  1537. expect(eventView.isEqualTo(eventView2)).toBe(true);
  1538. expect(
  1539. eventView.additionalConditions === eventView2.additionalConditions
  1540. ).toBeFalsy();
  1541. });
  1542. });
  1543. describe('EventView.withColumns()', function () {
  1544. const state = {
  1545. id: '1234',
  1546. name: 'best query',
  1547. fields: [
  1548. {field: 'count()', width: 30},
  1549. {field: 'project.id', width: 99},
  1550. {field: 'failure_count()', width: 30},
  1551. ],
  1552. yAxis: 'failure_count()',
  1553. sorts: generateSorts(['count']),
  1554. query: 'event.type:error',
  1555. project: [42],
  1556. start: '2019-10-01T00:00:00',
  1557. end: '2019-10-02T00:00:00',
  1558. statsPeriod: '14d',
  1559. environment: ['staging'],
  1560. };
  1561. const eventView = new EventView(state);
  1562. it('adds new columns, and replaces existing ones', function () {
  1563. const newView = eventView.withColumns([
  1564. {kind: 'field', field: 'title'},
  1565. {kind: 'function', function: ['count', '']},
  1566. {kind: 'field', field: 'project.id'},
  1567. {kind: 'field', field: 'culprit'},
  1568. ]);
  1569. // Views should be different.
  1570. expect(newView.isEqualTo(eventView)).toBe(false);
  1571. expect(newView.fields).toEqual([
  1572. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1573. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1574. {field: 'project.id', width: COL_WIDTH_UNDEFINED},
  1575. {field: 'culprit', width: COL_WIDTH_UNDEFINED},
  1576. ]);
  1577. });
  1578. it('drops empty columns', function () {
  1579. const newView = eventView.withColumns([
  1580. {kind: 'field', field: 'issue'},
  1581. {kind: 'function', function: ['count', '']},
  1582. {kind: 'field', field: ''},
  1583. {kind: 'function', function: ['', '']},
  1584. {kind: 'function', function: ['', '', undefined]},
  1585. ]);
  1586. expect(newView.fields).toEqual([
  1587. {field: 'issue', width: COL_WIDTH_UNDEFINED},
  1588. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1589. ]);
  1590. });
  1591. it('inherits widths from existing columns when names match', function () {
  1592. const newView = eventView.withColumns([
  1593. {kind: 'function', function: ['count', '']},
  1594. {kind: 'field', field: 'project.id'},
  1595. {kind: 'field', field: 'title'},
  1596. {kind: 'field', field: 'time'},
  1597. ]);
  1598. expect(newView.fields).toEqual([
  1599. {field: 'count()', width: 30},
  1600. {field: 'project.id', width: 99},
  1601. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1602. {field: 'time', width: COL_WIDTH_UNDEFINED},
  1603. ]);
  1604. });
  1605. it('retains sorts when sorted field is included', function () {
  1606. const newView = eventView.withColumns([
  1607. {kind: 'field', field: 'title'},
  1608. {kind: 'function', function: ['count', '']},
  1609. ]);
  1610. expect(newView.fields).toEqual([
  1611. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1612. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1613. ]);
  1614. expect(newView.sorts).toEqual([{field: 'count', kind: 'desc'}]);
  1615. });
  1616. it('updates sorts when sorted field is removed', function () {
  1617. const newView = eventView.withColumns([{kind: 'field', field: 'title'}]);
  1618. expect(newView.fields).toEqual([{field: 'title', width: COL_WIDTH_UNDEFINED}]);
  1619. // Should pick a sortable field.
  1620. expect(newView.sorts).toEqual([{field: 'title', kind: 'desc'}]);
  1621. });
  1622. it('has no sort if no sortable fields remain', function () {
  1623. const newView = eventView.withColumns([{kind: 'field', field: 'issue'}]);
  1624. expect(newView.fields).toEqual([{field: 'issue', width: COL_WIDTH_UNDEFINED}]);
  1625. expect(newView.sorts).toEqual([]);
  1626. });
  1627. it('updates yAxis if column is dropped', function () {
  1628. const newView = eventView.withColumns([
  1629. {kind: 'field', field: 'count()'},
  1630. {kind: 'field', field: 'project.id'},
  1631. ]);
  1632. expect(newView.fields).toEqual([
  1633. {field: 'count()', width: 30},
  1634. {field: 'project.id', width: 99},
  1635. ]);
  1636. expect(eventView.yAxis).toEqual('failure_count()');
  1637. expect(newView.yAxis).toEqual('count()');
  1638. });
  1639. });
  1640. describe('EventView.withNewColumn()', function () {
  1641. const state = {
  1642. id: '1234',
  1643. name: 'best query',
  1644. fields: [
  1645. {field: 'count()', width: 30},
  1646. {field: 'project.id', width: 99},
  1647. ],
  1648. sorts: generateSorts(['count']),
  1649. query: 'event.type:error',
  1650. project: [42],
  1651. start: '2019-10-01T00:00:00',
  1652. end: '2019-10-02T00:00:00',
  1653. statsPeriod: '14d',
  1654. environment: ['staging'],
  1655. };
  1656. it('adds a field', function () {
  1657. const eventView = new EventView(state);
  1658. const newColumn = {
  1659. kind: 'field',
  1660. field: 'title',
  1661. };
  1662. const eventView2 = eventView.withNewColumn(newColumn);
  1663. expect(eventView2 !== eventView).toBeTruthy();
  1664. expect(eventView).toMatchObject(state);
  1665. const nextState = {
  1666. ...state,
  1667. fields: [...state.fields, {field: 'title'}],
  1668. };
  1669. expect(eventView2).toMatchObject(nextState);
  1670. });
  1671. it('adds an aggregate function with no arguments', function () {
  1672. const eventView = new EventView(state);
  1673. const newColumn = {
  1674. kind: 'function',
  1675. function: ['count', ''],
  1676. };
  1677. const eventView2 = eventView.withNewColumn(newColumn);
  1678. expect(eventView2 !== eventView).toBeTruthy();
  1679. expect(eventView).toMatchObject(state);
  1680. const nextState = {
  1681. ...state,
  1682. fields: [...state.fields, {field: 'count()'}],
  1683. };
  1684. expect(eventView2).toMatchObject(nextState);
  1685. });
  1686. it('add an aggregate function with field', function () {
  1687. const eventView = new EventView(state);
  1688. const newColumn = {
  1689. kind: 'function',
  1690. function: ['avg', 'transaction.duration'],
  1691. };
  1692. const eventView2 = eventView.withNewColumn(newColumn);
  1693. expect(eventView2 !== eventView).toBeTruthy();
  1694. expect(eventView).toMatchObject(state);
  1695. const nextState = {
  1696. ...state,
  1697. fields: [...state.fields, {field: 'avg(transaction.duration)'}],
  1698. };
  1699. expect(eventView2).toMatchObject(nextState);
  1700. });
  1701. it('add an aggregate function with field & refinement', function () {
  1702. const eventView = new EventView(state);
  1703. const newColumn = {
  1704. kind: 'function',
  1705. function: ['percentile', 'transaction.duration', '0.5'],
  1706. };
  1707. const updated = eventView.withNewColumn(newColumn);
  1708. expect(updated.fields).toEqual([
  1709. ...state.fields,
  1710. {field: 'percentile(transaction.duration,0.5)', width: COL_WIDTH_UNDEFINED},
  1711. ]);
  1712. });
  1713. });
  1714. describe('EventView.withResizedColumn()', function () {
  1715. const state = {
  1716. id: '1234',
  1717. name: 'best query',
  1718. fields: [{field: 'count()'}, {field: 'project.id'}],
  1719. sorts: generateSorts(['count']),
  1720. query: 'event.type:error',
  1721. project: [42],
  1722. start: '2019-10-01T00:00:00',
  1723. end: '2019-10-02T00:00:00',
  1724. statsPeriod: '14d',
  1725. environment: ['staging'],
  1726. };
  1727. const view = new EventView(state);
  1728. it('updates a column that exists', function () {
  1729. const newView = view.withResizedColumn(0, 99);
  1730. expect(view.fields[0].width).toBeUndefined();
  1731. expect(newView.fields[0].width).toEqual(99);
  1732. });
  1733. it('ignores columns that do not exist', function () {
  1734. const newView = view.withResizedColumn(100, 99);
  1735. expect(view.fields).toEqual(newView.fields);
  1736. });
  1737. });
  1738. describe('EventView.withUpdatedColumn()', function () {
  1739. const state = {
  1740. id: '1234',
  1741. name: 'best query',
  1742. fields: [{field: 'count()'}, {field: 'project.id'}],
  1743. sorts: generateSorts(['count']),
  1744. query: 'event.type:error',
  1745. project: [42],
  1746. start: '2019-10-01T00:00:00',
  1747. end: '2019-10-02T00:00:00',
  1748. statsPeriod: '14d',
  1749. environment: ['staging'],
  1750. };
  1751. const meta = {
  1752. count: 'integer',
  1753. title: 'string',
  1754. };
  1755. it('update a column with no changes', function () {
  1756. const eventView = new EventView(state);
  1757. const newColumn = {
  1758. kind: 'function',
  1759. function: ['count', ''],
  1760. };
  1761. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1762. expect(eventView2 === eventView).toBeTruthy();
  1763. expect(eventView).toMatchObject(state);
  1764. });
  1765. it('update a column to a field', function () {
  1766. const eventView = new EventView(state);
  1767. const newColumn = {
  1768. kind: 'field',
  1769. field: 'title',
  1770. };
  1771. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1772. expect(eventView2 !== eventView).toBeTruthy();
  1773. expect(eventView).toMatchObject(state);
  1774. const nextState = {
  1775. ...state,
  1776. fields: [state.fields[0], {field: 'title'}],
  1777. };
  1778. expect(eventView2).toMatchObject(nextState);
  1779. });
  1780. it('update a column to an aggregate function with no arguments', function () {
  1781. const eventView = new EventView(state);
  1782. const newColumn = {
  1783. kind: 'function',
  1784. function: ['count', ''],
  1785. };
  1786. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1787. expect(eventView2 !== eventView).toBeTruthy();
  1788. expect(eventView).toMatchObject(state);
  1789. const nextState = {
  1790. ...state,
  1791. fields: [state.fields[0], {field: 'count()'}],
  1792. };
  1793. expect(eventView2).toMatchObject(nextState);
  1794. });
  1795. it('update a column to an aggregate function with field', function () {
  1796. const eventView = new EventView(state);
  1797. const newColumn = {
  1798. kind: 'function',
  1799. function: ['avg', 'transaction.duration'],
  1800. };
  1801. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1802. expect(eventView2 !== eventView).toBeTruthy();
  1803. expect(eventView).toMatchObject(state);
  1804. const nextState = {
  1805. ...state,
  1806. fields: [state.fields[0], {field: 'avg(transaction.duration)'}],
  1807. };
  1808. expect(eventView2).toMatchObject(nextState);
  1809. });
  1810. it('update a column to an aggregate function with field & refinement', function () {
  1811. const eventView = new EventView(state);
  1812. const newColumn = {
  1813. kind: 'function',
  1814. function: ['percentile', 'transaction.duration', '0.5'],
  1815. };
  1816. const newView = eventView.withUpdatedColumn(1, newColumn, meta);
  1817. expect(newView.fields).toEqual([
  1818. state.fields[0],
  1819. {field: 'percentile(transaction.duration,0.5)', width: COL_WIDTH_UNDEFINED},
  1820. ]);
  1821. });
  1822. describe('update a column that is sorted', function () {
  1823. it('the sorted column is the only sorted column', function () {
  1824. const eventView = new EventView(state);
  1825. const newColumn = {
  1826. kind: 'field',
  1827. field: 'title',
  1828. };
  1829. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1830. expect(eventView2 !== eventView).toBeTruthy();
  1831. expect(eventView).toMatchObject(state);
  1832. const nextState = {
  1833. ...state,
  1834. sorts: [{field: 'title', kind: 'desc'}],
  1835. fields: [{field: 'title'}, state.fields[1]],
  1836. };
  1837. expect(eventView2).toMatchObject(nextState);
  1838. });
  1839. it('the sorted column occurs at least twice', function () {
  1840. const modifiedState = {
  1841. ...state,
  1842. fields: [...state.fields, {field: 'count()'}],
  1843. };
  1844. const eventView = new EventView(modifiedState);
  1845. const newColumn = {
  1846. kind: 'field',
  1847. field: 'title',
  1848. };
  1849. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1850. expect(eventView2 !== eventView).toBeTruthy();
  1851. expect(eventView).toMatchObject(modifiedState);
  1852. const nextState = {
  1853. ...state,
  1854. fields: [{field: 'title'}, state.fields[1], {field: 'count()'}],
  1855. };
  1856. expect(eventView2).toMatchObject(nextState);
  1857. });
  1858. it('using no provided table meta', function () {
  1859. // table meta may not be provided in the invalid query state;
  1860. // we will still want to be able to update columns
  1861. const eventView = new EventView(state);
  1862. const expected = {
  1863. ...state,
  1864. sorts: [{field: 'title', kind: 'desc'}],
  1865. fields: [{field: 'title'}, state.fields[1]],
  1866. };
  1867. const newColumn = {
  1868. kind: 'field',
  1869. field: 'title',
  1870. };
  1871. const eventView2 = eventView.withUpdatedColumn(0, newColumn, {});
  1872. expect(eventView2).toMatchObject(expected);
  1873. const eventView3 = eventView.withUpdatedColumn(0, newColumn);
  1874. expect(eventView3).toMatchObject(expected);
  1875. const eventView4 = eventView.withUpdatedColumn(0, newColumn, null);
  1876. expect(eventView4).toMatchObject(expected);
  1877. });
  1878. });
  1879. describe('update a column to a non-sortable column', function () {
  1880. it('default to a sortable column', function () {
  1881. const modifiedState = {
  1882. ...state,
  1883. fields: [{field: 'count()'}, {field: 'title'}],
  1884. };
  1885. const eventView = new EventView(modifiedState);
  1886. // this column is expected to be non-sortable
  1887. const newColumn = {
  1888. kind: 'field',
  1889. field: 'project.id',
  1890. };
  1891. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1892. expect(eventView2 !== eventView).toBeTruthy();
  1893. expect(eventView).toMatchObject(modifiedState);
  1894. const nextState = {
  1895. ...state,
  1896. sorts: [{field: 'title', kind: 'desc'}],
  1897. fields: [{field: 'project.id'}, {field: 'title'}],
  1898. };
  1899. expect(eventView2).toMatchObject(nextState);
  1900. });
  1901. it('has no sort if there are no sortable columns', function () {
  1902. const modifiedState = {
  1903. ...state,
  1904. fields: [{field: 'count()'}],
  1905. };
  1906. const eventView = new EventView(modifiedState);
  1907. // this column is expected to be non-sortable
  1908. const newColumn = {
  1909. kind: 'field',
  1910. field: 'project.id',
  1911. };
  1912. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1913. expect(eventView2 !== eventView).toBeTruthy();
  1914. expect(eventView).toMatchObject(modifiedState);
  1915. const nextState = {
  1916. ...state,
  1917. sorts: [],
  1918. fields: [{field: 'project.id'}],
  1919. };
  1920. expect(eventView2).toMatchObject(nextState);
  1921. });
  1922. });
  1923. });
  1924. describe('EventView.withDeletedColumn()', function () {
  1925. const state = {
  1926. id: '1234',
  1927. name: 'best query',
  1928. fields: [{field: 'count()'}, {field: 'project.id'}],
  1929. sorts: generateSorts(['count']),
  1930. query: 'event.type:error',
  1931. project: [42],
  1932. start: '2019-10-01T00:00:00',
  1933. end: '2019-10-02T00:00:00',
  1934. statsPeriod: '14d',
  1935. environment: ['staging'],
  1936. };
  1937. const meta = {
  1938. count: 'integer',
  1939. title: 'string',
  1940. };
  1941. it('returns itself when attempting to delete the last remaining column', function () {
  1942. const modifiedState = {
  1943. ...state,
  1944. fields: [{field: 'count()'}],
  1945. };
  1946. const eventView = new EventView(modifiedState);
  1947. const eventView2 = eventView.withDeletedColumn(0, meta);
  1948. expect(eventView2 === eventView).toBeTruthy();
  1949. expect(eventView).toMatchObject(modifiedState);
  1950. });
  1951. describe('deletes column, and use any remaining sortable column', function () {
  1952. it('using no provided table meta', function () {
  1953. // table meta may not be provided in the invalid query state;
  1954. // we will still want to be able to delete columns
  1955. const state2 = {
  1956. ...state,
  1957. fields: [{field: 'title'}, {field: 'timestamp'}, {field: 'count()'}],
  1958. sorts: generateSorts(['timestamp']),
  1959. };
  1960. const eventView = new EventView(state2);
  1961. const expected = {
  1962. ...state,
  1963. sorts: generateSorts(['title']),
  1964. fields: [{field: 'title'}, {field: 'count()'}],
  1965. };
  1966. const eventView2 = eventView.withDeletedColumn(1, {});
  1967. expect(eventView2).toMatchObject(expected);
  1968. const eventView3 = eventView.withDeletedColumn(1);
  1969. expect(eventView3).toMatchObject(expected);
  1970. const eventView4 = eventView.withDeletedColumn(1, null);
  1971. expect(eventView4).toMatchObject(expected);
  1972. });
  1973. it('has no remaining sortable column', function () {
  1974. const eventView = new EventView(state);
  1975. const eventView2 = eventView.withDeletedColumn(0, meta);
  1976. expect(eventView2 !== eventView).toBeTruthy();
  1977. expect(eventView).toMatchObject(state);
  1978. const nextState = {
  1979. ...state,
  1980. // we expect sorts to be empty since project.id is non-sortable
  1981. sorts: [],
  1982. fields: [state.fields[1]],
  1983. };
  1984. expect(eventView2).toMatchObject(nextState);
  1985. });
  1986. it('has a remaining sortable column', function () {
  1987. const modifiedState = {
  1988. ...state,
  1989. fields: [{field: 'count()'}, {field: 'project.id'}, {field: 'title'}],
  1990. };
  1991. const eventView = new EventView(modifiedState);
  1992. const eventView2 = eventView.withDeletedColumn(0, meta);
  1993. expect(eventView2 !== eventView).toBeTruthy();
  1994. expect(eventView).toMatchObject(modifiedState);
  1995. const nextState = {
  1996. ...state,
  1997. sorts: [{field: 'title', kind: 'desc'}],
  1998. fields: [{field: 'project.id'}, {field: 'title'}],
  1999. };
  2000. expect(eventView2).toMatchObject(nextState);
  2001. });
  2002. it('sorted column occurs at least twice', function () {
  2003. const modifiedState = {
  2004. ...state,
  2005. fields: [...state.fields, state.fields[0]],
  2006. };
  2007. const eventView = new EventView(modifiedState);
  2008. const eventView2 = eventView.withDeletedColumn(0, meta);
  2009. expect(eventView2 !== eventView).toBeTruthy();
  2010. expect(eventView).toMatchObject(modifiedState);
  2011. const nextState = {
  2012. ...state,
  2013. fields: [state.fields[1], state.fields[0]],
  2014. };
  2015. expect(eventView2).toMatchObject(nextState);
  2016. });
  2017. it('ensures there is at one auto-width column on deletion', function () {
  2018. const modifiedState = {
  2019. ...state,
  2020. fields: [
  2021. {field: 'id', width: 75},
  2022. {field: 'title', width: 100},
  2023. {field: 'project', width: 80},
  2024. {field: 'environment', width: 99},
  2025. ],
  2026. };
  2027. const eventView = new EventView(modifiedState);
  2028. let updated = eventView.withDeletedColumn(0, meta);
  2029. let updatedFields = [
  2030. {field: 'title', width: -1},
  2031. {field: 'project', width: 80},
  2032. {field: 'environment', width: 99},
  2033. ];
  2034. expect(updated.fields).toEqual(updatedFields);
  2035. updated = updated.withDeletedColumn(0, meta);
  2036. updatedFields = [
  2037. {field: 'project', width: -1},
  2038. {field: 'environment', width: 99},
  2039. ];
  2040. expect(updated.fields).toEqual(updatedFields);
  2041. });
  2042. });
  2043. });
  2044. describe('EventView.getSorts()', function () {
  2045. it('returns fields', function () {
  2046. const eventView = new EventView({
  2047. fields: [{field: 'count()'}, {field: 'project.id'}],
  2048. sorts: generateSorts(['count']),
  2049. project: [],
  2050. });
  2051. expect(eventView.getSorts()).toEqual([
  2052. {
  2053. key: 'count',
  2054. order: 'desc',
  2055. },
  2056. ]);
  2057. });
  2058. });
  2059. describe('EventView.getQuery()', function () {
  2060. it('with query', function () {
  2061. const eventView = new EventView({
  2062. fields: [],
  2063. sorts: [],
  2064. project: [],
  2065. query: 'event.type:error',
  2066. });
  2067. expect(eventView.getQuery()).toEqual('event.type:error');
  2068. expect(eventView.getQuery(null)).toEqual('event.type:error');
  2069. expect(eventView.getQuery('hello')).toEqual('event.type:error hello');
  2070. expect(eventView.getQuery(['event.type:error', 'hello'])).toEqual(
  2071. 'event.type:error hello'
  2072. );
  2073. });
  2074. it('without query', function () {
  2075. const eventView = new EventView({
  2076. fields: [],
  2077. sorts: [],
  2078. project: [],
  2079. });
  2080. expect(eventView.getQuery()).toEqual('');
  2081. expect(eventView.getQuery(null)).toEqual('');
  2082. expect(eventView.getQuery('hello')).toEqual('hello');
  2083. expect(eventView.getQuery(['event.type:error', 'hello'])).toEqual(
  2084. 'event.type:error hello'
  2085. );
  2086. });
  2087. });
  2088. describe('EventView.getQueryWithAdditionalConditions', function () {
  2089. it('with overlapping conditions', function () {
  2090. const eventView = new EventView({
  2091. fields: [],
  2092. sorts: [],
  2093. project: [],
  2094. query: 'event.type:transaction foo:bar',
  2095. });
  2096. eventView.additionalConditions.setFilterValues('event.type', ['transaction']);
  2097. expect(eventView.getQueryWithAdditionalConditions()).toEqual(
  2098. 'event.type:transaction foo:bar'
  2099. );
  2100. });
  2101. });
  2102. describe('EventView.sortForField()', function () {
  2103. const state = {
  2104. id: '1234',
  2105. name: 'best query',
  2106. fields: [{field: 'count()'}, {field: 'project.id'}],
  2107. sorts: generateSorts(['count']),
  2108. query: 'event.type:error',
  2109. project: [42],
  2110. start: '2019-10-01T00:00:00',
  2111. end: '2019-10-02T00:00:00',
  2112. statsPeriod: '14d',
  2113. environment: ['staging'],
  2114. };
  2115. const eventView = new EventView(state);
  2116. const meta = {count: 'integer'};
  2117. it('returns the sort when selected field is sorted', function () {
  2118. const field = {
  2119. field: 'count()',
  2120. };
  2121. const actual = eventView.sortForField(field, meta);
  2122. expect(actual).toEqual({
  2123. field: 'count',
  2124. kind: 'desc',
  2125. });
  2126. });
  2127. it('returns undefined when selected field is not sorted', function () {
  2128. const field = {
  2129. field: 'project.id',
  2130. };
  2131. expect(eventView.sortForField(field, meta)).toBeUndefined();
  2132. });
  2133. it('returns undefined when no meta is provided', function () {
  2134. const field = {
  2135. field: 'project.id',
  2136. };
  2137. expect(eventView.sortForField(field, undefined)).toBeUndefined();
  2138. });
  2139. });
  2140. describe('EventView.sortOnField()', function () {
  2141. const state = {
  2142. id: '1234',
  2143. name: 'best query',
  2144. fields: [{field: 'count()'}, {field: 'project.id'}],
  2145. sorts: generateSorts(['count']),
  2146. query: 'event.type:error',
  2147. project: [42],
  2148. start: '2019-10-01T00:00:00',
  2149. end: '2019-10-02T00:00:00',
  2150. statsPeriod: '14d',
  2151. environment: ['staging'],
  2152. };
  2153. const meta = {count: 'integer', title: 'string'};
  2154. it('returns itself when attempting to sort on un-sortable field', function () {
  2155. const eventView = new EventView(state);
  2156. expect(eventView).toMatchObject(state);
  2157. const field = state.fields[1];
  2158. const eventView2 = eventView.sortOnField(field, meta);
  2159. expect(eventView2 === eventView).toBe(true);
  2160. });
  2161. it('reverses the sorted field', function () {
  2162. const eventView = new EventView(state);
  2163. expect(eventView).toMatchObject(state);
  2164. const field = state.fields[0];
  2165. const eventView2 = eventView.sortOnField(field, meta);
  2166. expect(eventView2 !== eventView).toBe(true);
  2167. const nextState = {
  2168. ...state,
  2169. sorts: [{field: 'count', kind: 'asc'}],
  2170. };
  2171. expect(eventView2).toMatchObject(nextState);
  2172. });
  2173. it('enforce sort order on sorted field', function () {
  2174. const eventView = new EventView(state);
  2175. expect(eventView).toMatchObject(state);
  2176. const field = state.fields[0];
  2177. const eventView2 = eventView.sortOnField(field, meta, 'asc');
  2178. expect(eventView2).toMatchObject({
  2179. ...state,
  2180. sorts: [{field: 'count', kind: 'asc'}],
  2181. });
  2182. const eventView3 = eventView.sortOnField(field, meta, 'desc');
  2183. expect(eventView3).toMatchObject({
  2184. ...state,
  2185. sorts: [{field: 'count', kind: 'desc'}],
  2186. });
  2187. });
  2188. it('supports function format on equation sorts', function () {
  2189. const modifiedState = {
  2190. ...state,
  2191. fields: [{field: 'count()'}, {field: 'equation|count() + 100'}],
  2192. sorts: [{field: 'equation|count() + 100', kind: 'desc'}],
  2193. };
  2194. const eventView = new EventView(modifiedState);
  2195. expect(eventView).toMatchObject(modifiedState);
  2196. });
  2197. it('supports index format on equation sorts', function () {
  2198. const modifiedState = {
  2199. ...state,
  2200. fields: [{field: 'count()'}, {field: 'equation|count() + 100'}],
  2201. sorts: [{field: 'equation[0]', kind: 'desc'}],
  2202. };
  2203. const eventView = new EventView(modifiedState);
  2204. expect(eventView).toMatchObject(modifiedState);
  2205. });
  2206. it('sort on new field', function () {
  2207. const modifiedState = {
  2208. ...state,
  2209. fields: [...state.fields, {field: 'title'}],
  2210. };
  2211. const eventView = new EventView(modifiedState);
  2212. expect(eventView).toMatchObject(modifiedState);
  2213. const field = modifiedState.fields[2];
  2214. const eventView2 = eventView.sortOnField(field, meta);
  2215. expect(eventView2 !== eventView).toBe(true);
  2216. const nextState = {
  2217. ...modifiedState,
  2218. sorts: [{field: 'title', kind: 'desc'}],
  2219. };
  2220. expect(eventView2).toMatchObject(nextState);
  2221. // enforce asc sort order
  2222. const eventView3 = eventView.sortOnField(field, meta, 'asc');
  2223. expect(eventView3).toMatchObject({
  2224. ...modifiedState,
  2225. sorts: [{field: 'title', kind: 'asc'}],
  2226. });
  2227. // enforce desc sort order
  2228. const eventView4 = eventView.sortOnField(field, meta, 'desc');
  2229. expect(eventView4).toMatchObject({
  2230. ...modifiedState,
  2231. sorts: [{field: 'title', kind: 'desc'}],
  2232. });
  2233. });
  2234. it('sorts on a field using function format', function () {
  2235. const modifiedState = {
  2236. ...state,
  2237. fields: [...state.fields, {field: 'count()'}],
  2238. };
  2239. const eventView = new EventView(modifiedState);
  2240. expect(eventView).toMatchObject(modifiedState);
  2241. const field = modifiedState.fields[2];
  2242. let sortedEventView = eventView.sortOnField(field, meta, undefined, true);
  2243. expect(sortedEventView.sorts).toEqual([{field: 'count()', kind: 'asc'}]);
  2244. sortedEventView = sortedEventView.sortOnField(field, meta, undefined, true);
  2245. expect(sortedEventView.sorts).toEqual([{field: 'count()', kind: 'desc'}]);
  2246. });
  2247. });
  2248. describe('EventView.withSorts()', function () {
  2249. it('returns a clone', function () {
  2250. const eventView = new EventView({
  2251. fields: [{field: 'event.type'}],
  2252. });
  2253. const updated = eventView.withSorts([{kind: 'desc', field: 'event.type'}]);
  2254. expect(updated.sorts).not.toEqual(eventView.sorts);
  2255. });
  2256. it('only accepts sorting on fields in the view', function () {
  2257. const eventView = new EventView({
  2258. fields: [{field: 'event.type'}],
  2259. });
  2260. const updated = eventView.withSorts([
  2261. {kind: 'desc', field: 'event.type'},
  2262. {kind: 'asc', field: 'unknown'},
  2263. ]);
  2264. expect(updated.sorts).toEqual([{kind: 'desc', field: 'event.type'}]);
  2265. });
  2266. it('accepts aggregate field sorts', function () {
  2267. const eventView = new EventView({
  2268. fields: [{field: 'p50()'}],
  2269. });
  2270. const updated = eventView.withSorts([
  2271. {kind: 'desc', field: 'p50'},
  2272. {kind: 'asc', field: 'unknown'},
  2273. ]);
  2274. expect(updated.sorts).toEqual([{kind: 'desc', field: 'p50'}]);
  2275. });
  2276. });
  2277. describe('EventView.isEqualTo()', function () {
  2278. it('should be true when equal', function () {
  2279. const state = {
  2280. id: '1234',
  2281. name: 'best query',
  2282. fields: [{field: 'count()'}, {field: 'project.id'}],
  2283. sorts: generateSorts(['count']),
  2284. query: 'event.type:error',
  2285. project: [42],
  2286. start: '2019-10-01T00:00:00',
  2287. end: '2019-10-02T00:00:00',
  2288. statsPeriod: '14d',
  2289. environment: ['staging'],
  2290. yAxis: 'fam',
  2291. display: 'releases',
  2292. };
  2293. const eventView = new EventView(state);
  2294. const eventView2 = new EventView(state);
  2295. expect(eventView2 !== eventView).toBeTruthy();
  2296. expect(eventView).toMatchObject(state);
  2297. expect(eventView2).toMatchObject(state);
  2298. expect(eventView.isEqualTo(eventView2)).toBe(true);
  2299. // commutativity property holds
  2300. expect(eventView2.isEqualTo(eventView)).toBe(true);
  2301. });
  2302. it('should be true when datetime are equal but differ in format', function () {
  2303. const state = {
  2304. id: '1234',
  2305. name: 'best query',
  2306. fields: [{field: 'count()'}, {field: 'project.id'}],
  2307. sorts: generateSorts(['count']),
  2308. query: 'event.type:error',
  2309. project: [42],
  2310. start: '2019-10-20T21:02:51+0000',
  2311. end: '2019-10-23T19:27:04+0000',
  2312. environment: ['staging'],
  2313. };
  2314. const eventView = new EventView(state);
  2315. const eventView2 = new EventView({
  2316. ...state,
  2317. start: '2019-10-20T21:02:51Z',
  2318. end: '2019-10-23T19:27:04Z',
  2319. });
  2320. expect(eventView.isEqualTo(eventView2)).toBe(true);
  2321. });
  2322. it('should be false when not equal', function () {
  2323. const state = {
  2324. id: '1234',
  2325. name: 'best query',
  2326. fields: [{field: 'count()'}, {field: 'project.id'}],
  2327. sorts: generateSorts(['count']),
  2328. query: 'event.type:error',
  2329. project: [42],
  2330. start: '2019-10-01T00:00:00',
  2331. end: '2019-10-02T00:00:00',
  2332. statsPeriod: '14d',
  2333. environment: ['staging'],
  2334. yAxis: 'fam',
  2335. display: 'releases',
  2336. };
  2337. const differences = {
  2338. id: '12',
  2339. name: 'new query',
  2340. fields: [{field: 'project.id'}, {field: 'count()'}],
  2341. sorts: [{field: 'count', kind: 'asc'}],
  2342. query: 'event.type:transaction',
  2343. project: [24],
  2344. start: '2019-09-01T00:00:00',
  2345. end: '2020-09-01T00:00:00',
  2346. statsPeriod: '24d',
  2347. environment: [],
  2348. yAxis: 'ok boomer',
  2349. display: 'previous',
  2350. };
  2351. const eventView = new EventView(state);
  2352. for (const key in differences) {
  2353. const eventView2 = new EventView({...state, [key]: differences[key]});
  2354. expect(eventView.isEqualTo(eventView2)).toBe(false);
  2355. }
  2356. });
  2357. it('undefined display type equals default display type', function () {
  2358. const state = {
  2359. id: '1234',
  2360. name: 'best query',
  2361. fields: [{field: 'count()'}, {field: 'project.id'}],
  2362. sorts: generateSorts(['count']),
  2363. query: 'event.type:error',
  2364. project: [42],
  2365. start: '2019-10-01T00:00:00',
  2366. end: '2019-10-02T00:00:00',
  2367. statsPeriod: '14d',
  2368. environment: ['staging'],
  2369. yAxis: 'fam',
  2370. };
  2371. const eventView = new EventView(state);
  2372. const eventView2 = new EventView({...state, display: 'default'});
  2373. expect(eventView.isEqualTo(eventView2)).toBe(true);
  2374. });
  2375. });
  2376. describe('EventView.getResultsViewUrlTarget()', function () {
  2377. const state = {
  2378. id: '1234',
  2379. name: 'best query',
  2380. fields: [{field: 'count()'}, {field: 'project.id'}],
  2381. sorts: generateSorts(['count']),
  2382. query: 'event.type:error',
  2383. project: [42],
  2384. start: '2019-10-01T00:00:00',
  2385. end: '2019-10-02T00:00:00',
  2386. statsPeriod: '14d',
  2387. environment: ['staging'],
  2388. display: 'previous',
  2389. };
  2390. const organization = TestStubs.Organization();
  2391. it('generates a URL', function () {
  2392. const view = new EventView(state);
  2393. const result = view.getResultsViewUrlTarget(organization.slug);
  2394. expect(result.pathname).toEqual('/organizations/org-slug/discover/results/');
  2395. expect(result.query.query).toEqual(state.query);
  2396. expect(result.query.project).toEqual(state.project);
  2397. expect(result.query.display).toEqual(state.display);
  2398. });
  2399. });
  2400. describe('EventView.getResultsViewShortUrlTarget()', function () {
  2401. const state = {
  2402. id: '1234',
  2403. name: 'best query',
  2404. fields: [{field: 'count()'}, {field: 'project.id'}],
  2405. sorts: generateSorts(['count']),
  2406. query: 'event.type:error',
  2407. project: [42],
  2408. start: '2019-10-01T00:00:00',
  2409. end: '2019-10-02T00:00:00',
  2410. statsPeriod: '14d',
  2411. environment: ['staging'],
  2412. display: 'previous',
  2413. };
  2414. const organization = TestStubs.Organization();
  2415. it('generates a URL', function () {
  2416. const view = new EventView(state);
  2417. const result = view.getResultsViewShortUrlTarget(organization.slug);
  2418. expect(result.pathname).toEqual('/organizations/org-slug/discover/results/');
  2419. expect(result.query).not.toHaveProperty('name');
  2420. expect(result.query).not.toHaveProperty('fields');
  2421. expect(result.query).not.toHaveProperty('query');
  2422. expect(result.query.id).toEqual(state.id);
  2423. expect(result.query.statsPeriod).toEqual(state.statsPeriod);
  2424. expect(result.query.project).toEqual(state.project);
  2425. expect(result.query.environment).toEqual(state.environment);
  2426. });
  2427. });
  2428. describe('EventView.getPerformanceTransactionEventsViewUrlTarget()', function () {
  2429. const state = {
  2430. id: '1234',
  2431. name: 'best query',
  2432. fields: [{field: 'count()'}, {field: 'project.id'}],
  2433. sorts: generateSorts(['count']),
  2434. query: 'event.type:error',
  2435. project: [42],
  2436. start: '2019-10-01T00:00:00',
  2437. end: '2019-10-02T00:00:00',
  2438. statsPeriod: '14d',
  2439. environment: ['staging'],
  2440. display: 'previous',
  2441. };
  2442. const organization = TestStubs.Organization();
  2443. const showTransactions = 'p99';
  2444. const breakdown = 'http';
  2445. const webVital = 'measurements.lcp';
  2446. it('generates a URL', function () {
  2447. const view = new EventView(state);
  2448. const result = view.getPerformanceTransactionEventsViewUrlTarget(organization.slug, {
  2449. showTransactions,
  2450. breakdown,
  2451. webVital,
  2452. });
  2453. expect(result.pathname).toEqual(
  2454. '/organizations/org-slug/performance/summary/events/'
  2455. );
  2456. expect(result.query.query).toEqual(state.query);
  2457. expect(result.query.project).toEqual(state.project);
  2458. expect(result.query.sort).toEqual(['-count']);
  2459. expect(result.query.transaction).toEqual(state.name);
  2460. expect(result.query.showTransactions).toEqual(showTransactions);
  2461. expect(result.query.breakdown).toEqual(breakdown);
  2462. expect(result.query.webVital).toEqual(webVital);
  2463. });
  2464. });
  2465. describe('EventView.getPageFilters()', function () {
  2466. it('return default global selection', function () {
  2467. const eventView = new EventView({});
  2468. expect(eventView.getPageFilters()).toMatchObject({
  2469. projects: [],
  2470. environments: [],
  2471. datetime: {
  2472. start: null,
  2473. end: null,
  2474. period: null,
  2475. // event views currently do not support the utc option,
  2476. // see comment in EventView.getPageFilters
  2477. utc: true,
  2478. },
  2479. });
  2480. });
  2481. it('returns global selection query', function () {
  2482. const state2 = {
  2483. project: [42],
  2484. start: 'start',
  2485. end: 'end',
  2486. statsPeriod: '42d',
  2487. environment: ['prod'],
  2488. };
  2489. const eventView = new EventView(state2);
  2490. expect(eventView.getPageFilters()).toMatchObject({
  2491. projects: state2.project,
  2492. environments: state2.environment,
  2493. datetime: {
  2494. start: state2.start,
  2495. end: state2.end,
  2496. period: state2.statsPeriod,
  2497. // event views currently do not support the utc option,
  2498. // see comment in EventView.getPageFilters
  2499. utc: true,
  2500. },
  2501. });
  2502. });
  2503. });
  2504. describe('EventView.getPageFiltersQuery()', function () {
  2505. it('return default global selection query', function () {
  2506. const eventView = new EventView({});
  2507. expect(eventView.getPageFiltersQuery()).toMatchObject({
  2508. project: [],
  2509. start: undefined,
  2510. end: undefined,
  2511. statsPeriod: undefined,
  2512. environment: [],
  2513. // event views currently do not support the utc option,
  2514. // see comment in EventView.getPageFilters
  2515. utc: 'true',
  2516. });
  2517. });
  2518. it('returns global selection query', function () {
  2519. const state2 = {
  2520. project: [42],
  2521. start: 'start',
  2522. end: 'end',
  2523. statsPeriod: '42d',
  2524. environment: ['prod'],
  2525. };
  2526. const eventView = new EventView(state2);
  2527. expect(eventView.getPageFiltersQuery()).toMatchObject({
  2528. ...state2,
  2529. // when generating the query, it converts numbers to strings
  2530. project: ['42'],
  2531. // event views currently do not support the utc option,
  2532. // see comment in EventView.getPageFilters
  2533. utc: 'true',
  2534. });
  2535. });
  2536. });
  2537. describe('EventView.generateBlankQueryStringObject()', function () {
  2538. it('should return blank values', function () {
  2539. const eventView = new EventView({});
  2540. expect(eventView.generateBlankQueryStringObject()).toEqual({
  2541. id: undefined,
  2542. name: undefined,
  2543. fields: undefined,
  2544. sorts: undefined,
  2545. query: undefined,
  2546. project: undefined,
  2547. start: undefined,
  2548. end: undefined,
  2549. statsPeriod: undefined,
  2550. environment: undefined,
  2551. yAxis: undefined,
  2552. cursor: undefined,
  2553. });
  2554. });
  2555. });
  2556. describe('EventView.getYAxisOptions()', function () {
  2557. const state = {
  2558. fields: [],
  2559. sorts: [],
  2560. query: '',
  2561. project: [],
  2562. statsPeriod: '42d',
  2563. environment: [],
  2564. };
  2565. function generateYaxis(value) {
  2566. return {
  2567. value,
  2568. label: value,
  2569. };
  2570. }
  2571. it('should return default options', function () {
  2572. const thisEventView = new EventView(state);
  2573. expect(thisEventView.getYAxisOptions()).toEqual(CHART_AXIS_OPTIONS);
  2574. });
  2575. it('should add aggregate fields as options', function () {
  2576. let thisEventView = new EventView({
  2577. ...state,
  2578. fields: generateFields(['ignored-field', 'count_unique(issue)']),
  2579. });
  2580. expect(thisEventView.getYAxisOptions()).toEqual([
  2581. generateYaxis('count_unique(issue)'),
  2582. ...CHART_AXIS_OPTIONS,
  2583. ]);
  2584. // should de-duplicate entries
  2585. thisEventView = new EventView({
  2586. ...state,
  2587. fields: generateFields(['ignored-field', 'count()']),
  2588. });
  2589. expect(thisEventView.getYAxisOptions()).toEqual([...CHART_AXIS_OPTIONS]);
  2590. });
  2591. it('should exclude yAxis options that are not useful', function () {
  2592. const thisEventView = new EventView({
  2593. ...state,
  2594. fields: generateFields([
  2595. 'ignored-field',
  2596. 'count_unique(issue)',
  2597. 'last_seen()',
  2598. 'max(timestamp)',
  2599. ]),
  2600. });
  2601. expect(thisEventView.getYAxisOptions()).toEqual([
  2602. generateYaxis('count_unique(issue)'),
  2603. ...CHART_AXIS_OPTIONS,
  2604. ]);
  2605. });
  2606. });
  2607. describe('EventView.getYAxis()', function () {
  2608. const state = {
  2609. fields: [],
  2610. sorts: [],
  2611. query: '',
  2612. project: [],
  2613. statsPeriod: '42d',
  2614. environment: [],
  2615. };
  2616. it('should return first default yAxis', function () {
  2617. const thisEventView = new EventView(state);
  2618. expect(thisEventView.getYAxis()).toEqual('count()');
  2619. });
  2620. it('should return valid yAxis', function () {
  2621. const thisEventView = new EventView({
  2622. ...state,
  2623. fields: generateFields(['ignored-field', 'count_unique(user)', 'last_seen']),
  2624. yAxis: 'count_unique(user)',
  2625. });
  2626. expect(thisEventView.getYAxis()).toEqual('count_unique(user)');
  2627. });
  2628. it('should ignore invalid yAxis', function () {
  2629. const invalid = [
  2630. 'last_seen',
  2631. 'latest_event',
  2632. 'count_unique(issue)', // this is not one of the selected fields
  2633. ];
  2634. for (const option of invalid) {
  2635. const thisEventView = new EventView({
  2636. ...state,
  2637. fields: generateFields(['ignored-field', 'last_seen', 'latest_event']),
  2638. yAxis: option,
  2639. });
  2640. // yAxis defaults to the first entry of the default yAxis options
  2641. expect(thisEventView.getYAxis()).toEqual('count()');
  2642. }
  2643. });
  2644. });
  2645. describe('EventView.getDisplayOptions()', function () {
  2646. const state = {
  2647. fields: [],
  2648. sorts: [],
  2649. query: '',
  2650. project: [],
  2651. statsPeriod: '42d',
  2652. environment: [],
  2653. };
  2654. it('should return default options', function () {
  2655. const eventView = new EventView({
  2656. ...state,
  2657. // there needs to exist an aggregate or TOP 5 modes will be disabled
  2658. fields: [{field: 'count()'}],
  2659. });
  2660. expect(eventView.getDisplayOptions()).toEqual(DISPLAY_MODE_OPTIONS);
  2661. });
  2662. it('should disable previous when start/end are used.', function () {
  2663. const eventView = new EventView({
  2664. ...state,
  2665. end: '2020-04-13T12:13:14',
  2666. start: '2020-04-01T12:13:14',
  2667. });
  2668. const options = eventView.getDisplayOptions();
  2669. expect(options[1].value).toEqual('previous');
  2670. expect(options[1].disabled).toBeTruthy();
  2671. });
  2672. it('should disable top 5 period/daily if no aggregates present', function () {
  2673. const eventView = new EventView({
  2674. ...state,
  2675. });
  2676. const options = eventView.getDisplayOptions();
  2677. expect(options[2].value).toEqual('top5');
  2678. expect(options[2].disabled).toBeTruthy();
  2679. expect(options[4].value).toEqual('dailytop5');
  2680. expect(options[4].disabled).toBeTruthy();
  2681. });
  2682. });
  2683. describe('EventView.getDisplayMode()', function () {
  2684. const state = {
  2685. fields: [],
  2686. sorts: [],
  2687. query: '',
  2688. project: [],
  2689. statsPeriod: '42d',
  2690. environment: [],
  2691. };
  2692. it('should have default', function () {
  2693. const eventView = new EventView({
  2694. ...state,
  2695. });
  2696. const displayMode = eventView.getDisplayMode();
  2697. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2698. });
  2699. it('should return current mode when not disabled', function () {
  2700. const eventView = new EventView({
  2701. ...state,
  2702. display: DisplayModes.DAILY,
  2703. });
  2704. const displayMode = eventView.getDisplayMode();
  2705. expect(displayMode).toEqual(DisplayModes.DAILY);
  2706. });
  2707. it('should return default mode when disabled', function () {
  2708. const eventView = new EventView({
  2709. ...state,
  2710. // the existence of start and end will disable the PREVIOUS mode
  2711. end: '2020-04-13T12:13:14',
  2712. start: '2020-04-01T12:13:14',
  2713. display: DisplayModes.PREVIOUS,
  2714. });
  2715. const displayMode = eventView.getDisplayMode();
  2716. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2717. });
  2718. it('top 5 should fallback to default when disabled', function () {
  2719. const eventView = new EventView({
  2720. ...state,
  2721. // the lack of an aggregate will disable the TOP5 mode
  2722. display: DisplayModes.TOP5,
  2723. });
  2724. const displayMode = eventView.getDisplayMode();
  2725. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2726. });
  2727. it('top 5 daily should fallback to daily when disabled', function () {
  2728. const eventView = new EventView({
  2729. ...state,
  2730. // the lack of an aggregate will disable the DAILYTOP5 mode
  2731. display: DisplayModes.DAILYTOP5,
  2732. });
  2733. const displayMode = eventView.getDisplayMode();
  2734. expect(displayMode).toEqual(DisplayModes.DAILY);
  2735. });
  2736. it('daily mode should fall back to default when disabled', function () {
  2737. const eventView = new EventView({
  2738. ...state,
  2739. // the period being less than 24h will disable the DAILY mode
  2740. statsPeriod: '1h',
  2741. display: DisplayModes.DAILY,
  2742. });
  2743. const displayMode = eventView.getDisplayMode();
  2744. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2745. });
  2746. it('top 5 daily mode should fall back to default when daily is disabled', function () {
  2747. const eventView = new EventView({
  2748. ...state,
  2749. // the period being less than 24h will disable the DAILY mode
  2750. start: '2020-04-01T12:13:14',
  2751. end: '2020-04-02T12:10:14',
  2752. display: DisplayModes.TOP5DAILY,
  2753. });
  2754. const displayMode = eventView.getDisplayMode();
  2755. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2756. });
  2757. });
  2758. describe('EventView.getAggregateFields()', function () {
  2759. const state = {
  2760. fields: [
  2761. {field: 'title'},
  2762. {field: 'count()'},
  2763. {field: 'count_unique(user)'},
  2764. {field: 'apdex(300)'},
  2765. {field: 'transaction'},
  2766. ],
  2767. sorts: [],
  2768. query: '',
  2769. project: [],
  2770. statsPeriod: '42d',
  2771. environment: [],
  2772. };
  2773. it('getAggregateFields() returns only aggregates', function () {
  2774. const eventView = new EventView(state);
  2775. const expected = [
  2776. {field: 'count()'},
  2777. {field: 'count_unique(user)'},
  2778. {field: 'apdex(300)'},
  2779. ];
  2780. expect(eventView.getAggregateFields()).toEqual(expected);
  2781. });
  2782. });
  2783. describe('EventView.hasAggregateField', function () {
  2784. it('ensures an eventview has an aggregate field', function () {
  2785. let eventView = new EventView({
  2786. fields: [{field: 'foobar'}],
  2787. sorts: [],
  2788. query: '',
  2789. project: [],
  2790. environment: [],
  2791. });
  2792. expect(eventView.hasAggregateField()).toBe(false);
  2793. eventView = new EventView({
  2794. fields: [{field: 'count(foo.bar.is-Enterprise_42)'}],
  2795. sorts: [],
  2796. query: '',
  2797. project: [],
  2798. environment: [],
  2799. });
  2800. expect(eventView.hasAggregateField()).toBe(true);
  2801. });
  2802. });
  2803. describe('isAPIPayloadSimilar', function () {
  2804. const state = {
  2805. id: '1234',
  2806. name: 'best query',
  2807. fields: [{field: 'count()'}, {field: 'project.id'}],
  2808. sorts: generateSorts(['count']),
  2809. query: 'event.type:error',
  2810. project: [42],
  2811. start: '2019-10-01T00:00:00',
  2812. end: '2019-10-02T00:00:00',
  2813. statsPeriod: '14d',
  2814. environment: ['staging'],
  2815. };
  2816. const meta = {
  2817. count: 'integer',
  2818. title: 'string',
  2819. };
  2820. describe('getEventsAPIPayload', function () {
  2821. it('is not similar when relevant query string keys are present in the Location object', function () {
  2822. const thisEventView = new EventView(state);
  2823. const location = {
  2824. query: {
  2825. project: 'project',
  2826. environment: 'environment',
  2827. start: 'start',
  2828. end: 'end',
  2829. utc: 'utc',
  2830. statsPeriod: 'statsPeriod',
  2831. cursor: 'cursor',
  2832. },
  2833. };
  2834. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2835. const otherLocation = {};
  2836. const otherAPIPayload = thisEventView.getEventsAPIPayload(otherLocation);
  2837. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2838. expect(results).toBe(false);
  2839. });
  2840. it('is similar when irrelevant query string keys are present in the Location object', function () {
  2841. const thisEventView = new EventView(state);
  2842. const location = {
  2843. query: {
  2844. bestCountry: 'canada',
  2845. },
  2846. };
  2847. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2848. const otherLocation = {};
  2849. const otherAPIPayload = thisEventView.getEventsAPIPayload(otherLocation);
  2850. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2851. expect(results).toBe(true);
  2852. });
  2853. it('is not similar on sort key sorted in opposite directions', function () {
  2854. const thisEventView = new EventView(state);
  2855. const location = {};
  2856. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2857. const otherEventView = thisEventView.sortOnField({field: 'count()'}, meta);
  2858. const otherLocation = {};
  2859. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2860. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2861. expect(results).toBe(false);
  2862. });
  2863. it('is not similar when a new column is added', function () {
  2864. const thisEventView = new EventView(state);
  2865. const location = {};
  2866. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2867. const otherEventView = thisEventView.clone();
  2868. otherEventView.fields.push({field: 'title', width: COL_WIDTH_UNDEFINED});
  2869. const otherLocation = {};
  2870. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2871. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2872. expect(results).toBe(false);
  2873. });
  2874. it('is similar when a column is updated with no changes', function () {
  2875. const thisEventView = new EventView(state);
  2876. const location = {};
  2877. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2878. const newColumn = {
  2879. kind: 'function',
  2880. function: ['count', ''],
  2881. };
  2882. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2883. const otherLocation = {};
  2884. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2885. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2886. expect(results).toBe(true);
  2887. });
  2888. it('is not similar when a column is updated with a replaced field', function () {
  2889. const thisEventView = new EventView(state);
  2890. const location = {};
  2891. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2892. const newColumn = {
  2893. kind: 'field',
  2894. field: 'title',
  2895. };
  2896. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2897. const otherLocation = {};
  2898. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2899. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2900. expect(results).toBe(false);
  2901. });
  2902. it('is not similar when a column is updated with a replaced aggregation', function () {
  2903. const thisEventView = new EventView(state);
  2904. const location = {};
  2905. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2906. const newColumn = {
  2907. kind: 'function',
  2908. function: ['avg', ''],
  2909. };
  2910. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2911. const otherLocation = {};
  2912. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2913. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2914. expect(results).toBe(false);
  2915. });
  2916. it('is similar when a column is renamed', function () {
  2917. const thisEventView = new EventView(state);
  2918. const location = {};
  2919. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2920. const newColumn = {
  2921. kind: 'function',
  2922. function: ['count', ''],
  2923. };
  2924. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2925. const otherLocation = {};
  2926. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2927. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2928. expect(results).toBe(true);
  2929. });
  2930. it('is not similar when a column is deleted', function () {
  2931. const thisEventView = new EventView(state);
  2932. const location = {};
  2933. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2934. const otherEventView = thisEventView.withDeletedColumn(0, meta);
  2935. const otherLocation = {};
  2936. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2937. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2938. expect(results).toBe(false);
  2939. });
  2940. it('is similar if column order changes', function () {
  2941. const thisEventView = new EventView(state);
  2942. const location = {};
  2943. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2944. state.fields.reverse();
  2945. const otherEventView = new EventView(state);
  2946. const otherLocation = {};
  2947. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2948. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2949. expect(results).toBe(true);
  2950. });
  2951. it('is similar if equation order relatively same', function () {
  2952. const equationField = {field: 'equation|failure_count() / count()'};
  2953. const otherEquationField = {field: 'equation|failure_count() / 2'};
  2954. state.fields = [
  2955. {field: 'project.id'},
  2956. {field: 'count()'},
  2957. equationField,
  2958. otherEquationField,
  2959. ];
  2960. const thisEventView = new EventView(state);
  2961. const location = {};
  2962. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2963. state.fields = [
  2964. equationField,
  2965. {field: 'project.id'},
  2966. {field: 'count()'},
  2967. otherEquationField,
  2968. ];
  2969. const otherEventView = new EventView(state);
  2970. const otherLocation = {};
  2971. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2972. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2973. expect(results).toBe(true);
  2974. });
  2975. it('is not similar if equation order changes', function () {
  2976. const equationField = {field: 'equation|failure_count() / count()'};
  2977. const otherEquationField = {field: 'equation|failure_count() / 2'};
  2978. state.fields = [
  2979. {field: 'project.id'},
  2980. {field: 'count()'},
  2981. equationField,
  2982. otherEquationField,
  2983. ];
  2984. const thisEventView = new EventView(state);
  2985. const location = {};
  2986. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2987. state.fields = [
  2988. {field: 'project.id'},
  2989. {field: 'count()'},
  2990. otherEquationField,
  2991. equationField,
  2992. ];
  2993. const otherEventView = new EventView(state);
  2994. const otherLocation = {};
  2995. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2996. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2997. expect(results).toBe(false);
  2998. });
  2999. });
  3000. describe('getFacetsAPIPayload', function () {
  3001. it('only includes relevant parameters', function () {
  3002. const thisEventView = new EventView(state);
  3003. const location = {};
  3004. const results = thisEventView.getFacetsAPIPayload(location);
  3005. const expected = {
  3006. query: state.query,
  3007. project: ['42'],
  3008. statsPeriod: state.statsPeriod,
  3009. environment: state.environment,
  3010. };
  3011. expect(results).toEqual(expected);
  3012. });
  3013. it('is similar on sort key sorted in opposite directions', function () {
  3014. const thisEventView = new EventView(state);
  3015. const location = {};
  3016. const thisAPIPayload = thisEventView.getFacetsAPIPayload(location);
  3017. const newColumn = {
  3018. kind: 'function',
  3019. function: ['count', ''],
  3020. };
  3021. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  3022. const otherLocation = {};
  3023. const otherAPIPayload = otherEventView.getFacetsAPIPayload(otherLocation);
  3024. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3025. expect(results).toBe(true);
  3026. });
  3027. it('is similar when a columns are different', function () {
  3028. const thisEventView = new EventView(state);
  3029. const location = {};
  3030. const thisAPIPayload = thisEventView.getFacetsAPIPayload(location);
  3031. const otherEventView = thisEventView.clone();
  3032. otherEventView.fields.push({field: 'title', width: COL_WIDTH_UNDEFINED});
  3033. const otherLocation = {};
  3034. const otherAPIPayload = otherEventView.getFacetsAPIPayload(otherLocation);
  3035. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3036. expect(results).toBe(true);
  3037. });
  3038. });
  3039. });
  3040. describe('pickRelevantLocationQueryStrings', function () {
  3041. it('picks relevant query strings', function () {
  3042. const location = {
  3043. query: {
  3044. project: 'project',
  3045. environment: 'environment',
  3046. start: 'start',
  3047. end: 'end',
  3048. utc: 'utc',
  3049. statsPeriod: 'statsPeriod',
  3050. cursor: 'cursor',
  3051. // irrelevant query strings
  3052. bestCountry: 'canada',
  3053. },
  3054. };
  3055. const actual = pickRelevantLocationQueryStrings(location);
  3056. const expected = {
  3057. start: 'start',
  3058. end: 'end',
  3059. utc: 'utc',
  3060. statsPeriod: 'statsPeriod',
  3061. cursor: 'cursor',
  3062. };
  3063. expect(actual).toEqual(expected);
  3064. });
  3065. });