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

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