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

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