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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. import { LogicalOperator } 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 'path';
  6. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  7. import { initialData } from '../../../e2e-common/e2e-initial-data';
  8. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  9. import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
  10. import { LanguageCode, SortOrder } from './graphql/generated-e2e-admin-types';
  11. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  12. import { fixPostgresTimezone } from './utils/fix-pg-timezone';
  13. import { sortById } from './utils/test-order-utils';
  14. fixPostgresTimezone();
  15. describe('ListQueryBuilder', () => {
  16. const { server, adminClient, shopClient } = createTestEnvironment(
  17. mergeConfig(testConfig(), {
  18. apiOptions: {
  19. shopListQueryLimit: 10,
  20. adminListQueryLimit: 30,
  21. },
  22. plugins: [ListQueryPlugin],
  23. }),
  24. );
  25. beforeAll(async () => {
  26. await server.init({
  27. initialData,
  28. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  29. customerCount: 3,
  30. });
  31. await adminClient.asSuperAdmin();
  32. }, TEST_SETUP_TIMEOUT_MS);
  33. afterAll(async () => {
  34. await server.destroy();
  35. });
  36. function getItemLabels(items: any[]): string[] {
  37. return items.map((x: any) => x.label).sort();
  38. }
  39. describe('pagination', () => {
  40. it('all en', async () => {
  41. const { testEntities } = await adminClient.query(
  42. GET_LIST,
  43. {
  44. options: {},
  45. },
  46. { languageCode: LanguageCode.en },
  47. );
  48. expect(testEntities.totalItems).toBe(6);
  49. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  50. expect(testEntities.items.map((i: any) => i.name)).toEqual(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. it('all de', async () => {
  60. const { testEntities } = await adminClient.query(
  61. GET_LIST,
  62. {
  63. options: {},
  64. },
  65. { languageCode: LanguageCode.de },
  66. );
  67. expect(testEntities.totalItems).toBe(6);
  68. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  69. expect(testEntities.items.map((i: any) => i.name)).toEqual(expect.arrayContaining([
  70. 'apfel',
  71. 'fahrrad',
  72. 'kuchen',
  73. 'hund',
  74. 'egg', // falls back to en translation when de doesn't exist
  75. 'baum',
  76. ]));
  77. });
  78. it('take', async () => {
  79. const { testEntities } = await adminClient.query(GET_LIST, {
  80. options: {
  81. take: 2,
  82. },
  83. });
  84. expect(testEntities.totalItems).toBe(6);
  85. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  86. });
  87. it('skip', async () => {
  88. const { testEntities } = await adminClient.query(GET_LIST, {
  89. options: {
  90. skip: 2,
  91. },
  92. });
  93. expect(testEntities.totalItems).toBe(6);
  94. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  95. });
  96. it('skip negative is ignored', async () => {
  97. const { testEntities } = await adminClient.query(GET_LIST, {
  98. options: {
  99. skip: -1,
  100. },
  101. });
  102. expect(testEntities.totalItems).toBe(6);
  103. expect(testEntities.items.length).toBe(6);
  104. });
  105. it('take zero is ignored', async () => {
  106. const { testEntities } = await adminClient.query(GET_LIST, {
  107. options: {
  108. take: 0,
  109. },
  110. });
  111. expect(testEntities.totalItems).toBe(6);
  112. expect(testEntities.items.length).toBe(6);
  113. });
  114. it('take negative is ignored', async () => {
  115. const { testEntities } = await adminClient.query(GET_LIST, {
  116. options: {
  117. take: -1,
  118. },
  119. });
  120. expect(testEntities.totalItems).toBe(6);
  121. expect(testEntities.items.length).toBe(6);
  122. });
  123. it(
  124. 'take beyond adminListQueryLimit',
  125. assertThrowsWithMessage(async () => {
  126. await adminClient.query(GET_LIST, {
  127. options: {
  128. take: 50,
  129. },
  130. });
  131. }, 'Cannot take more than 30 results from a list query'),
  132. );
  133. it(
  134. 'take beyond shopListQueryLimit',
  135. assertThrowsWithMessage(async () => {
  136. await shopClient.query(GET_LIST, {
  137. options: {
  138. take: 50,
  139. },
  140. });
  141. }, 'Cannot take more than 10 results from a list query'),
  142. );
  143. });
  144. describe('string filtering', () => {
  145. it('eq', async () => {
  146. const { testEntities } = await adminClient.query(GET_LIST, {
  147. options: {
  148. filter: {
  149. label: {
  150. eq: 'B',
  151. },
  152. },
  153. },
  154. });
  155. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  156. });
  157. it('notEq', async () => {
  158. const { testEntities } = await adminClient.query(GET_LIST, {
  159. options: {
  160. filter: {
  161. label: {
  162. notEq: 'B',
  163. },
  164. },
  165. },
  166. });
  167. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E', 'F']);
  168. });
  169. it('contains', async () => {
  170. const { testEntities } = await adminClient.query(GET_LIST, {
  171. options: {
  172. filter: {
  173. description: {
  174. contains: 'adip',
  175. },
  176. },
  177. },
  178. });
  179. expect(getItemLabels(testEntities.items)).toEqual(['C']);
  180. });
  181. it('notContains', async () => {
  182. const { testEntities } = await adminClient.query(GET_LIST, {
  183. options: {
  184. filter: {
  185. description: {
  186. notContains: 'te',
  187. },
  188. },
  189. },
  190. });
  191. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E', 'F']);
  192. });
  193. it('in', async () => {
  194. const { testEntities } = await adminClient.query(GET_LIST, {
  195. options: {
  196. filter: {
  197. label: {
  198. in: ['A', 'C'],
  199. },
  200. },
  201. },
  202. });
  203. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
  204. });
  205. it('notIn', async () => {
  206. const { testEntities } = await adminClient.query(GET_LIST, {
  207. options: {
  208. filter: {
  209. label: {
  210. notIn: ['A', 'C'],
  211. },
  212. },
  213. },
  214. });
  215. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E', 'F']);
  216. });
  217. it('isNull true', async () => {
  218. const { testEntities } = await adminClient.query(GET_LIST, {
  219. options: {
  220. filter: {
  221. nullableString: {
  222. isNull: true,
  223. },
  224. },
  225. },
  226. });
  227. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  228. });
  229. it('isNull false', async () => {
  230. const { testEntities } = await adminClient.query(GET_LIST, {
  231. options: {
  232. filter: {
  233. nullableString: {
  234. isNull: false,
  235. },
  236. },
  237. },
  238. });
  239. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  240. });
  241. describe('regex', () => {
  242. it('simple substring', async () => {
  243. const { testEntities } = await adminClient.query(GET_LIST, {
  244. options: {
  245. filter: {
  246. description: {
  247. regex: 'or',
  248. },
  249. },
  250. },
  251. });
  252. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  253. });
  254. it('start of string', async () => {
  255. const { testEntities } = await adminClient.query(GET_LIST, {
  256. options: {
  257. filter: {
  258. description: {
  259. regex: '^in',
  260. },
  261. },
  262. },
  263. });
  264. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  265. });
  266. it('end of string', async () => {
  267. const { testEntities } = await adminClient.query(GET_LIST, {
  268. options: {
  269. filter: {
  270. description: {
  271. regex: 'or$',
  272. },
  273. },
  274. },
  275. });
  276. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  277. });
  278. it('alternation', async () => {
  279. const { testEntities } = await adminClient.query(GET_LIST, {
  280. options: {
  281. filter: {
  282. description: {
  283. regex: 'dolor|tempor',
  284. },
  285. },
  286. },
  287. });
  288. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  289. });
  290. it('complex', async () => {
  291. const { testEntities } = await adminClient.query(GET_LIST, {
  292. options: {
  293. filter: {
  294. description: {
  295. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  296. },
  297. },
  298. },
  299. });
  300. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  301. });
  302. });
  303. });
  304. describe('ID filtering', () => {
  305. it('eq', async () => {
  306. const { testEntities } = await adminClient.query(GET_LIST, {
  307. options: {
  308. filter: {
  309. ownerId: {
  310. eq: 'T_13',
  311. },
  312. },
  313. },
  314. });
  315. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  316. });
  317. it('notEq', async () => {
  318. const { testEntities } = await adminClient.query(GET_LIST, {
  319. options: {
  320. filter: {
  321. ownerId: {
  322. notEq: 'T_13',
  323. },
  324. },
  325. },
  326. });
  327. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'E', 'F']);
  328. });
  329. it('in', async () => {
  330. const { testEntities } = await adminClient.query(GET_LIST, {
  331. options: {
  332. filter: {
  333. ownerId: {
  334. in: ['T_10', 'T_15'],
  335. },
  336. },
  337. },
  338. });
  339. expect(getItemLabels(testEntities.items)).toEqual(['A', 'F']);
  340. });
  341. it('in with empty set', async () => {
  342. const { testEntities } = await adminClient.query(GET_LIST, {
  343. options: {
  344. filter: {
  345. ownerId: {
  346. in: [],
  347. },
  348. },
  349. },
  350. });
  351. expect(getItemLabels(testEntities.items)).toEqual([]);
  352. });
  353. it('notIn', async () => {
  354. const { testEntities } = await adminClient.query(GET_LIST, {
  355. options: {
  356. filter: {
  357. ownerId: {
  358. notIn: ['T_10', 'T_15'],
  359. },
  360. },
  361. },
  362. });
  363. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
  364. });
  365. it('notIn with empty set', async () => {
  366. const { testEntities } = await adminClient.query(GET_LIST, {
  367. options: {
  368. filter: {
  369. ownerId: {
  370. notIn: [],
  371. },
  372. },
  373. },
  374. });
  375. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  376. });
  377. it('isNull true', async () => {
  378. const { testEntities } = await adminClient.query(GET_LIST, {
  379. options: {
  380. filter: {
  381. nullableId: {
  382. isNull: true,
  383. },
  384. },
  385. },
  386. });
  387. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  388. });
  389. it('isNull false', async () => {
  390. const { testEntities } = await adminClient.query(GET_LIST, {
  391. options: {
  392. filter: {
  393. nullableId: {
  394. isNull: false,
  395. },
  396. },
  397. },
  398. });
  399. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  400. });
  401. describe('regex', () => {
  402. it('simple substring', async () => {
  403. const { testEntities } = await adminClient.query(GET_LIST, {
  404. options: {
  405. filter: {
  406. description: {
  407. regex: 'or',
  408. },
  409. },
  410. },
  411. });
  412. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  413. });
  414. it('start of string', async () => {
  415. const { testEntities } = await adminClient.query(GET_LIST, {
  416. options: {
  417. filter: {
  418. description: {
  419. regex: '^in',
  420. },
  421. },
  422. },
  423. });
  424. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  425. });
  426. it('end of string', async () => {
  427. const { testEntities } = await adminClient.query(GET_LIST, {
  428. options: {
  429. filter: {
  430. description: {
  431. regex: 'or$',
  432. },
  433. },
  434. },
  435. });
  436. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  437. });
  438. it('alternation', async () => {
  439. const { testEntities } = await adminClient.query(GET_LIST, {
  440. options: {
  441. filter: {
  442. description: {
  443. regex: 'dolor|tempor',
  444. },
  445. },
  446. },
  447. });
  448. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  449. });
  450. it('complex', async () => {
  451. const { testEntities } = await adminClient.query(GET_LIST, {
  452. options: {
  453. filter: {
  454. description: {
  455. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  456. },
  457. },
  458. },
  459. });
  460. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  461. });
  462. });
  463. });
  464. describe('boolean filtering', () => {
  465. it('eq', async () => {
  466. const { testEntities } = await adminClient.query(GET_LIST, {
  467. options: {
  468. filter: {
  469. active: {
  470. eq: false,
  471. },
  472. },
  473. },
  474. });
  475. expect(getItemLabels(testEntities.items)).toEqual(['C', 'E', 'F']);
  476. });
  477. it('isNull true', async () => {
  478. const { testEntities } = await adminClient.query(GET_LIST, {
  479. options: {
  480. filter: {
  481. nullableBoolean: {
  482. isNull: true,
  483. },
  484. },
  485. },
  486. });
  487. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  488. });
  489. it('isNull false', async () => {
  490. const { testEntities } = await adminClient.query(GET_LIST, {
  491. options: {
  492. filter: {
  493. nullableBoolean: {
  494. isNull: false,
  495. },
  496. },
  497. },
  498. });
  499. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  500. });
  501. });
  502. describe('number filtering', () => {
  503. it('eq', async () => {
  504. const { testEntities } = await adminClient.query(GET_LIST, {
  505. options: {
  506. filter: {
  507. order: {
  508. eq: 1,
  509. },
  510. },
  511. },
  512. });
  513. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  514. });
  515. it('lt', async () => {
  516. const { testEntities } = await adminClient.query(GET_LIST, {
  517. options: {
  518. filter: {
  519. order: {
  520. lt: 1,
  521. },
  522. },
  523. },
  524. });
  525. expect(getItemLabels(testEntities.items)).toEqual(['A']);
  526. });
  527. it('lte', async () => {
  528. const { testEntities } = await adminClient.query(GET_LIST, {
  529. options: {
  530. filter: {
  531. order: {
  532. lte: 1,
  533. },
  534. },
  535. },
  536. });
  537. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  538. });
  539. it('gt', async () => {
  540. const { testEntities } = await adminClient.query(GET_LIST, {
  541. options: {
  542. filter: {
  543. order: {
  544. gt: 1,
  545. },
  546. },
  547. },
  548. });
  549. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  550. });
  551. it('gte', async () => {
  552. const { testEntities } = await adminClient.query(GET_LIST, {
  553. options: {
  554. filter: {
  555. order: {
  556. gte: 1,
  557. },
  558. },
  559. },
  560. });
  561. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E', 'F']);
  562. });
  563. it('between', async () => {
  564. const { testEntities } = await adminClient.query(GET_LIST, {
  565. options: {
  566. filter: {
  567. order: {
  568. between: {
  569. start: 2,
  570. end: 4,
  571. },
  572. },
  573. },
  574. },
  575. });
  576. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  577. });
  578. it('isNull true', async () => {
  579. const { testEntities } = await adminClient.query(GET_LIST, {
  580. options: {
  581. filter: {
  582. nullableNumber: {
  583. isNull: true,
  584. },
  585. },
  586. },
  587. });
  588. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  589. });
  590. it('isNull false', async () => {
  591. const { testEntities } = await adminClient.query(GET_LIST, {
  592. options: {
  593. filter: {
  594. nullableNumber: {
  595. isNull: false,
  596. },
  597. },
  598. },
  599. });
  600. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  601. });
  602. });
  603. describe('date filtering', () => {
  604. it('before', async () => {
  605. const { testEntities } = await adminClient.query(GET_LIST, {
  606. options: {
  607. filter: {
  608. date: {
  609. before: '2020-01-20T10:00:00.000Z',
  610. },
  611. },
  612. },
  613. });
  614. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  615. });
  616. it('before on same date', async () => {
  617. const { testEntities } = await adminClient.query(GET_LIST, {
  618. options: {
  619. filter: {
  620. date: {
  621. before: '2020-01-15T17:00:00.000Z',
  622. },
  623. },
  624. },
  625. });
  626. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  627. });
  628. it('after', async () => {
  629. const { testEntities } = await adminClient.query(GET_LIST, {
  630. options: {
  631. filter: {
  632. date: {
  633. after: '2020-01-20T10:00:00.000Z',
  634. },
  635. },
  636. },
  637. });
  638. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  639. });
  640. it('after on same date', async () => {
  641. const { testEntities } = await adminClient.query(GET_LIST, {
  642. options: {
  643. filter: {
  644. date: {
  645. after: '2020-01-25T09:00:00.000Z',
  646. },
  647. },
  648. },
  649. });
  650. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  651. });
  652. it('between', async () => {
  653. const { testEntities } = await adminClient.query(GET_LIST, {
  654. options: {
  655. filter: {
  656. date: {
  657. between: {
  658. start: '2020-01-10T10:00:00.000Z',
  659. end: '2020-01-20T10:00:00.000Z',
  660. },
  661. },
  662. },
  663. },
  664. });
  665. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  666. });
  667. it('isNull true', async () => {
  668. const { testEntities } = await adminClient.query(GET_LIST, {
  669. options: {
  670. filter: {
  671. nullableDate: {
  672. isNull: true,
  673. },
  674. },
  675. },
  676. });
  677. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  678. });
  679. it('isNull false', async () => {
  680. const { testEntities } = await adminClient.query(GET_LIST, {
  681. options: {
  682. filter: {
  683. nullableDate: {
  684. isNull: false,
  685. },
  686. },
  687. },
  688. });
  689. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  690. });
  691. });
  692. describe('multiple filters with filterOperator', () => {
  693. it('default AND', async () => {
  694. const { testEntities } = await adminClient.query(GET_LIST, {
  695. options: {
  696. filter: {
  697. description: {
  698. contains: 'Lorem',
  699. },
  700. active: {
  701. eq: false,
  702. },
  703. },
  704. },
  705. });
  706. expect(getItemLabels(testEntities.items)).toEqual([]);
  707. });
  708. it('explicit 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. filterOperator: LogicalOperator.AND,
  720. },
  721. });
  722. expect(getItemLabels(testEntities.items)).toEqual([]);
  723. });
  724. it('explicit OR', async () => {
  725. const { testEntities } = await adminClient.query(GET_LIST, {
  726. options: {
  727. filter: {
  728. description: {
  729. contains: 'Lorem',
  730. },
  731. active: {
  732. eq: false,
  733. },
  734. },
  735. filterOperator: LogicalOperator.OR,
  736. },
  737. });
  738. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
  739. });
  740. it('explicit OR with 3 filters', async () => {
  741. const { testEntities } = await adminClient.query(GET_LIST, {
  742. options: {
  743. filter: {
  744. description: {
  745. contains: 'eiusmod',
  746. },
  747. active: {
  748. eq: false,
  749. },
  750. order: {
  751. lt: 3,
  752. },
  753. },
  754. filterOperator: LogicalOperator.OR,
  755. },
  756. });
  757. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  758. });
  759. it('explicit OR with empty filters object', async () => {
  760. const { testEntities } = await adminClient.query(GET_LIST, {
  761. options: {
  762. filter: {},
  763. filterOperator: LogicalOperator.OR,
  764. },
  765. });
  766. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  767. });
  768. });
  769. describe('complex boolean logic with _and & _or', () => {
  770. it('single _and', async () => {
  771. const { testEntities } = await adminClient.query(GET_LIST, {
  772. options: {
  773. filter: {
  774. _and: [
  775. {
  776. description: {
  777. contains: 'Lorem',
  778. },
  779. active: {
  780. eq: false,
  781. },
  782. },
  783. ],
  784. },
  785. },
  786. });
  787. expect(getItemLabels(testEntities.items)).toEqual([]);
  788. });
  789. it('single _or', async () => {
  790. const { testEntities } = await adminClient.query(GET_LIST, {
  791. options: {
  792. filter: {
  793. _or: [
  794. {
  795. description: {
  796. contains: 'Lorem',
  797. },
  798. active: {
  799. eq: false,
  800. },
  801. },
  802. ],
  803. },
  804. },
  805. });
  806. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
  807. });
  808. it('_or with nested _and', async () => {
  809. const { testEntities } = await adminClient.query(GET_LIST, {
  810. options: {
  811. filter: {
  812. _or: [
  813. {
  814. description: { contains: 'Lorem' },
  815. },
  816. {
  817. _and: [{ order: { gt: 3 } }, { order: { lt: 5 } }],
  818. },
  819. ],
  820. },
  821. },
  822. });
  823. expect(getItemLabels(testEntities.items)).toEqual(['A', 'E']);
  824. });
  825. it('_and with nested _or', async () => {
  826. const { testEntities } = await adminClient.query(GET_LIST, {
  827. options: {
  828. filter: {
  829. _and: [
  830. {
  831. description: { contains: 'e' },
  832. },
  833. {
  834. _or: [{ active: { eq: false } }, { ownerId: { eq: '10' } }],
  835. },
  836. ],
  837. },
  838. },
  839. });
  840. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'F']);
  841. });
  842. });
  843. describe('sorting', () => {
  844. it('sort by string', async () => {
  845. const { testEntities } = await adminClient.query(GET_LIST, {
  846. options: {
  847. sort: {
  848. label: SortOrder.DESC,
  849. },
  850. },
  851. });
  852. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  853. });
  854. it('sort by number', async () => {
  855. const { testEntities } = await adminClient.query(GET_LIST, {
  856. options: {
  857. sort: {
  858. order: SortOrder.DESC,
  859. },
  860. },
  861. });
  862. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  863. });
  864. it('sort by date', async () => {
  865. const { testEntities } = await adminClient.query(GET_LIST, {
  866. options: {
  867. sort: {
  868. date: SortOrder.DESC,
  869. },
  870. },
  871. });
  872. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  873. });
  874. it('sort by ID', async () => {
  875. const { testEntities } = await adminClient.query(GET_LIST, {
  876. options: {
  877. sort: {
  878. id: SortOrder.DESC,
  879. },
  880. },
  881. });
  882. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  883. });
  884. it('sort by translated field en', async () => {
  885. const { testEntities } = await adminClient.query(GET_LIST, {
  886. options: {
  887. sort: {
  888. name: SortOrder.ASC,
  889. },
  890. },
  891. });
  892. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  893. 'apple',
  894. 'baum', // falling back to de here
  895. 'bike',
  896. 'cake',
  897. 'dog',
  898. 'egg',
  899. ]);
  900. });
  901. it('sort by translated field de', async () => {
  902. const { testEntities } = await adminClient.query(
  903. GET_LIST,
  904. {
  905. options: {
  906. sort: {
  907. name: SortOrder.ASC,
  908. },
  909. },
  910. },
  911. { languageCode: LanguageCode.de },
  912. );
  913. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  914. 'apfel',
  915. 'baum',
  916. 'egg',
  917. 'fahrrad',
  918. 'hund',
  919. 'kuchen',
  920. ]);
  921. });
  922. it('sort by translated field en with take', async () => {
  923. const { testEntities } = await adminClient.query(GET_LIST, {
  924. options: {
  925. sort: {
  926. name: SortOrder.ASC,
  927. },
  928. take: 4,
  929. },
  930. });
  931. expect(testEntities.items.map((x: any) => x.name)).toEqual(['apple', 'baum', 'bike', 'cake']);
  932. });
  933. it('sort by translated field de with take', async () => {
  934. const { testEntities } = await adminClient.query(
  935. GET_LIST,
  936. {
  937. options: {
  938. sort: {
  939. name: SortOrder.ASC,
  940. },
  941. take: 4,
  942. },
  943. },
  944. { languageCode: LanguageCode.de },
  945. );
  946. expect(testEntities.items.map((x: any) => x.name)).toEqual(['apfel', 'baum', 'egg', 'fahrrad']);
  947. });
  948. });
  949. describe('calculated fields', () => {
  950. it('filter by simple calculated property', async () => {
  951. const { testEntities } = await adminClient.query(GET_LIST, {
  952. options: {
  953. filter: {
  954. descriptionLength: {
  955. lt: 12,
  956. },
  957. },
  958. },
  959. });
  960. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  961. });
  962. it('filter by calculated property with join', async () => {
  963. const { testEntities } = await adminClient.query(GET_LIST, {
  964. options: {
  965. filter: {
  966. price: {
  967. lt: 14,
  968. },
  969. },
  970. },
  971. });
  972. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  973. });
  974. it('sort by simple calculated property', async () => {
  975. const { testEntities } = await adminClient.query(GET_LIST, {
  976. options: {
  977. sort: {
  978. descriptionLength: SortOrder.ASC,
  979. },
  980. },
  981. });
  982. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
  983. });
  984. it('sort by calculated property with join', async () => {
  985. const { testEntities } = await adminClient.query(GET_LIST, {
  986. options: {
  987. sort: {
  988. price: SortOrder.ASC,
  989. },
  990. },
  991. });
  992. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
  993. });
  994. });
  995. describe('multiple clauses', () => {
  996. it('sort by translated field en & filter', async () => {
  997. const { testEntities } = await adminClient.query(GET_LIST, {
  998. options: {
  999. sort: {
  1000. name: SortOrder.ASC,
  1001. },
  1002. filter: {
  1003. order: {
  1004. gte: 1,
  1005. },
  1006. },
  1007. },
  1008. });
  1009. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  1010. 'baum',
  1011. 'bike',
  1012. 'cake',
  1013. 'dog',
  1014. 'egg',
  1015. ]);
  1016. });
  1017. it('sort by translated field de & filter', async () => {
  1018. const { testEntities } = await adminClient.query(
  1019. GET_LIST,
  1020. {
  1021. options: {
  1022. sort: {
  1023. name: SortOrder.ASC,
  1024. },
  1025. filter: {
  1026. order: {
  1027. gte: 1,
  1028. },
  1029. },
  1030. },
  1031. },
  1032. { languageCode: LanguageCode.de },
  1033. );
  1034. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  1035. 'baum',
  1036. 'egg',
  1037. 'fahrrad',
  1038. 'hund',
  1039. 'kuchen',
  1040. ]);
  1041. });
  1042. it('sort by translated field de & filter & pagination', async () => {
  1043. const { testEntities } = await adminClient.query(
  1044. GET_LIST,
  1045. {
  1046. options: {
  1047. sort: {
  1048. name: SortOrder.ASC,
  1049. },
  1050. filter: {
  1051. order: {
  1052. gte: 1,
  1053. },
  1054. },
  1055. take: 2,
  1056. skip: 1,
  1057. },
  1058. },
  1059. { languageCode: LanguageCode.de },
  1060. );
  1061. expect(testEntities.items.map((x: any) => x.name)).toEqual(['egg', 'fahrrad']);
  1062. });
  1063. });
  1064. // https://github.com/vendure-ecommerce/vendure/issues/1586
  1065. it('using the getMany() of the resulting QueryBuilder', async () => {
  1066. const { testEntitiesGetMany } = await adminClient.query(GET_ARRAY_LIST, {});
  1067. const actualPrices = testEntitiesGetMany.sort(sortById).map((x: any) => x.price).sort((a: number, b: number) => a - b);
  1068. const expectedPrices = [11, 9, 22, 14, 13, 33].sort((a, b) => a - b);
  1069. expect(actualPrices).toEqual(expectedPrices);
  1070. });
  1071. // https://github.com/vendure-ecommerce/vendure/issues/1611
  1072. describe('translations handling', () => {
  1073. const allTranslations = [
  1074. [
  1075. { languageCode: LanguageCode.en, name: 'apple' },
  1076. { languageCode: LanguageCode.de, name: 'apfel' },
  1077. ],
  1078. [
  1079. { languageCode: LanguageCode.en, name: 'bike' },
  1080. { languageCode: LanguageCode.de, name: 'fahrrad' },
  1081. ],
  1082. ];
  1083. function getTestEntityTranslations(testEntities: { items: any[] }) {
  1084. // Explicitly sort the order of the translations as it was being non-deterministic on
  1085. // the mysql CI tests.
  1086. return testEntities.items.map((e: any) =>
  1087. e.translations.sort((a: any, b: any) => (a.languageCode < b.languageCode ? 1 : -1)),
  1088. );
  1089. }
  1090. it('returns all translations with default languageCode', async () => {
  1091. const { testEntities } = await shopClient.query(GET_LIST_WITH_TRANSLATIONS, {
  1092. options: {
  1093. take: 2,
  1094. sort: {
  1095. order: SortOrder.ASC,
  1096. },
  1097. },
  1098. });
  1099. const testEntityTranslations = getTestEntityTranslations(testEntities);
  1100. expect(testEntities.items.map((e: any) => e.name)).toEqual(['apple', 'bike']);
  1101. expect(testEntityTranslations).toEqual(allTranslations);
  1102. });
  1103. it('returns all translations with non-default languageCode', async () => {
  1104. const { testEntities } = await shopClient.query(
  1105. GET_LIST_WITH_TRANSLATIONS,
  1106. {
  1107. options: {
  1108. take: 2,
  1109. sort: {
  1110. order: SortOrder.ASC,
  1111. },
  1112. },
  1113. },
  1114. { languageCode: LanguageCode.de },
  1115. );
  1116. const testEntityTranslations = getTestEntityTranslations(testEntities);
  1117. expect(testEntities.items.map((e: any) => e.name)).toEqual(['apfel', 'fahrrad']);
  1118. expect(testEntityTranslations).toEqual(allTranslations);
  1119. });
  1120. });
  1121. describe('customPropertyMap', () => {
  1122. it('filter by custom string field', async () => {
  1123. const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
  1124. options: {
  1125. sort: {
  1126. label: SortOrder.ASC,
  1127. },
  1128. filter: {
  1129. customerLastName: { contains: 'zieme' },
  1130. },
  1131. },
  1132. });
  1133. expect(testEntities.items).toEqual([
  1134. {
  1135. id: 'T_1',
  1136. label: 'A',
  1137. name: 'apple',
  1138. parent: {
  1139. id: 'T_2',
  1140. label: 'B',
  1141. name: 'bike',
  1142. },
  1143. orderRelation: {
  1144. customer: {
  1145. firstName: 'Hayden',
  1146. lastName: 'Zieme',
  1147. },
  1148. id: 'T_1',
  1149. },
  1150. },
  1151. {
  1152. id: 'T_4',
  1153. label: 'D',
  1154. name: 'dog',
  1155. parent: {
  1156. id: 'T_2',
  1157. label: 'B',
  1158. name: 'bike',
  1159. },
  1160. orderRelation: {
  1161. customer: {
  1162. firstName: 'Hayden',
  1163. lastName: 'Zieme',
  1164. },
  1165. id: 'T_4',
  1166. },
  1167. },
  1168. ]);
  1169. });
  1170. it('sort by custom string field', async () => {
  1171. const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
  1172. options: {
  1173. sort: {
  1174. customerLastName: SortOrder.ASC,
  1175. },
  1176. },
  1177. });
  1178. expect(testEntities.items.map((i: any) => i.orderRelation.customer)).toEqual([
  1179. { firstName: 'Trevor', lastName: 'Donnelly' },
  1180. { firstName: 'Trevor', lastName: 'Donnelly' },
  1181. { firstName: 'Marques', lastName: 'Sawayn' },
  1182. { firstName: 'Marques', lastName: 'Sawayn' },
  1183. { firstName: 'Hayden', lastName: 'Zieme' },
  1184. { firstName: 'Hayden', lastName: 'Zieme' },
  1185. ]);
  1186. });
  1187. });
  1188. describe('relations in customFields', () => {
  1189. it('should resolve relations in customFields successfully', async () => {
  1190. const { testEntities } = await shopClient.query(GET_LIST_WITH_CUSTOM_FIELD_RELATION, {
  1191. options: {
  1192. filter: {
  1193. label: { eq: 'A' },
  1194. },
  1195. },
  1196. });
  1197. expect(testEntities.items).toEqual([
  1198. {
  1199. id: 'T_1',
  1200. label: 'A',
  1201. customFields: {
  1202. relation: [{ id: 'T_1', data: 'A' }],
  1203. },
  1204. },
  1205. ]);
  1206. });
  1207. it('should resolve multiple relations in customFields successfully', async () => {
  1208. const { testEntities } = await shopClient.query(GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION, {
  1209. options: {
  1210. filter: {
  1211. label: { eq: 'A' },
  1212. },
  1213. },
  1214. });
  1215. expect(testEntities.items).toEqual([
  1216. {
  1217. id: 'T_1',
  1218. label: 'A',
  1219. customFields: {
  1220. relation: [{ id: 'T_1', data: 'A' }],
  1221. otherRelation: [{ id: 'T_1', data: 'A' }],
  1222. },
  1223. },
  1224. ]);
  1225. });
  1226. });
  1227. });
  1228. const GET_LIST = gql`
  1229. query GetTestEntities($options: TestEntityListOptions) {
  1230. testEntities(options: $options) {
  1231. totalItems
  1232. items {
  1233. id
  1234. label
  1235. name
  1236. date
  1237. }
  1238. }
  1239. }
  1240. `;
  1241. const GET_LIST_WITH_TRANSLATIONS = gql`
  1242. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1243. testEntities(options: $options) {
  1244. totalItems
  1245. items {
  1246. id
  1247. label
  1248. name
  1249. date
  1250. translations {
  1251. languageCode
  1252. name
  1253. }
  1254. }
  1255. }
  1256. }
  1257. `;
  1258. const GET_LIST_WITH_ORDERS = gql`
  1259. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1260. testEntities(options: $options) {
  1261. totalItems
  1262. items {
  1263. id
  1264. label
  1265. name
  1266. parent {
  1267. id
  1268. label
  1269. name
  1270. }
  1271. orderRelation {
  1272. id
  1273. customer {
  1274. firstName
  1275. lastName
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. `;
  1282. const GET_ARRAY_LIST = gql`
  1283. query GetTestEntitiesArray($options: TestEntityListOptions) {
  1284. testEntitiesGetMany(options: $options) {
  1285. id
  1286. label
  1287. name
  1288. date
  1289. price
  1290. }
  1291. }
  1292. `;
  1293. const GET_LIST_WITH_CUSTOM_FIELD_RELATION = gql`
  1294. query GetTestWithCustomFieldRelation($options: TestEntityListOptions) {
  1295. testEntities(options: $options) {
  1296. items {
  1297. id
  1298. label
  1299. customFields {
  1300. relation {
  1301. id
  1302. data
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. `;
  1309. const GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION = gql`
  1310. query GetTestWithMultipleCustomFieldRelation($options: TestEntityListOptions) {
  1311. testEntities(options: $options) {
  1312. items {
  1313. id
  1314. label
  1315. customFields {
  1316. relation {
  1317. id
  1318. data
  1319. }
  1320. otherRelation {
  1321. id
  1322. data
  1323. }
  1324. }
  1325. }
  1326. }
  1327. }
  1328. `;