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

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