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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 { fixPostgresTimezone } from './utils/fix-pg-timezone';
  9. fixPostgresTimezone();
  10. describe('ListQueryBuilder', () => {
  11. const { server, adminClient } = createTestEnvironment(
  12. mergeConfig(testConfig, {
  13. plugins: [ListQueryPlugin],
  14. }),
  15. );
  16. beforeAll(async () => {
  17. await server.init({
  18. initialData,
  19. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  20. customerCount: 1,
  21. });
  22. await adminClient.asSuperAdmin();
  23. }, TEST_SETUP_TIMEOUT_MS);
  24. afterAll(async () => {
  25. await server.destroy();
  26. });
  27. function getItemLabels(items: any[]): string[] {
  28. return items.map((x: any) => x.label).sort();
  29. }
  30. describe('string filtering', () => {
  31. it('eq', async () => {
  32. const { testEntities } = await adminClient.query(GET_LIST, {
  33. options: {
  34. filter: {
  35. label: {
  36. eq: 'B',
  37. },
  38. },
  39. },
  40. });
  41. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  42. });
  43. it('notEq', async () => {
  44. const { testEntities } = await adminClient.query(GET_LIST, {
  45. options: {
  46. filter: {
  47. label: {
  48. notEq: 'B',
  49. },
  50. },
  51. },
  52. });
  53. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E']);
  54. });
  55. it('contains', async () => {
  56. const { testEntities } = await adminClient.query(GET_LIST, {
  57. options: {
  58. filter: {
  59. description: {
  60. contains: 'adip',
  61. },
  62. },
  63. },
  64. });
  65. expect(getItemLabels(testEntities.items)).toEqual(['C']);
  66. });
  67. it('notContains', async () => {
  68. const { testEntities } = await adminClient.query(GET_LIST, {
  69. options: {
  70. filter: {
  71. description: {
  72. notContains: 'te',
  73. },
  74. },
  75. },
  76. });
  77. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  78. });
  79. it('in', async () => {
  80. const { testEntities } = await adminClient.query(GET_LIST, {
  81. options: {
  82. filter: {
  83. label: {
  84. in: ['A', 'C'],
  85. },
  86. },
  87. },
  88. });
  89. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
  90. });
  91. it('notIn', async () => {
  92. const { testEntities } = await adminClient.query(GET_LIST, {
  93. options: {
  94. filter: {
  95. label: {
  96. notIn: ['A', 'C'],
  97. },
  98. },
  99. },
  100. });
  101. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  102. });
  103. describe('regex', () => {
  104. it('simple substring', async () => {
  105. const { testEntities } = await adminClient.query(GET_LIST, {
  106. options: {
  107. filter: {
  108. description: {
  109. regex: 'or',
  110. },
  111. },
  112. },
  113. });
  114. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  115. });
  116. it('start of string', async () => {
  117. const { testEntities } = await adminClient.query(GET_LIST, {
  118. options: {
  119. filter: {
  120. description: {
  121. regex: '^in',
  122. },
  123. },
  124. },
  125. });
  126. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  127. });
  128. it('end of string', async () => {
  129. const { testEntities } = await adminClient.query(GET_LIST, {
  130. options: {
  131. filter: {
  132. description: {
  133. regex: 'or$',
  134. },
  135. },
  136. },
  137. });
  138. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  139. });
  140. it('alternation', async () => {
  141. const { testEntities } = await adminClient.query(GET_LIST, {
  142. options: {
  143. filter: {
  144. description: {
  145. regex: 'dolor|tempor',
  146. },
  147. },
  148. },
  149. });
  150. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  151. });
  152. it('complex', async () => {
  153. const { testEntities } = await adminClient.query(GET_LIST, {
  154. options: {
  155. filter: {
  156. description: {
  157. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  158. },
  159. },
  160. },
  161. });
  162. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  163. });
  164. });
  165. });
  166. describe('boolean filtering', () => {
  167. it('eq', async () => {
  168. const { testEntities } = await adminClient.query(GET_LIST, {
  169. options: {
  170. filter: {
  171. active: {
  172. eq: false,
  173. },
  174. },
  175. },
  176. });
  177. expect(getItemLabels(testEntities.items)).toEqual(['C', 'E']);
  178. });
  179. });
  180. describe('number filtering', () => {
  181. it('eq', async () => {
  182. const { testEntities } = await adminClient.query(GET_LIST, {
  183. options: {
  184. filter: {
  185. order: {
  186. eq: 1,
  187. },
  188. },
  189. },
  190. });
  191. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  192. });
  193. it('lt', async () => {
  194. const { testEntities } = await adminClient.query(GET_LIST, {
  195. options: {
  196. filter: {
  197. order: {
  198. lt: 1,
  199. },
  200. },
  201. },
  202. });
  203. expect(getItemLabels(testEntities.items)).toEqual(['A']);
  204. });
  205. it('lte', async () => {
  206. const { testEntities } = await adminClient.query(GET_LIST, {
  207. options: {
  208. filter: {
  209. order: {
  210. lte: 1,
  211. },
  212. },
  213. },
  214. });
  215. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  216. });
  217. it('gt', async () => {
  218. const { testEntities } = await adminClient.query(GET_LIST, {
  219. options: {
  220. filter: {
  221. order: {
  222. gt: 1,
  223. },
  224. },
  225. },
  226. });
  227. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  228. });
  229. it('gte', async () => {
  230. const { testEntities } = await adminClient.query(GET_LIST, {
  231. options: {
  232. filter: {
  233. order: {
  234. gte: 1,
  235. },
  236. },
  237. },
  238. });
  239. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
  240. });
  241. it('between', async () => {
  242. const { testEntities } = await adminClient.query(GET_LIST, {
  243. options: {
  244. filter: {
  245. order: {
  246. between: {
  247. start: 2,
  248. end: 4,
  249. },
  250. },
  251. },
  252. },
  253. });
  254. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  255. });
  256. });
  257. describe('date filtering', () => {
  258. it('before', async () => {
  259. const { testEntities } = await adminClient.query(GET_LIST, {
  260. options: {
  261. filter: {
  262. date: {
  263. before: '2020-01-20T10:00:00.000Z',
  264. },
  265. },
  266. },
  267. });
  268. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  269. });
  270. it('before on same date', async () => {
  271. const { testEntities } = await adminClient.query(GET_LIST, {
  272. options: {
  273. filter: {
  274. date: {
  275. before: '2020-01-15T17:00:00.000Z',
  276. },
  277. },
  278. },
  279. });
  280. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  281. });
  282. it('after', async () => {
  283. const { testEntities } = await adminClient.query(GET_LIST, {
  284. options: {
  285. filter: {
  286. date: {
  287. after: '2020-01-20T10:00:00.000Z',
  288. },
  289. },
  290. },
  291. });
  292. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  293. });
  294. it('after on same date', async () => {
  295. const { testEntities } = await adminClient.query(GET_LIST, {
  296. options: {
  297. filter: {
  298. date: {
  299. after: '2020-01-25T09:00:00.000Z',
  300. },
  301. },
  302. },
  303. });
  304. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  305. });
  306. it('between', async () => {
  307. const { testEntities } = await adminClient.query(GET_LIST, {
  308. options: {
  309. filter: {
  310. date: {
  311. between: {
  312. start: '2020-01-10T10:00:00.000Z',
  313. end: '2020-01-20T10:00:00.000Z',
  314. },
  315. },
  316. },
  317. },
  318. });
  319. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  320. });
  321. });
  322. });
  323. const GET_LIST = gql`
  324. query GetTestEntities($options: TestEntityListOptions) {
  325. testEntities(options: $options) {
  326. totalItems
  327. items {
  328. id
  329. label
  330. date
  331. }
  332. }
  333. }
  334. `;