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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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/vendure-ecommerce/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/vendure-ecommerce/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. });
  1278. const GET_LIST = gql`
  1279. query GetTestEntities($options: TestEntityListOptions) {
  1280. testEntities(options: $options) {
  1281. totalItems
  1282. items {
  1283. id
  1284. label
  1285. name
  1286. date
  1287. }
  1288. }
  1289. }
  1290. `;
  1291. const GET_LIST_WITH_TRANSLATIONS = gql`
  1292. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1293. testEntities(options: $options) {
  1294. totalItems
  1295. items {
  1296. id
  1297. label
  1298. name
  1299. date
  1300. translations {
  1301. languageCode
  1302. name
  1303. }
  1304. }
  1305. }
  1306. }
  1307. `;
  1308. const GET_LIST_WITH_ORDERS = gql`
  1309. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1310. testEntities(options: $options) {
  1311. totalItems
  1312. items {
  1313. id
  1314. label
  1315. name
  1316. parent {
  1317. id
  1318. label
  1319. name
  1320. }
  1321. orderRelation {
  1322. id
  1323. customer {
  1324. firstName
  1325. lastName
  1326. }
  1327. }
  1328. }
  1329. }
  1330. }
  1331. `;
  1332. const GET_ARRAY_LIST = gql`
  1333. query GetTestEntitiesArray($options: TestEntityListOptions) {
  1334. testEntitiesGetMany(options: $options) {
  1335. id
  1336. label
  1337. name
  1338. date
  1339. price
  1340. }
  1341. }
  1342. `;
  1343. const GET_LIST_WITH_CUSTOM_FIELD_RELATION = gql`
  1344. query GetTestWithCustomFieldRelation($options: TestEntityListOptions) {
  1345. testEntities(options: $options) {
  1346. items {
  1347. id
  1348. label
  1349. customFields {
  1350. relation {
  1351. id
  1352. data
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358. `;
  1359. const GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION = gql`
  1360. query GetTestWithMultipleCustomFieldRelation($options: TestEntityListOptions) {
  1361. testEntities(options: $options) {
  1362. items {
  1363. id
  1364. label
  1365. customFields {
  1366. relation {
  1367. id
  1368. data
  1369. }
  1370. otherRelation {
  1371. id
  1372. data
  1373. }
  1374. }
  1375. }
  1376. }
  1377. }
  1378. `;