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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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('contains', async () => {
  44. const { testEntities } = await adminClient.query(GET_LIST, {
  45. options: {
  46. filter: {
  47. description: {
  48. contains: 'adip',
  49. },
  50. },
  51. },
  52. });
  53. expect(getItemLabels(testEntities.items)).toEqual(['C']);
  54. });
  55. it('in', async () => {
  56. const { testEntities } = await adminClient.query(GET_LIST, {
  57. options: {
  58. filter: {
  59. label: {
  60. in: ['A', 'C'],
  61. },
  62. },
  63. },
  64. });
  65. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
  66. });
  67. describe('regex', () => {
  68. it('simple substring', async () => {
  69. const { testEntities } = await adminClient.query(GET_LIST, {
  70. options: {
  71. filter: {
  72. description: {
  73. regex: 'or',
  74. },
  75. },
  76. },
  77. });
  78. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  79. });
  80. it('start of string', async () => {
  81. const { testEntities } = await adminClient.query(GET_LIST, {
  82. options: {
  83. filter: {
  84. description: {
  85. regex: '^in',
  86. },
  87. },
  88. },
  89. });
  90. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  91. });
  92. it('end of string', async () => {
  93. const { testEntities } = await adminClient.query(GET_LIST, {
  94. options: {
  95. filter: {
  96. description: {
  97. regex: 'or$',
  98. },
  99. },
  100. },
  101. });
  102. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  103. });
  104. it('alternation', async () => {
  105. const { testEntities } = await adminClient.query(GET_LIST, {
  106. options: {
  107. filter: {
  108. description: {
  109. regex: 'dolor|tempor',
  110. },
  111. },
  112. },
  113. });
  114. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  115. });
  116. it('complex', async () => {
  117. const { testEntities } = await adminClient.query(GET_LIST, {
  118. options: {
  119. filter: {
  120. description: {
  121. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  122. },
  123. },
  124. },
  125. });
  126. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  127. });
  128. });
  129. });
  130. describe('boolean filtering', () => {
  131. it('eq', async () => {
  132. const { testEntities } = await adminClient.query(GET_LIST, {
  133. options: {
  134. filter: {
  135. active: {
  136. eq: false,
  137. },
  138. },
  139. },
  140. });
  141. expect(getItemLabels(testEntities.items)).toEqual(['C', 'E']);
  142. });
  143. });
  144. describe('number filtering', () => {
  145. it('eq', async () => {
  146. const { testEntities } = await adminClient.query(GET_LIST, {
  147. options: {
  148. filter: {
  149. order: {
  150. eq: 1,
  151. },
  152. },
  153. },
  154. });
  155. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  156. });
  157. it('lt', async () => {
  158. const { testEntities } = await adminClient.query(GET_LIST, {
  159. options: {
  160. filter: {
  161. order: {
  162. lt: 1,
  163. },
  164. },
  165. },
  166. });
  167. expect(getItemLabels(testEntities.items)).toEqual(['A']);
  168. });
  169. it('lte', async () => {
  170. const { testEntities } = await adminClient.query(GET_LIST, {
  171. options: {
  172. filter: {
  173. order: {
  174. lte: 1,
  175. },
  176. },
  177. },
  178. });
  179. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  180. });
  181. it('gt', async () => {
  182. const { testEntities } = await adminClient.query(GET_LIST, {
  183. options: {
  184. filter: {
  185. order: {
  186. gt: 1,
  187. },
  188. },
  189. },
  190. });
  191. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  192. });
  193. it('gte', async () => {
  194. const { testEntities } = await adminClient.query(GET_LIST, {
  195. options: {
  196. filter: {
  197. order: {
  198. gte: 1,
  199. },
  200. },
  201. },
  202. });
  203. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
  204. });
  205. it('between', async () => {
  206. const { testEntities } = await adminClient.query(GET_LIST, {
  207. options: {
  208. filter: {
  209. order: {
  210. between: {
  211. start: 2,
  212. end: 4,
  213. },
  214. },
  215. },
  216. },
  217. });
  218. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  219. });
  220. });
  221. describe('date filtering', () => {
  222. it('before', async () => {
  223. const { testEntities } = await adminClient.query(GET_LIST, {
  224. options: {
  225. filter: {
  226. date: {
  227. before: '2020-01-20T10:00:00.000Z',
  228. },
  229. },
  230. },
  231. });
  232. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  233. });
  234. it('before on same date', async () => {
  235. const { testEntities } = await adminClient.query(GET_LIST, {
  236. options: {
  237. filter: {
  238. date: {
  239. before: '2020-01-15T17:00:00.000Z',
  240. },
  241. },
  242. },
  243. });
  244. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  245. });
  246. it('after', async () => {
  247. const { testEntities } = await adminClient.query(GET_LIST, {
  248. options: {
  249. filter: {
  250. date: {
  251. after: '2020-01-20T10:00:00.000Z',
  252. },
  253. },
  254. },
  255. });
  256. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  257. });
  258. it('after on same date', async () => {
  259. const { testEntities } = await adminClient.query(GET_LIST, {
  260. options: {
  261. filter: {
  262. date: {
  263. after: '2020-01-25T09:00:00.000Z',
  264. },
  265. },
  266. },
  267. });
  268. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  269. });
  270. it('between', async () => {
  271. const { testEntities } = await adminClient.query(GET_LIST, {
  272. options: {
  273. filter: {
  274. date: {
  275. between: {
  276. start: '2020-01-10T10:00:00.000Z',
  277. end: '2020-01-20T10:00:00.000Z',
  278. },
  279. },
  280. },
  281. },
  282. });
  283. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  284. });
  285. });
  286. });
  287. const GET_LIST = gql`
  288. query GetTestEntities($options: TestEntityListOptions) {
  289. testEntities(options: $options) {
  290. totalItems
  291. items {
  292. id
  293. label
  294. date
  295. }
  296. }
  297. }
  298. `;