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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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('string filtering', () => {
  32. it('eq', async () => {
  33. const { testEntities } = await adminClient.query(GET_LIST, {
  34. options: {
  35. filter: {
  36. label: {
  37. eq: 'B',
  38. },
  39. },
  40. },
  41. });
  42. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  43. });
  44. it('notEq', async () => {
  45. const { testEntities } = await adminClient.query(GET_LIST, {
  46. options: {
  47. filter: {
  48. label: {
  49. notEq: 'B',
  50. },
  51. },
  52. },
  53. });
  54. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E']);
  55. });
  56. it('contains', async () => {
  57. const { testEntities } = await adminClient.query(GET_LIST, {
  58. options: {
  59. filter: {
  60. description: {
  61. contains: 'adip',
  62. },
  63. },
  64. },
  65. });
  66. expect(getItemLabels(testEntities.items)).toEqual(['C']);
  67. });
  68. it('notContains', async () => {
  69. const { testEntities } = await adminClient.query(GET_LIST, {
  70. options: {
  71. filter: {
  72. description: {
  73. notContains: 'te',
  74. },
  75. },
  76. },
  77. });
  78. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  79. });
  80. it('in', async () => {
  81. const { testEntities } = await adminClient.query(GET_LIST, {
  82. options: {
  83. filter: {
  84. label: {
  85. in: ['A', 'C'],
  86. },
  87. },
  88. },
  89. });
  90. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
  91. });
  92. it('notIn', async () => {
  93. const { testEntities } = await adminClient.query(GET_LIST, {
  94. options: {
  95. filter: {
  96. label: {
  97. notIn: ['A', 'C'],
  98. },
  99. },
  100. },
  101. });
  102. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  103. });
  104. describe('regex', () => {
  105. it('simple substring', async () => {
  106. const { testEntities } = await adminClient.query(GET_LIST, {
  107. options: {
  108. filter: {
  109. description: {
  110. regex: 'or',
  111. },
  112. },
  113. },
  114. });
  115. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  116. });
  117. it('start of string', async () => {
  118. const { testEntities } = await adminClient.query(GET_LIST, {
  119. options: {
  120. filter: {
  121. description: {
  122. regex: '^in',
  123. },
  124. },
  125. },
  126. });
  127. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  128. });
  129. it('end of string', async () => {
  130. const { testEntities } = await adminClient.query(GET_LIST, {
  131. options: {
  132. filter: {
  133. description: {
  134. regex: 'or$',
  135. },
  136. },
  137. },
  138. });
  139. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  140. });
  141. it('alternation', async () => {
  142. const { testEntities } = await adminClient.query(GET_LIST, {
  143. options: {
  144. filter: {
  145. description: {
  146. regex: 'dolor|tempor',
  147. },
  148. },
  149. },
  150. });
  151. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  152. });
  153. it('complex', async () => {
  154. const { testEntities } = await adminClient.query(GET_LIST, {
  155. options: {
  156. filter: {
  157. description: {
  158. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  159. },
  160. },
  161. },
  162. });
  163. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  164. });
  165. });
  166. });
  167. describe('boolean filtering', () => {
  168. it('eq', async () => {
  169. const { testEntities } = await adminClient.query(GET_LIST, {
  170. options: {
  171. filter: {
  172. active: {
  173. eq: false,
  174. },
  175. },
  176. },
  177. });
  178. expect(getItemLabels(testEntities.items)).toEqual(['C', 'E']);
  179. });
  180. });
  181. describe('number filtering', () => {
  182. it('eq', async () => {
  183. const { testEntities } = await adminClient.query(GET_LIST, {
  184. options: {
  185. filter: {
  186. order: {
  187. eq: 1,
  188. },
  189. },
  190. },
  191. });
  192. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  193. });
  194. it('lt', async () => {
  195. const { testEntities } = await adminClient.query(GET_LIST, {
  196. options: {
  197. filter: {
  198. order: {
  199. lt: 1,
  200. },
  201. },
  202. },
  203. });
  204. expect(getItemLabels(testEntities.items)).toEqual(['A']);
  205. });
  206. it('lte', async () => {
  207. const { testEntities } = await adminClient.query(GET_LIST, {
  208. options: {
  209. filter: {
  210. order: {
  211. lte: 1,
  212. },
  213. },
  214. },
  215. });
  216. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  217. });
  218. it('gt', async () => {
  219. const { testEntities } = await adminClient.query(GET_LIST, {
  220. options: {
  221. filter: {
  222. order: {
  223. gt: 1,
  224. },
  225. },
  226. },
  227. });
  228. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  229. });
  230. it('gte', async () => {
  231. const { testEntities } = await adminClient.query(GET_LIST, {
  232. options: {
  233. filter: {
  234. order: {
  235. gte: 1,
  236. },
  237. },
  238. },
  239. });
  240. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
  241. });
  242. it('between', async () => {
  243. const { testEntities } = await adminClient.query(GET_LIST, {
  244. options: {
  245. filter: {
  246. order: {
  247. between: {
  248. start: 2,
  249. end: 4,
  250. },
  251. },
  252. },
  253. },
  254. });
  255. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  256. });
  257. });
  258. describe('date filtering', () => {
  259. it('before', async () => {
  260. const { testEntities } = await adminClient.query(GET_LIST, {
  261. options: {
  262. filter: {
  263. date: {
  264. before: '2020-01-20T10:00:00.000Z',
  265. },
  266. },
  267. },
  268. });
  269. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  270. });
  271. it('before on same date', async () => {
  272. const { testEntities } = await adminClient.query(GET_LIST, {
  273. options: {
  274. filter: {
  275. date: {
  276. before: '2020-01-15T17:00:00.000Z',
  277. },
  278. },
  279. },
  280. });
  281. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  282. });
  283. it('after', async () => {
  284. const { testEntities } = await adminClient.query(GET_LIST, {
  285. options: {
  286. filter: {
  287. date: {
  288. after: '2020-01-20T10:00:00.000Z',
  289. },
  290. },
  291. },
  292. });
  293. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  294. });
  295. it('after on same date', async () => {
  296. const { testEntities } = await adminClient.query(GET_LIST, {
  297. options: {
  298. filter: {
  299. date: {
  300. after: '2020-01-25T09:00:00.000Z',
  301. },
  302. },
  303. },
  304. });
  305. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  306. });
  307. it('between', async () => {
  308. const { testEntities } = await adminClient.query(GET_LIST, {
  309. options: {
  310. filter: {
  311. date: {
  312. between: {
  313. start: '2020-01-10T10:00:00.000Z',
  314. end: '2020-01-20T10:00:00.000Z',
  315. },
  316. },
  317. },
  318. },
  319. });
  320. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  321. });
  322. });
  323. describe('sorting', () => {
  324. it('sort by string', async () => {
  325. const { testEntities } = await adminClient.query(GET_LIST, {
  326. options: {
  327. sort: {
  328. label: SortOrder.DESC,
  329. },
  330. },
  331. });
  332. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  333. });
  334. it('sort by number', async () => {
  335. const { testEntities } = await adminClient.query(GET_LIST, {
  336. options: {
  337. sort: {
  338. order: SortOrder.DESC,
  339. },
  340. },
  341. });
  342. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  343. });
  344. it('sort by date', async () => {
  345. const { testEntities } = await adminClient.query(GET_LIST, {
  346. options: {
  347. sort: {
  348. date: SortOrder.DESC,
  349. },
  350. },
  351. });
  352. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  353. });
  354. it('sort by ID', async () => {
  355. const { testEntities } = await adminClient.query(GET_LIST, {
  356. options: {
  357. sort: {
  358. id: SortOrder.DESC,
  359. },
  360. },
  361. });
  362. expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
  363. });
  364. });
  365. describe('calculated fields', () => {
  366. it('filter by simple calculated property', async () => {
  367. const { testEntities } = await adminClient.query(GET_LIST, {
  368. options: {
  369. filter: {
  370. descriptionLength: {
  371. lt: 12,
  372. },
  373. },
  374. },
  375. });
  376. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  377. });
  378. it('filter by calculated property with join', async () => {
  379. const { testEntities } = await adminClient.query(GET_LIST, {
  380. options: {
  381. filter: {
  382. price: {
  383. lt: 14,
  384. },
  385. },
  386. },
  387. });
  388. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  389. });
  390. it('sort by simple calculated property', async () => {
  391. const { testEntities } = await adminClient.query(GET_LIST, {
  392. options: {
  393. sort: {
  394. descriptionLength: SortOrder.ASC,
  395. },
  396. },
  397. });
  398. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C']);
  399. });
  400. it('sort by calculated property with join', async () => {
  401. const { testEntities } = await adminClient.query(GET_LIST, {
  402. options: {
  403. sort: {
  404. price: SortOrder.ASC,
  405. },
  406. },
  407. });
  408. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C']);
  409. });
  410. });
  411. });
  412. const GET_LIST = gql`
  413. query GetTestEntities($options: TestEntityListOptions) {
  414. testEntities(options: $options) {
  415. totalItems
  416. items {
  417. id
  418. label
  419. date
  420. }
  421. }
  422. }
  423. `;