eventView.spec.jsx 96 KB

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