list-query-builder.e2e-spec.ts 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. import { LanguageCode, LogicalOperator, SortOrder } from '@vendure/common/lib/generated-types';
  2. import { mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'node:path';
  6. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  7. import { initialData } from '../../../e2e-common/e2e-initial-data';
  8. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  9. import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
  10. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  11. import { fixPostgresTimezone } from './utils/fix-pg-timezone';
  12. import { sortById } from './utils/test-order-utils';
  13. fixPostgresTimezone();
  14. describe('ListQueryBuilder', () => {
  15. const { server, adminClient, shopClient } = createTestEnvironment(
  16. mergeConfig(testConfig(), {
  17. apiOptions: {
  18. shopListQueryLimit: 10,
  19. adminListQueryLimit: 30,
  20. },
  21. plugins: [ListQueryPlugin],
  22. }),
  23. );
  24. beforeAll(async () => {
  25. await server.init({
  26. initialData,
  27. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  28. customerCount: 3,
  29. });
  30. await adminClient.asSuperAdmin();
  31. }, TEST_SETUP_TIMEOUT_MS);
  32. afterAll(async () => {
  33. await server.destroy();
  34. });
  35. function getItemLabels(items: any[]): string[] {
  36. return items.map((x: any) => x.label).sort();
  37. }
  38. describe('pagination', () => {
  39. it('all en', async () => {
  40. const { testEntities } = await adminClient.query(
  41. GET_LIST,
  42. {
  43. options: {},
  44. },
  45. { languageCode: LanguageCode.en },
  46. );
  47. expect(testEntities.totalItems).toBe(6);
  48. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  49. expect(testEntities.items.map((i: any) => i.name)).toEqual(
  50. expect.arrayContaining([
  51. 'apple',
  52. 'bike',
  53. 'cake',
  54. 'dog',
  55. 'egg',
  56. 'baum', // if default en lang does not exist, use next available lang
  57. ]),
  58. );
  59. });
  60. it('all de', async () => {
  61. const { testEntities } = await adminClient.query(
  62. GET_LIST,
  63. {
  64. options: {},
  65. },
  66. { languageCode: LanguageCode.de },
  67. );
  68. expect(testEntities.totalItems).toBe(6);
  69. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  70. expect(testEntities.items.map((i: any) => i.name)).toEqual(
  71. expect.arrayContaining([
  72. 'apfel',
  73. 'fahrrad',
  74. 'kuchen',
  75. 'hund',
  76. 'egg', // falls back to en translation when de doesn't exist
  77. 'baum',
  78. ]),
  79. );
  80. });
  81. it('take', async () => {
  82. const { testEntities } = await adminClient.query(GET_LIST, {
  83. options: {
  84. take: 2,
  85. },
  86. });
  87. expect(testEntities.totalItems).toBe(6);
  88. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  89. });
  90. it('skip', async () => {
  91. const { testEntities } = await adminClient.query(GET_LIST, {
  92. options: {
  93. skip: 2,
  94. },
  95. });
  96. expect(testEntities.totalItems).toBe(6);
  97. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  98. });
  99. it('skip negative is ignored', async () => {
  100. const { testEntities } = await adminClient.query(GET_LIST, {
  101. options: {
  102. skip: -1,
  103. },
  104. });
  105. expect(testEntities.totalItems).toBe(6);
  106. expect(testEntities.items.length).toBe(6);
  107. });
  108. it('take zero is ignored', async () => {
  109. const { testEntities } = await adminClient.query(GET_LIST, {
  110. options: {
  111. take: 0,
  112. },
  113. });
  114. expect(testEntities.totalItems).toBe(6);
  115. expect(testEntities.items.length).toBe(6);
  116. });
  117. it('take negative is ignored', async () => {
  118. const { testEntities } = await adminClient.query(GET_LIST, {
  119. options: {
  120. take: -1,
  121. },
  122. });
  123. expect(testEntities.totalItems).toBe(6);
  124. expect(testEntities.items.length).toBe(6);
  125. });
  126. it(
  127. 'take beyond adminListQueryLimit',
  128. assertThrowsWithMessage(async () => {
  129. await adminClient.query(GET_LIST, {
  130. options: {
  131. take: 50,
  132. },
  133. });
  134. }, 'Cannot take more than 30 results from a list query'),
  135. );
  136. it(
  137. 'take beyond shopListQueryLimit',
  138. assertThrowsWithMessage(async () => {
  139. await shopClient.query(GET_LIST, {
  140. options: {
  141. take: 50,
  142. },
  143. });
  144. }, 'Cannot take more than 10 results from a list query'),
  145. );
  146. });
  147. describe('string filtering', () => {
  148. it('eq', async () => {
  149. const { testEntities } = await adminClient.query(GET_LIST, {
  150. options: {
  151. filter: {
  152. label: {
  153. eq: 'B',
  154. },
  155. },
  156. },
  157. });
  158. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  159. });
  160. it('notEq', async () => {
  161. const { testEntities } = await adminClient.query(GET_LIST, {
  162. options: {
  163. filter: {
  164. label: {
  165. notEq: 'B',
  166. },
  167. },
  168. },
  169. });
  170. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E', 'F']);
  171. });
  172. it('contains', async () => {
  173. const { testEntities } = await adminClient.query(GET_LIST, {
  174. options: {
  175. filter: {
  176. description: {
  177. contains: 'adip',
  178. },
  179. },
  180. },
  181. });
  182. expect(getItemLabels(testEntities.items)).toEqual(['C']);
  183. });
  184. it('notContains', async () => {
  185. const { testEntities } = await adminClient.query(GET_LIST, {
  186. options: {
  187. filter: {
  188. description: {
  189. notContains: 'te',
  190. },
  191. },
  192. },
  193. });
  194. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E', 'F']);
  195. });
  196. it('in', async () => {
  197. const { testEntities } = await adminClient.query(GET_LIST, {
  198. options: {
  199. filter: {
  200. label: {
  201. in: ['A', 'C'],
  202. },
  203. },
  204. },
  205. });
  206. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
  207. });
  208. it('notIn', async () => {
  209. const { testEntities } = await adminClient.query(GET_LIST, {
  210. options: {
  211. filter: {
  212. label: {
  213. notIn: ['A', 'C'],
  214. },
  215. },
  216. },
  217. });
  218. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E', 'F']);
  219. });
  220. it('isNull true', async () => {
  221. const { testEntities } = await adminClient.query(GET_LIST, {
  222. options: {
  223. filter: {
  224. nullableString: {
  225. isNull: true,
  226. },
  227. },
  228. },
  229. });
  230. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  231. });
  232. it('isNull false', async () => {
  233. const { testEntities } = await adminClient.query(GET_LIST, {
  234. options: {
  235. filter: {
  236. nullableString: {
  237. isNull: false,
  238. },
  239. },
  240. },
  241. });
  242. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  243. });
  244. it('filtering on translatable string', async () => {
  245. const { testEntities } = await adminClient.query(GET_LIST_WITH_TRANSLATIONS, {
  246. options: {
  247. filter: {
  248. name: {
  249. contains: 'g',
  250. },
  251. },
  252. },
  253. });
  254. expect(getItemLabels(testEntities.items)).toEqual(['D', 'E']);
  255. });
  256. describe('regex', () => {
  257. it('simple substring', async () => {
  258. const { testEntities } = await adminClient.query(GET_LIST, {
  259. options: {
  260. filter: {
  261. description: {
  262. regex: 'or',
  263. },
  264. },
  265. },
  266. });
  267. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  268. });
  269. it('start of string', async () => {
  270. const { testEntities } = await adminClient.query(GET_LIST, {
  271. options: {
  272. filter: {
  273. description: {
  274. regex: '^in',
  275. },
  276. },
  277. },
  278. });
  279. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  280. });
  281. it('end of string', async () => {
  282. const { testEntities } = await adminClient.query(GET_LIST, {
  283. options: {
  284. filter: {
  285. description: {
  286. regex: 'or$',
  287. },
  288. },
  289. },
  290. });
  291. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  292. });
  293. it('alternation', async () => {
  294. const { testEntities } = await adminClient.query(GET_LIST, {
  295. options: {
  296. filter: {
  297. description: {
  298. regex: 'dolor|tempor',
  299. },
  300. },
  301. },
  302. });
  303. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  304. });
  305. it('complex', async () => {
  306. const { testEntities } = await adminClient.query(GET_LIST, {
  307. options: {
  308. filter: {
  309. description: {
  310. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  311. },
  312. },
  313. },
  314. });
  315. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  316. });
  317. });
  318. });
  319. describe('ID filtering', () => {
  320. it('eq', async () => {
  321. const { testEntities } = await adminClient.query(GET_LIST, {
  322. options: {
  323. filter: {
  324. ownerId: {
  325. eq: 'T_13',
  326. },
  327. },
  328. },
  329. });
  330. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  331. });
  332. it('notEq', async () => {
  333. const { testEntities } = await adminClient.query(GET_LIST, {
  334. options: {
  335. filter: {
  336. ownerId: {
  337. notEq: 'T_13',
  338. },
  339. },
  340. },
  341. });
  342. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'E', 'F']);
  343. });
  344. it('in', async () => {
  345. const { testEntities } = await adminClient.query(GET_LIST, {
  346. options: {
  347. filter: {
  348. ownerId: {
  349. in: ['T_10', 'T_15'],
  350. },
  351. },
  352. },
  353. });
  354. expect(getItemLabels(testEntities.items)).toEqual(['A', 'F']);
  355. });
  356. it('in with empty set', async () => {
  357. const { testEntities } = await adminClient.query(GET_LIST, {
  358. options: {
  359. filter: {
  360. ownerId: {
  361. in: [],
  362. },
  363. },
  364. },
  365. });
  366. expect(getItemLabels(testEntities.items)).toEqual([]);
  367. });
  368. it('notIn', async () => {
  369. const { testEntities } = await adminClient.query(GET_LIST, {
  370. options: {
  371. filter: {
  372. ownerId: {
  373. notIn: ['T_10', 'T_15'],
  374. },
  375. },
  376. },
  377. });
  378. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
  379. });
  380. it('notIn with empty set', async () => {
  381. const { testEntities } = await adminClient.query(GET_LIST, {
  382. options: {
  383. filter: {
  384. ownerId: {
  385. notIn: [],
  386. },
  387. },
  388. },
  389. });
  390. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  391. });
  392. it('isNull true', async () => {
  393. const { testEntities } = await adminClient.query(GET_LIST, {
  394. options: {
  395. filter: {
  396. nullableId: {
  397. isNull: true,
  398. },
  399. },
  400. },
  401. });
  402. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  403. });
  404. it('isNull false', async () => {
  405. const { testEntities } = await adminClient.query(GET_LIST, {
  406. options: {
  407. filter: {
  408. nullableId: {
  409. isNull: false,
  410. },
  411. },
  412. },
  413. });
  414. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  415. });
  416. describe('regex', () => {
  417. it('simple substring', async () => {
  418. const { testEntities } = await adminClient.query(GET_LIST, {
  419. options: {
  420. filter: {
  421. description: {
  422. regex: 'or',
  423. },
  424. },
  425. },
  426. });
  427. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  428. });
  429. it('start of string', async () => {
  430. const { testEntities } = await adminClient.query(GET_LIST, {
  431. options: {
  432. filter: {
  433. description: {
  434. regex: '^in',
  435. },
  436. },
  437. },
  438. });
  439. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  440. });
  441. it('end of string', async () => {
  442. const { testEntities } = await adminClient.query(GET_LIST, {
  443. options: {
  444. filter: {
  445. description: {
  446. regex: 'or$',
  447. },
  448. },
  449. },
  450. });
  451. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  452. });
  453. it('alternation', async () => {
  454. const { testEntities } = await adminClient.query(GET_LIST, {
  455. options: {
  456. filter: {
  457. description: {
  458. regex: 'dolor|tempor',
  459. },
  460. },
  461. },
  462. });
  463. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  464. });
  465. it('complex', async () => {
  466. const { testEntities } = await adminClient.query(GET_LIST, {
  467. options: {
  468. filter: {
  469. description: {
  470. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  471. },
  472. },
  473. },
  474. });
  475. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  476. });
  477. });
  478. });
  479. describe('boolean filtering', () => {
  480. it('eq', async () => {
  481. const { testEntities } = await adminClient.query(GET_LIST, {
  482. options: {
  483. filter: {
  484. active: {
  485. eq: false,
  486. },
  487. },
  488. },
  489. });
  490. expect(getItemLabels(testEntities.items)).toEqual(['C', 'E', 'F']);
  491. });
  492. it('isNull true', async () => {
  493. const { testEntities } = await adminClient.query(GET_LIST, {
  494. options: {
  495. filter: {
  496. nullableBoolean: {
  497. isNull: true,
  498. },
  499. },
  500. },
  501. });
  502. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  503. });
  504. it('isNull false', async () => {
  505. const { testEntities } = await adminClient.query(GET_LIST, {
  506. options: {
  507. filter: {
  508. nullableBoolean: {
  509. isNull: false,
  510. },
  511. },
  512. },
  513. });
  514. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  515. });
  516. });
  517. describe('number filtering', () => {
  518. it('eq', async () => {
  519. const { testEntities } = await adminClient.query(GET_LIST, {
  520. options: {
  521. filter: {
  522. order: {
  523. eq: 1,
  524. },
  525. },
  526. },
  527. });
  528. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  529. });
  530. it('lt', async () => {
  531. const { testEntities } = await adminClient.query(GET_LIST, {
  532. options: {
  533. filter: {
  534. order: {
  535. lt: 1,
  536. },
  537. },
  538. },
  539. });
  540. expect(getItemLabels(testEntities.items)).toEqual(['A']);
  541. });
  542. it('lte', async () => {
  543. const { testEntities } = await adminClient.query(GET_LIST, {
  544. options: {
  545. filter: {
  546. order: {
  547. lte: 1,
  548. },
  549. },
  550. },
  551. });
  552. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  553. });
  554. it('gt', async () => {
  555. const { testEntities } = await adminClient.query(GET_LIST, {
  556. options: {
  557. filter: {
  558. order: {
  559. gt: 1,
  560. },
  561. },
  562. },
  563. });
  564. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  565. });
  566. it('gte', async () => {
  567. const { testEntities } = await adminClient.query(GET_LIST, {
  568. options: {
  569. filter: {
  570. order: {
  571. gte: 1,
  572. },
  573. },
  574. },
  575. });
  576. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E', 'F']);
  577. });
  578. it('between', async () => {
  579. const { testEntities } = await adminClient.query(GET_LIST, {
  580. options: {
  581. filter: {
  582. order: {
  583. between: {
  584. start: 2,
  585. end: 4,
  586. },
  587. },
  588. },
  589. },
  590. });
  591. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  592. });
  593. it('isNull true', async () => {
  594. const { testEntities } = await adminClient.query(GET_LIST, {
  595. options: {
  596. filter: {
  597. nullableNumber: {
  598. isNull: true,
  599. },
  600. },
  601. },
  602. });
  603. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  604. });
  605. it('isNull false', async () => {
  606. const { testEntities } = await adminClient.query(GET_LIST, {
  607. options: {
  608. filter: {
  609. nullableNumber: {
  610. isNull: false,
  611. },
  612. },
  613. },
  614. });
  615. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  616. });
  617. });
  618. describe('date filtering', () => {
  619. it('before', async () => {
  620. const { testEntities } = await adminClient.query(GET_LIST, {
  621. options: {
  622. filter: {
  623. date: {
  624. before: '2020-01-20T10:00:00.000Z',
  625. },
  626. },
  627. },
  628. });
  629. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  630. });
  631. it('before on same date', async () => {
  632. const { testEntities } = await adminClient.query(GET_LIST, {
  633. options: {
  634. filter: {
  635. date: {
  636. before: '2020-01-15T17:00:00.000Z',
  637. },
  638. },
  639. },
  640. });
  641. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  642. });
  643. it('after', async () => {
  644. const { testEntities } = await adminClient.query(GET_LIST, {
  645. options: {
  646. filter: {
  647. date: {
  648. after: '2020-01-20T10:00:00.000Z',
  649. },
  650. },
  651. },
  652. });
  653. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  654. });
  655. it('after on same date', async () => {
  656. const { testEntities } = await adminClient.query(GET_LIST, {
  657. options: {
  658. filter: {
  659. date: {
  660. after: '2020-01-25T09:00:00.000Z',
  661. },
  662. },
  663. },
  664. });
  665. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  666. });
  667. it('between', async () => {
  668. const { testEntities } = await adminClient.query(GET_LIST, {
  669. options: {
  670. filter: {
  671. date: {
  672. between: {
  673. start: '2020-01-10T10:00:00.000Z',
  674. end: '2020-01-20T10:00:00.000Z',
  675. },
  676. },
  677. },
  678. },
  679. });
  680. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  681. });
  682. it('isNull true', async () => {
  683. const { testEntities } = await adminClient.query(GET_LIST, {
  684. options: {
  685. filter: {
  686. nullableDate: {
  687. isNull: true,
  688. },
  689. },
  690. },
  691. });
  692. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  693. });
  694. it('isNull false', async () => {
  695. const { testEntities } = await adminClient.query(GET_LIST, {
  696. options: {
  697. filter: {
  698. nullableDate: {
  699. isNull: false,
  700. },
  701. },
  702. },
  703. });
  704. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  705. });
  706. });
  707. describe('multiple filters with filterOperator', () => {
  708. it('default AND', async () => {
  709. const { testEntities } = await adminClient.query(GET_LIST, {
  710. options: {
  711. filter: {
  712. description: {
  713. contains: 'Lorem',
  714. },
  715. active: {
  716. eq: false,
  717. },
  718. },
  719. },
  720. });
  721. expect(getItemLabels(testEntities.items)).toEqual([]);
  722. });
  723. it('explicit AND', async () => {
  724. const { testEntities } = await adminClient.query(GET_LIST, {
  725. options: {
  726. filter: {
  727. description: {
  728. contains: 'Lorem',
  729. },
  730. active: {
  731. eq: false,
  732. },
  733. },
  734. filterOperator: LogicalOperator.AND,
  735. },
  736. });
  737. expect(getItemLabels(testEntities.items)).toEqual([]);
  738. });
  739. it('explicit OR', async () => {
  740. const { testEntities } = await adminClient.query(GET_LIST, {
  741. options: {
  742. filter: {
  743. description: {
  744. contains: 'Lorem',
  745. },
  746. active: {
  747. eq: false,
  748. },
  749. },
  750. filterOperator: LogicalOperator.OR,
  751. },
  752. });
  753. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
  754. });
  755. it('explicit OR with 3 filters', async () => {
  756. const { testEntities } = await adminClient.query(GET_LIST, {
  757. options: {
  758. filter: {
  759. description: {
  760. contains: 'eiusmod',
  761. },
  762. active: {
  763. eq: false,
  764. },
  765. order: {
  766. lt: 3,
  767. },
  768. },
  769. filterOperator: LogicalOperator.OR,
  770. },
  771. });
  772. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  773. });
  774. it('explicit OR with empty filters object', async () => {
  775. const { testEntities } = await adminClient.query(GET_LIST, {
  776. options: {
  777. filter: {},
  778. filterOperator: LogicalOperator.OR,
  779. },
  780. });
  781. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  782. });
  783. });
  784. describe('complex boolean logic with _and & _or', () => {
  785. it('single _and', async () => {
  786. const { testEntities } = await adminClient.query(GET_LIST, {
  787. options: {
  788. filter: {
  789. _and: [
  790. {
  791. description: {
  792. contains: 'Lorem',
  793. },
  794. active: {
  795. eq: false,
  796. },
  797. },
  798. ],
  799. },
  800. },
  801. });
  802. expect(getItemLabels(testEntities.items)).toEqual([]);
  803. });
  804. it('single _or', async () => {
  805. const { testEntities } = await adminClient.query(GET_LIST, {
  806. options: {
  807. filter: {
  808. _or: [
  809. {
  810. description: {
  811. contains: 'Lorem',
  812. },
  813. active: {
  814. eq: false,
  815. },
  816. },
  817. ],
  818. },
  819. },
  820. });
  821. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
  822. });
  823. it('_or with nested _and', async () => {
  824. const { testEntities } = await adminClient.query(GET_LIST, {
  825. options: {
  826. filter: {
  827. _or: [
  828. {
  829. description: { contains: 'Lorem' },
  830. },
  831. {
  832. _and: [{ order: { gt: 3 } }, { order: { lt: 5 } }],
  833. },
  834. ],
  835. },
  836. },
  837. });
  838. expect(getItemLabels(testEntities.items)).toEqual(['A', 'E']);
  839. });
  840. it('_and with nested _or', async () => {
  841. const { testEntities } = await adminClient.query(GET_LIST, {
  842. options: {
  843. filter: {
  844. _and: [
  845. {
  846. description: { contains: 'e' },
  847. },
  848. {
  849. _or: [{ active: { eq: false } }, { ownerId: { eq: 'T_10' } }],
  850. },
  851. ],
  852. },
  853. },
  854. });
  855. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'F']);
  856. });
  857. });
  858. describe('sorting', () => {
  859. it('sort by string', async () => {
  860. const { testEntities } = await adminClient.query(GET_LIST, {
  861. options: {
  862. sort: {
  863. label: SortOrder.DESC,
  864. },
  865. },
  866. });
  867. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  868. });
  869. it('sort by number', async () => {
  870. const { testEntities } = await adminClient.query(GET_LIST, {
  871. options: {
  872. sort: {
  873. order: SortOrder.DESC,
  874. },
  875. },
  876. });
  877. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  878. });
  879. it('sort by date', async () => {
  880. const { testEntities } = await adminClient.query(GET_LIST, {
  881. options: {
  882. sort: {
  883. date: SortOrder.DESC,
  884. },
  885. },
  886. });
  887. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  888. });
  889. it('sort by ID', async () => {
  890. const { testEntities } = await adminClient.query(GET_LIST, {
  891. options: {
  892. sort: {
  893. id: SortOrder.DESC,
  894. },
  895. },
  896. });
  897. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  898. });
  899. it('sort by translated field en', async () => {
  900. const { testEntities } = await adminClient.query(GET_LIST, {
  901. options: {
  902. sort: {
  903. name: SortOrder.ASC,
  904. },
  905. },
  906. });
  907. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  908. 'apple',
  909. 'baum', // falling back to de here
  910. 'bike',
  911. 'cake',
  912. 'dog',
  913. 'egg',
  914. ]);
  915. });
  916. it('sort by translated field de', async () => {
  917. const { testEntities } = await adminClient.query(
  918. GET_LIST,
  919. {
  920. options: {
  921. sort: {
  922. name: SortOrder.ASC,
  923. },
  924. },
  925. },
  926. { languageCode: LanguageCode.de },
  927. );
  928. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  929. 'apfel',
  930. 'baum',
  931. 'egg',
  932. 'fahrrad',
  933. 'hund',
  934. 'kuchen',
  935. ]);
  936. });
  937. it('sort by translated field en with take', async () => {
  938. const { testEntities } = await adminClient.query(GET_LIST, {
  939. options: {
  940. sort: {
  941. name: SortOrder.ASC,
  942. },
  943. take: 4,
  944. },
  945. });
  946. expect(testEntities.items.map((x: any) => x.name)).toEqual(['apple', 'baum', 'bike', 'cake']);
  947. });
  948. it('sort by translated field de with take', async () => {
  949. const { testEntities } = await adminClient.query(
  950. GET_LIST,
  951. {
  952. options: {
  953. sort: {
  954. name: SortOrder.ASC,
  955. },
  956. take: 4,
  957. },
  958. },
  959. { languageCode: LanguageCode.de },
  960. );
  961. expect(testEntities.items.map((x: any) => x.name)).toEqual(['apfel', 'baum', 'egg', 'fahrrad']);
  962. });
  963. });
  964. describe('calculated fields', () => {
  965. it('filter by simple calculated property', async () => {
  966. const { testEntities } = await adminClient.query(GET_LIST, {
  967. options: {
  968. filter: {
  969. descriptionLength: {
  970. lt: 12,
  971. },
  972. },
  973. },
  974. });
  975. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  976. });
  977. it('filter by calculated property with boolean expression', async () => {
  978. const { testEntities } = await adminClient.query(GET_LIST, {
  979. options: {
  980. filter: {
  981. _and: [
  982. {
  983. descriptionLength: {
  984. lt: 12,
  985. },
  986. },
  987. ],
  988. },
  989. },
  990. });
  991. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  992. });
  993. it('filter by calculated property with join', async () => {
  994. const { testEntities } = await adminClient.query(GET_LIST, {
  995. options: {
  996. filter: {
  997. price: {
  998. lt: 14,
  999. },
  1000. },
  1001. },
  1002. });
  1003. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1004. });
  1005. it('filter by calculated property with join and boolean expression', async () => {
  1006. const { testEntities } = await adminClient.query(GET_LIST, {
  1007. options: {
  1008. filter: {
  1009. _and: [
  1010. {
  1011. price: {
  1012. lt: 14,
  1013. },
  1014. },
  1015. ],
  1016. },
  1017. },
  1018. });
  1019. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1020. });
  1021. it('sort by simple calculated property', async () => {
  1022. const { testEntities } = await adminClient.query(GET_LIST, {
  1023. options: {
  1024. sort: {
  1025. descriptionLength: SortOrder.ASC,
  1026. },
  1027. },
  1028. });
  1029. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
  1030. });
  1031. it('sort by calculated property with join', async () => {
  1032. const { testEntities } = await adminClient.query(GET_LIST, {
  1033. options: {
  1034. sort: {
  1035. price: SortOrder.ASC,
  1036. },
  1037. },
  1038. });
  1039. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
  1040. });
  1041. });
  1042. describe('multiple clauses', () => {
  1043. it('sort by translated field en & filter', async () => {
  1044. const { testEntities } = await adminClient.query(GET_LIST, {
  1045. options: {
  1046. sort: {
  1047. name: SortOrder.ASC,
  1048. },
  1049. filter: {
  1050. order: {
  1051. gte: 1,
  1052. },
  1053. },
  1054. },
  1055. });
  1056. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  1057. 'baum',
  1058. 'bike',
  1059. 'cake',
  1060. 'dog',
  1061. 'egg',
  1062. ]);
  1063. });
  1064. it('sort by translated field de & filter', async () => {
  1065. const { testEntities } = await adminClient.query(
  1066. GET_LIST,
  1067. {
  1068. options: {
  1069. sort: {
  1070. name: SortOrder.ASC,
  1071. },
  1072. filter: {
  1073. order: {
  1074. gte: 1,
  1075. },
  1076. },
  1077. },
  1078. },
  1079. { languageCode: LanguageCode.de },
  1080. );
  1081. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  1082. 'baum',
  1083. 'egg',
  1084. 'fahrrad',
  1085. 'hund',
  1086. 'kuchen',
  1087. ]);
  1088. });
  1089. it('sort by translated field de & filter & pagination', async () => {
  1090. const { testEntities } = await adminClient.query(
  1091. GET_LIST,
  1092. {
  1093. options: {
  1094. sort: {
  1095. name: SortOrder.ASC,
  1096. },
  1097. filter: {
  1098. order: {
  1099. gte: 1,
  1100. },
  1101. },
  1102. take: 2,
  1103. skip: 1,
  1104. },
  1105. },
  1106. { languageCode: LanguageCode.de },
  1107. );
  1108. expect(testEntities.items.map((x: any) => x.name)).toEqual(['egg', 'fahrrad']);
  1109. });
  1110. });
  1111. // https://github.com/vendurehq/vendure/issues/1586
  1112. it('using the getMany() of the resulting QueryBuilder', async () => {
  1113. const { testEntitiesGetMany } = await adminClient.query(GET_ARRAY_LIST, {});
  1114. const actualPrices = testEntitiesGetMany
  1115. .sort(sortById)
  1116. .map((x: any) => x.price)
  1117. .sort((a: number, b: number) => a - b);
  1118. const expectedPrices = [11, 9, 22, 14, 13, 33].sort((a, b) => a - b);
  1119. expect(actualPrices).toEqual(expectedPrices);
  1120. });
  1121. // https://github.com/vendurehq/vendure/issues/1611
  1122. describe('translations handling', () => {
  1123. const allTranslations = [
  1124. [
  1125. { languageCode: LanguageCode.en, name: 'apple' },
  1126. { languageCode: LanguageCode.de, name: 'apfel' },
  1127. ],
  1128. [
  1129. { languageCode: LanguageCode.en, name: 'bike' },
  1130. { languageCode: LanguageCode.de, name: 'fahrrad' },
  1131. ],
  1132. ];
  1133. function getTestEntityTranslations(testEntities: { items: any[] }) {
  1134. // Explicitly sort the order of the translations as it was being non-deterministic on
  1135. // the mysql CI tests.
  1136. return testEntities.items.map((e: any) =>
  1137. e.translations.sort((a: any, b: any) => (a.languageCode < b.languageCode ? 1 : -1)),
  1138. );
  1139. }
  1140. it('returns all translations with default languageCode', async () => {
  1141. const { testEntities } = await shopClient.query(GET_LIST_WITH_TRANSLATIONS, {
  1142. options: {
  1143. take: 2,
  1144. sort: {
  1145. order: SortOrder.ASC,
  1146. },
  1147. },
  1148. });
  1149. const testEntityTranslations = getTestEntityTranslations(testEntities);
  1150. expect(testEntities.items.map((e: any) => e.name)).toEqual(['apple', 'bike']);
  1151. expect(testEntityTranslations).toEqual(allTranslations);
  1152. });
  1153. it('returns all translations with non-default languageCode', async () => {
  1154. const { testEntities } = await shopClient.query(
  1155. GET_LIST_WITH_TRANSLATIONS,
  1156. {
  1157. options: {
  1158. take: 2,
  1159. sort: {
  1160. order: SortOrder.ASC,
  1161. },
  1162. },
  1163. },
  1164. { languageCode: LanguageCode.de },
  1165. );
  1166. const testEntityTranslations = getTestEntityTranslations(testEntities);
  1167. expect(testEntities.items.map((e: any) => e.name)).toEqual(['apfel', 'fahrrad']);
  1168. expect(testEntityTranslations).toEqual(allTranslations);
  1169. });
  1170. });
  1171. describe('customPropertyMap', () => {
  1172. it('filter by custom string field', async () => {
  1173. const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
  1174. options: {
  1175. sort: {
  1176. label: SortOrder.ASC,
  1177. },
  1178. filter: {
  1179. customerLastName: { contains: 'zieme' },
  1180. },
  1181. },
  1182. });
  1183. expect(testEntities.items).toEqual([
  1184. {
  1185. id: 'T_1',
  1186. label: 'A',
  1187. name: 'apple',
  1188. parent: {
  1189. id: 'T_2',
  1190. label: 'B',
  1191. name: 'bike',
  1192. },
  1193. orderRelation: {
  1194. customer: {
  1195. firstName: 'Hayden',
  1196. lastName: 'Zieme',
  1197. },
  1198. id: 'T_1',
  1199. },
  1200. },
  1201. {
  1202. id: 'T_4',
  1203. label: 'D',
  1204. name: 'dog',
  1205. parent: {
  1206. id: 'T_2',
  1207. label: 'B',
  1208. name: 'bike',
  1209. },
  1210. orderRelation: {
  1211. customer: {
  1212. firstName: 'Hayden',
  1213. lastName: 'Zieme',
  1214. },
  1215. id: 'T_4',
  1216. },
  1217. },
  1218. ]);
  1219. });
  1220. it('sort by custom string field', async () => {
  1221. const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
  1222. options: {
  1223. sort: {
  1224. customerLastName: SortOrder.ASC,
  1225. },
  1226. },
  1227. });
  1228. expect(testEntities.items.map((i: any) => i.orderRelation.customer)).toEqual([
  1229. { firstName: 'Trevor', lastName: 'Donnelly' },
  1230. { firstName: 'Trevor', lastName: 'Donnelly' },
  1231. { firstName: 'Marques', lastName: 'Sawayn' },
  1232. { firstName: 'Marques', lastName: 'Sawayn' },
  1233. { firstName: 'Hayden', lastName: 'Zieme' },
  1234. { firstName: 'Hayden', lastName: 'Zieme' },
  1235. ]);
  1236. });
  1237. });
  1238. describe('relations in customFields', () => {
  1239. it('should resolve relations in customFields successfully', async () => {
  1240. const { testEntities } = await shopClient.query(GET_LIST_WITH_CUSTOM_FIELD_RELATION, {
  1241. options: {
  1242. filter: {
  1243. label: { eq: 'A' },
  1244. },
  1245. },
  1246. });
  1247. expect(testEntities.items).toEqual([
  1248. {
  1249. id: 'T_1',
  1250. label: 'A',
  1251. customFields: {
  1252. relation: [{ id: 'T_1', data: 'A' }],
  1253. },
  1254. },
  1255. ]);
  1256. });
  1257. it('should resolve multiple relations in customFields successfully', async () => {
  1258. const { testEntities } = await shopClient.query(GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION, {
  1259. options: {
  1260. filter: {
  1261. label: { eq: 'A' },
  1262. },
  1263. },
  1264. });
  1265. expect(testEntities.items).toEqual([
  1266. {
  1267. id: 'T_1',
  1268. label: 'A',
  1269. customFields: {
  1270. relation: [{ id: 'T_1', data: 'A' }],
  1271. otherRelation: [{ id: 'T_1', data: 'A' }],
  1272. },
  1273. },
  1274. ]);
  1275. });
  1276. });
  1277. // https://github.com/vendurehq/vendure/issues/3267
  1278. describe('filtering with duplicate custom property fields in _and blocks', () => {
  1279. it('filters by single tagId', async () => {
  1280. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1281. options: {
  1282. sort: { label: SortOrder.ASC },
  1283. filter: {
  1284. tagId: { eq: 'T_1' }, // tag1
  1285. },
  1286. },
  1287. });
  1288. // Entities with tag1: A, B, C, E
  1289. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'E']);
  1290. });
  1291. it('filters by multiple tagIds with _and (same field used twice)', async () => {
  1292. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1293. options: {
  1294. sort: { label: SortOrder.ASC },
  1295. filter: {
  1296. _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }], // tag1 AND tag2
  1297. },
  1298. },
  1299. });
  1300. // Entities with BOTH tag1 AND tag2: A, B, E
  1301. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1302. });
  1303. it('filters by three tagIds with _and (same field used three times)', async () => {
  1304. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1305. options: {
  1306. sort: { label: SortOrder.ASC },
  1307. filter: {
  1308. _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }, { tagId: { eq: 'T_3' } }], // tag1 AND tag2 AND tag3
  1309. },
  1310. },
  1311. });
  1312. // Only entity E has all three tags
  1313. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1314. });
  1315. it('filters by tagIds with _or (same field used twice)', async () => {
  1316. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1317. options: {
  1318. sort: { label: SortOrder.ASC },
  1319. filter: {
  1320. _or: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }], // tag1 OR tag2
  1321. },
  1322. },
  1323. });
  1324. // Entities with tag1 OR tag2: A, B, C, D, E
  1325. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
  1326. });
  1327. it('filters by tagId combined with other fields in _and', async () => {
  1328. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1329. options: {
  1330. sort: { label: SortOrder.ASC },
  1331. filter: {
  1332. _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }, { active: { eq: true } }],
  1333. },
  1334. },
  1335. });
  1336. // Entities with tag1 AND tag2 AND active=true: A, B (E is not active)
  1337. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  1338. });
  1339. // ===== NESTED _and WITHIN _and =====
  1340. it('filters with nested _and within _and (same field)', async () => {
  1341. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1342. options: {
  1343. sort: { label: SortOrder.ASC },
  1344. filter: {
  1345. _and: [
  1346. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }] },
  1347. { tagId: { eq: 'T_3' } },
  1348. ],
  1349. },
  1350. },
  1351. });
  1352. // (tag1 AND tag2) AND tag3 => Only E has all three
  1353. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1354. });
  1355. it('filters with deeply nested _and (three levels)', async () => {
  1356. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1357. options: {
  1358. sort: { label: SortOrder.ASC },
  1359. filter: {
  1360. _and: [
  1361. {
  1362. _and: [{ tagId: { eq: 'T_1' } }, { _and: [{ tagId: { eq: 'T_2' } }] }],
  1363. },
  1364. ],
  1365. },
  1366. },
  1367. });
  1368. // Deeply nested: tag1 AND tag2 => A, B, E
  1369. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1370. });
  1371. // ===== _and WITHIN _or =====
  1372. it('filters with _and within _or (same field in inner _and)', async () => {
  1373. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1374. options: {
  1375. sort: { label: SortOrder.ASC },
  1376. filter: {
  1377. _or: [
  1378. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }] },
  1379. { label: { eq: 'F' } },
  1380. ],
  1381. },
  1382. },
  1383. });
  1384. // (tag1 AND tag2) OR label='F' => A, B, E (have both tags) + F (by label)
  1385. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E', 'F']);
  1386. });
  1387. it('filters with multiple _and blocks within _or', async () => {
  1388. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1389. options: {
  1390. sort: { label: SortOrder.ASC },
  1391. filter: {
  1392. _or: [
  1393. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_3' } }] },
  1394. { _and: [{ tagId: { eq: 'T_2' } }, { active: { eq: true } }] },
  1395. ],
  1396. },
  1397. },
  1398. });
  1399. // (tag1 AND tag3) => E only
  1400. // OR (tag2 AND active) => A, B, D
  1401. // Combined: A, B, D, E
  1402. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D', 'E']);
  1403. });
  1404. // ===== _or WITHIN _and =====
  1405. it('filters with _or within _and (same field in inner _or)', async () => {
  1406. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1407. options: {
  1408. sort: { label: SortOrder.ASC },
  1409. filter: {
  1410. _and: [
  1411. { _or: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }] },
  1412. { active: { eq: true } },
  1413. ],
  1414. },
  1415. },
  1416. });
  1417. // (tag1 OR tag2) AND active => A, B, D (C has tag1 but inactive, E has both but inactive)
  1418. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  1419. });
  1420. it('filters with same field in both _or and _and at different levels', async () => {
  1421. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1422. options: {
  1423. sort: { label: SortOrder.ASC },
  1424. filter: {
  1425. _and: [
  1426. { tagId: { eq: 'T_1' } },
  1427. { _or: [{ tagId: { eq: 'T_2' } }, { tagId: { eq: 'T_3' } }] },
  1428. ],
  1429. },
  1430. },
  1431. });
  1432. // tag1 AND (tag2 OR tag3)
  1433. // tag1: A, B, C, E
  1434. // tag2 OR tag3: A, B, D, E
  1435. // Intersection: A, B, E
  1436. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1437. });
  1438. // ===== COMPLEX MIXED SCENARIOS =====
  1439. it('filters with complex nested structure (_and containing _or containing _and)', async () => {
  1440. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1441. options: {
  1442. sort: { label: SortOrder.ASC },
  1443. filter: {
  1444. _and: [
  1445. {
  1446. _or: [
  1447. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_3' } }] },
  1448. { label: { eq: 'D' } },
  1449. ],
  1450. },
  1451. { active: { eq: false } },
  1452. ],
  1453. },
  1454. },
  1455. });
  1456. // ((tag1 AND tag3) OR label='D') AND inactive
  1457. // (tag1 AND tag3): E
  1458. // label='D': D
  1459. // Combined for OR: D, E
  1460. // AND inactive: E only (D is active)
  1461. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1462. });
  1463. it('filters with same field appearing at multiple nesting levels', async () => {
  1464. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1465. options: {
  1466. sort: { label: SortOrder.ASC },
  1467. filter: {
  1468. _and: [
  1469. { tagId: { eq: 'T_1' } },
  1470. {
  1471. _and: [
  1472. { tagId: { eq: 'T_2' } },
  1473. { _or: [{ tagId: { eq: 'T_3' } }, { active: { eq: true } }] },
  1474. ],
  1475. },
  1476. ],
  1477. },
  1478. },
  1479. });
  1480. // tag1 AND (tag2 AND (tag3 OR active))
  1481. // tag1: A, B, C, E
  1482. // tag2: A, B, D, E
  1483. // tag3 OR active: E (tag3) + A, B, D (active) = A, B, D, E
  1484. // tag1 AND tag2: A, B, E
  1485. // tag1 AND tag2 AND (tag3 OR active): A, B, E (E has tag3, A and B are active)
  1486. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1487. });
  1488. // ===== USING "in" OPERATOR WITH SAME FIELD =====
  1489. it('filters with "in" operator combined with "eq" on same field', async () => {
  1490. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1491. options: {
  1492. sort: { label: SortOrder.ASC },
  1493. filter: {
  1494. _and: [{ tagId: { in: ['T_1', 'T_2'] } }, { tagId: { eq: 'T_3' } }],
  1495. },
  1496. },
  1497. });
  1498. // (tag1 or tag2) AND tag3 => Only E has tag3 and also has tag1 or tag2
  1499. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1500. });
  1501. it('filters with multiple "in" operators on same field in _and', async () => {
  1502. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1503. options: {
  1504. sort: { label: SortOrder.ASC },
  1505. filter: {
  1506. _and: [{ tagId: { in: ['T_1'] } }, { tagId: { in: ['T_2'] } }],
  1507. },
  1508. },
  1509. });
  1510. // Must have tag1 AND must have tag2 => A, B, E
  1511. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1512. });
  1513. // ===== TOP-LEVEL SAME FIELD (regression test) =====
  1514. it('handles same field at top level (outside _and/_or)', async () => {
  1515. // This tests that a single use of a *-to-Many field still works correctly
  1516. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1517. options: {
  1518. sort: { label: SortOrder.ASC },
  1519. filter: {
  1520. tagId: { in: ['T_1', 'T_2'] },
  1521. },
  1522. },
  1523. });
  1524. // Entities with tag1 OR tag2: A, B, C, D, E
  1525. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
  1526. });
  1527. it('handles single tagId filter combined with top-level _and', async () => {
  1528. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1529. options: {
  1530. sort: { label: SortOrder.ASC },
  1531. filter: {
  1532. _and: [{ tagId: { eq: 'T_3' } }, { active: { eq: false } }],
  1533. },
  1534. },
  1535. });
  1536. // tag3 AND inactive => E only (only entity with tag3, and it's inactive)
  1537. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1538. });
  1539. // ===== filterOperator: OR at root level =====
  1540. it('respects filterOperator OR with duplicate tagId filters', async () => {
  1541. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1542. options: {
  1543. sort: { label: SortOrder.ASC },
  1544. filterOperator: LogicalOperator.OR,
  1545. filter: {
  1546. tagId: { eq: 'T_3' },
  1547. label: { eq: 'F' },
  1548. },
  1549. },
  1550. });
  1551. // tag3 OR label='F' => E (has tag3) + F (by label)
  1552. expect(getItemLabels(testEntities.items)).toEqual(['E', 'F']);
  1553. });
  1554. // ===== EDGE CASES =====
  1555. it('handles empty _and array gracefully', async () => {
  1556. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1557. options: {
  1558. sort: { label: SortOrder.ASC },
  1559. filter: {
  1560. _and: [],
  1561. },
  1562. },
  1563. });
  1564. // Empty _and should return all entities
  1565. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  1566. });
  1567. it('handles _and with single condition on *-to-Many field', async () => {
  1568. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1569. options: {
  1570. sort: { label: SortOrder.ASC },
  1571. filter: {
  1572. _and: [{ tagId: { eq: 'T_3' } }],
  1573. },
  1574. },
  1575. });
  1576. // Single condition in _and: tag3 => E only
  1577. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1578. });
  1579. it('handles notEq operator on *-to-Many field', async () => {
  1580. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1581. options: {
  1582. sort: { label: SortOrder.ASC },
  1583. filter: {
  1584. tagId: { notEq: 'T_3' },
  1585. },
  1586. },
  1587. });
  1588. // Entities that have at least one tag that is NOT tag3
  1589. // A (tag1, tag2), B (tag1, tag2), C (tag1), D (tag2), E (tag1, tag2, tag3 - has non-T_3 tags)
  1590. // F has no tags, so no tag satisfies notEq
  1591. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
  1592. });
  1593. it('handles BETWEEN operator on *-to-Many field', async () => {
  1594. // Tag priorities: tag1=10, tag2=20, tag3=30
  1595. // Find entities with tags having priority between 15 and 25 (i.e., tag2 with priority 20)
  1596. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1597. options: {
  1598. sort: { label: SortOrder.ASC },
  1599. filter: {
  1600. tagPriority: { between: { start: 15, end: 25 } },
  1601. },
  1602. },
  1603. });
  1604. // Entities with tag2 (priority 20): A, B, D, E
  1605. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D', 'E']);
  1606. });
  1607. it('handles BETWEEN with _and on *-to-Many field', async () => {
  1608. // Find entities with tags having priority between 5 and 15 (tag1=10)
  1609. // AND tags having priority between 25 and 35 (tag3=30)
  1610. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1611. options: {
  1612. sort: { label: SortOrder.ASC },
  1613. filter: {
  1614. _and: [
  1615. { tagPriority: { between: { start: 5, end: 15 } } },
  1616. { tagPriority: { between: { start: 25, end: 35 } } },
  1617. ],
  1618. },
  1619. },
  1620. });
  1621. // Only E has both tag1 (priority 10) and tag3 (priority 30)
  1622. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1623. });
  1624. });
  1625. });
  1626. const GET_LIST = gql`
  1627. query GetTestEntities($options: TestEntityListOptions) {
  1628. testEntities(options: $options) {
  1629. totalItems
  1630. items {
  1631. id
  1632. label
  1633. name
  1634. date
  1635. }
  1636. }
  1637. }
  1638. `;
  1639. const GET_LIST_WITH_TRANSLATIONS = gql`
  1640. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1641. testEntities(options: $options) {
  1642. totalItems
  1643. items {
  1644. id
  1645. label
  1646. name
  1647. date
  1648. translations {
  1649. languageCode
  1650. name
  1651. }
  1652. }
  1653. }
  1654. }
  1655. `;
  1656. const GET_LIST_WITH_ORDERS = gql`
  1657. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1658. testEntities(options: $options) {
  1659. totalItems
  1660. items {
  1661. id
  1662. label
  1663. name
  1664. parent {
  1665. id
  1666. label
  1667. name
  1668. }
  1669. orderRelation {
  1670. id
  1671. customer {
  1672. firstName
  1673. lastName
  1674. }
  1675. }
  1676. }
  1677. }
  1678. }
  1679. `;
  1680. const GET_ARRAY_LIST = gql`
  1681. query GetTestEntitiesArray($options: TestEntityListOptions) {
  1682. testEntitiesGetMany(options: $options) {
  1683. id
  1684. label
  1685. name
  1686. date
  1687. price
  1688. }
  1689. }
  1690. `;
  1691. const GET_LIST_WITH_CUSTOM_FIELD_RELATION = gql`
  1692. query GetTestWithCustomFieldRelation($options: TestEntityListOptions) {
  1693. testEntities(options: $options) {
  1694. items {
  1695. id
  1696. label
  1697. customFields {
  1698. relation {
  1699. id
  1700. data
  1701. }
  1702. }
  1703. }
  1704. }
  1705. }
  1706. `;
  1707. const GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION = gql`
  1708. query GetTestWithMultipleCustomFieldRelation($options: TestEntityListOptions) {
  1709. testEntities(options: $options) {
  1710. items {
  1711. id
  1712. label
  1713. customFields {
  1714. relation {
  1715. id
  1716. data
  1717. }
  1718. otherRelation {
  1719. id
  1720. data
  1721. }
  1722. }
  1723. }
  1724. }
  1725. }
  1726. `;
  1727. const GET_LIST_WITH_TAGS = gql`
  1728. query GetTestEntitiesWithTags($options: TestEntityListOptions) {
  1729. testEntities(options: $options) {
  1730. totalItems
  1731. items {
  1732. id
  1733. label
  1734. tags {
  1735. id
  1736. name
  1737. }
  1738. }
  1739. }
  1740. }
  1741. `;