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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import { mergeConfig } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  7. import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
  8. import { SortOrder } from './graphql/generated-e2e-admin-types';
  9. import { fixPostgresTimezone } from './utils/fix-pg-timezone';
  10. fixPostgresTimezone();
  11. describe('ListQueryBuilder', () => {
  12. const { server, adminClient } = createTestEnvironment(
  13. mergeConfig(testConfig, {
  14. plugins: [ListQueryPlugin],
  15. }),
  16. );
  17. beforeAll(async () => {
  18. await server.init({
  19. initialData,
  20. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  21. customerCount: 1,
  22. });
  23. await adminClient.asSuperAdmin();
  24. }, TEST_SETUP_TIMEOUT_MS);
  25. afterAll(async () => {
  26. await server.destroy();
  27. });
  28. function getItemLabels(items: any[]): string[] {
  29. return items.map((x: any) => x.label).sort();
  30. }
  31. describe('pagination', () => {
  32. it('take', async () => {
  33. const { testEntities } = await adminClient.query(GET_LIST, {
  34. options: {
  35. take: 2,
  36. },
  37. });
  38. expect(testEntities.totalItems).toBe(5);
  39. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  40. });
  41. it('skip', async () => {
  42. const { testEntities } = await adminClient.query(GET_LIST, {
  43. options: {
  44. skip: 2,
  45. },
  46. });
  47. expect(testEntities.totalItems).toBe(5);
  48. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  49. });
  50. it('skip negative is ignored', async () => {
  51. const { testEntities } = await adminClient.query(GET_LIST, {
  52. options: {
  53. skip: -1,
  54. },
  55. });
  56. expect(testEntities.totalItems).toBe(5);
  57. expect(testEntities.items.length).toBe(5);
  58. });
  59. it('take zero is ignored', async () => {
  60. const { testEntities } = await adminClient.query(GET_LIST, {
  61. options: {
  62. take: 0,
  63. },
  64. });
  65. expect(testEntities.totalItems).toBe(5);
  66. expect(testEntities.items.length).toBe(5);
  67. });
  68. it('take negative is ignored', async () => {
  69. const { testEntities } = await adminClient.query(GET_LIST, {
  70. options: {
  71. take: -1,
  72. },
  73. });
  74. expect(testEntities.totalItems).toBe(5);
  75. expect(testEntities.items.length).toBe(5);
  76. });
  77. });
  78. describe('string filtering', () => {
  79. it('eq', async () => {
  80. const { testEntities } = await adminClient.query(GET_LIST, {
  81. options: {
  82. filter: {
  83. label: {
  84. eq: 'B',
  85. },
  86. },
  87. },
  88. });
  89. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  90. });
  91. it('notEq', async () => {
  92. const { testEntities } = await adminClient.query(GET_LIST, {
  93. options: {
  94. filter: {
  95. label: {
  96. notEq: 'B',
  97. },
  98. },
  99. },
  100. });
  101. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E']);
  102. });
  103. it('contains', async () => {
  104. const { testEntities } = await adminClient.query(GET_LIST, {
  105. options: {
  106. filter: {
  107. description: {
  108. contains: 'adip',
  109. },
  110. },
  111. },
  112. });
  113. expect(getItemLabels(testEntities.items)).toEqual(['C']);
  114. });
  115. it('notContains', async () => {
  116. const { testEntities } = await adminClient.query(GET_LIST, {
  117. options: {
  118. filter: {
  119. description: {
  120. notContains: 'te',
  121. },
  122. },
  123. },
  124. });
  125. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  126. });
  127. it('in', async () => {
  128. const { testEntities } = await adminClient.query(GET_LIST, {
  129. options: {
  130. filter: {
  131. label: {
  132. in: ['A', 'C'],
  133. },
  134. },
  135. },
  136. });
  137. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
  138. });
  139. it('notIn', async () => {
  140. const { testEntities } = await adminClient.query(GET_LIST, {
  141. options: {
  142. filter: {
  143. label: {
  144. notIn: ['A', 'C'],
  145. },
  146. },
  147. },
  148. });
  149. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  150. });
  151. describe('regex', () => {
  152. it('simple substring', async () => {
  153. const { testEntities } = await adminClient.query(GET_LIST, {
  154. options: {
  155. filter: {
  156. description: {
  157. regex: 'or',
  158. },
  159. },
  160. },
  161. });
  162. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  163. });
  164. it('start of string', async () => {
  165. const { testEntities } = await adminClient.query(GET_LIST, {
  166. options: {
  167. filter: {
  168. description: {
  169. regex: '^in',
  170. },
  171. },
  172. },
  173. });
  174. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  175. });
  176. it('end of string', async () => {
  177. const { testEntities } = await adminClient.query(GET_LIST, {
  178. options: {
  179. filter: {
  180. description: {
  181. regex: 'or$',
  182. },
  183. },
  184. },
  185. });
  186. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  187. });
  188. it('alternation', async () => {
  189. const { testEntities } = await adminClient.query(GET_LIST, {
  190. options: {
  191. filter: {
  192. description: {
  193. regex: 'dolor|tempor',
  194. },
  195. },
  196. },
  197. });
  198. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  199. });
  200. it('complex', async () => {
  201. const { testEntities } = await adminClient.query(GET_LIST, {
  202. options: {
  203. filter: {
  204. description: {
  205. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  206. },
  207. },
  208. },
  209. });
  210. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  211. });
  212. });
  213. });
  214. describe('boolean filtering', () => {
  215. it('eq', async () => {
  216. const { testEntities } = await adminClient.query(GET_LIST, {
  217. options: {
  218. filter: {
  219. active: {
  220. eq: false,
  221. },
  222. },
  223. },
  224. });
  225. expect(getItemLabels(testEntities.items)).toEqual(['C', 'E']);
  226. });
  227. });
  228. describe('number filtering', () => {
  229. it('eq', async () => {
  230. const { testEntities } = await adminClient.query(GET_LIST, {
  231. options: {
  232. filter: {
  233. order: {
  234. eq: 1,
  235. },
  236. },
  237. },
  238. });
  239. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  240. });
  241. it('lt', async () => {
  242. const { testEntities } = await adminClient.query(GET_LIST, {
  243. options: {
  244. filter: {
  245. order: {
  246. lt: 1,
  247. },
  248. },
  249. },
  250. });
  251. expect(getItemLabels(testEntities.items)).toEqual(['A']);
  252. });
  253. it('lte', async () => {
  254. const { testEntities } = await adminClient.query(GET_LIST, {
  255. options: {
  256. filter: {
  257. order: {
  258. lte: 1,
  259. },
  260. },
  261. },
  262. });
  263. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  264. });
  265. it('gt', async () => {
  266. const { testEntities } = await adminClient.query(GET_LIST, {
  267. options: {
  268. filter: {
  269. order: {
  270. gt: 1,
  271. },
  272. },
  273. },
  274. });
  275. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  276. });
  277. it('gte', async () => {
  278. const { testEntities } = await adminClient.query(GET_LIST, {
  279. options: {
  280. filter: {
  281. order: {
  282. gte: 1,
  283. },
  284. },
  285. },
  286. });
  287. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
  288. });
  289. it('between', async () => {
  290. const { testEntities } = await adminClient.query(GET_LIST, {
  291. options: {
  292. filter: {
  293. order: {
  294. between: {
  295. start: 2,
  296. end: 4,
  297. },
  298. },
  299. },
  300. },
  301. });
  302. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  303. });
  304. });
  305. describe('date filtering', () => {
  306. it('before', async () => {
  307. const { testEntities } = await adminClient.query(GET_LIST, {
  308. options: {
  309. filter: {
  310. date: {
  311. before: '2020-01-20T10:00:00.000Z',
  312. },
  313. },
  314. },
  315. });
  316. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  317. });
  318. it('before on same date', async () => {
  319. const { testEntities } = await adminClient.query(GET_LIST, {
  320. options: {
  321. filter: {
  322. date: {
  323. before: '2020-01-15T17:00:00.000Z',
  324. },
  325. },
  326. },
  327. });
  328. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  329. });
  330. it('after', async () => {
  331. const { testEntities } = await adminClient.query(GET_LIST, {
  332. options: {
  333. filter: {
  334. date: {
  335. after: '2020-01-20T10:00:00.000Z',
  336. },
  337. },
  338. },
  339. });
  340. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  341. });
  342. it('after on same date', async () => {
  343. const { testEntities } = await adminClient.query(GET_LIST, {
  344. options: {
  345. filter: {
  346. date: {
  347. after: '2020-01-25T09:00:00.000Z',
  348. },
  349. },
  350. },
  351. });
  352. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  353. });
  354. it('between', async () => {
  355. const { testEntities } = await adminClient.query(GET_LIST, {
  356. options: {
  357. filter: {
  358. date: {
  359. between: {
  360. start: '2020-01-10T10:00:00.000Z',
  361. end: '2020-01-20T10:00:00.000Z',
  362. },
  363. },
  364. },
  365. },
  366. });
  367. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  368. });
  369. });
  370. describe('sorting', () => {
  371. it('sort by string', async () => {
  372. const { testEntities } = await adminClient.query(GET_LIST, {
  373. options: {
  374. sort: {
  375. label: SortOrder.DESC,
  376. },
  377. },
  378. });
  379. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  380. });
  381. it('sort by number', async () => {
  382. const { testEntities } = await adminClient.query(GET_LIST, {
  383. options: {
  384. sort: {
  385. order: SortOrder.DESC,
  386. },
  387. },
  388. });
  389. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  390. });
  391. it('sort by date', async () => {
  392. const { testEntities } = await adminClient.query(GET_LIST, {
  393. options: {
  394. sort: {
  395. date: SortOrder.DESC,
  396. },
  397. },
  398. });
  399. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  400. });
  401. it('sort by ID', async () => {
  402. const { testEntities } = await adminClient.query(GET_LIST, {
  403. options: {
  404. sort: {
  405. id: SortOrder.DESC,
  406. },
  407. },
  408. });
  409. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  410. });
  411. });
  412. describe('calculated fields', () => {
  413. it('filter by simple calculated property', async () => {
  414. const { testEntities } = await adminClient.query(GET_LIST, {
  415. options: {
  416. filter: {
  417. descriptionLength: {
  418. lt: 12,
  419. },
  420. },
  421. },
  422. });
  423. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  424. });
  425. it('filter by calculated property with join', async () => {
  426. const { testEntities } = await adminClient.query(GET_LIST, {
  427. options: {
  428. filter: {
  429. price: {
  430. lt: 14,
  431. },
  432. },
  433. },
  434. });
  435. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  436. });
  437. it('sort by simple calculated property', async () => {
  438. const { testEntities } = await adminClient.query(GET_LIST, {
  439. options: {
  440. sort: {
  441. descriptionLength: SortOrder.ASC,
  442. },
  443. },
  444. });
  445. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C']);
  446. });
  447. it('sort by calculated property with join', async () => {
  448. const { testEntities } = await adminClient.query(GET_LIST, {
  449. options: {
  450. sort: {
  451. price: SortOrder.ASC,
  452. },
  453. },
  454. });
  455. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C']);
  456. });
  457. });
  458. });
  459. const GET_LIST = gql`
  460. query GetTestEntities($options: TestEntityListOptions) {
  461. testEntities(options: $options) {
  462. totalItems
  463. items {
  464. id
  465. label
  466. date
  467. }
  468. }
  469. }
  470. `;