elasticsearch-plugin-uuid.e2e-spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { DefaultJobQueuePlugin, DefaultLogger, LogLevel, mergeConfig, UuidIdStrategy } 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 { SearchProductsShop } from '../../core/e2e/graphql/generated-e2e-shop-types';
  8. import { SEARCH_PRODUCTS_SHOP } from '../../core/e2e/graphql/shop-definitions';
  9. import { awaitRunningJobs } from '../../core/e2e/utils/await-running-jobs';
  10. import { ElasticsearchPlugin } from '../src/plugin';
  11. import { GetCollectionList } from './graphql/generated-e2e-elasticsearch-plugin-types';
  12. // tslint:disable-next-line:no-var-requires
  13. const { elasticsearchHost, elasticsearchPort } = require('./constants');
  14. /**
  15. * The Elasticsearch tests sometimes take a long time in CI due to limited resources.
  16. * We increase the timeout to 30 seconds to prevent failure due to timeouts.
  17. */
  18. if (process.env.CI) {
  19. jest.setTimeout(10 * 3000);
  20. }
  21. // https://github.com/vendure-ecommerce/vendure/issues/494
  22. describe('Elasticsearch plugin with UuidIdStrategy', () => {
  23. const { server, adminClient, shopClient } = createTestEnvironment(
  24. mergeConfig(testConfig(), {
  25. entityOptions: { entityIdStrategy: new UuidIdStrategy() },
  26. // logger: new DefaultLogger({ level: LogLevel.Info }),
  27. plugins: [
  28. ElasticsearchPlugin.init({
  29. indexPrefix: 'e2e-uuid-tests',
  30. port: elasticsearchPort,
  31. host: elasticsearchHost,
  32. }),
  33. DefaultJobQueuePlugin,
  34. ],
  35. }),
  36. );
  37. beforeAll(async () => {
  38. await server.init({
  39. initialData,
  40. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  41. customerCount: 1,
  42. });
  43. await adminClient.asSuperAdmin();
  44. await adminClient.query(REINDEX);
  45. await awaitRunningJobs(adminClient);
  46. }, TEST_SETUP_TIMEOUT_MS);
  47. afterAll(async () => {
  48. await server.destroy();
  49. });
  50. it('no term or filters', async () => {
  51. const { search } = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  52. SEARCH_PRODUCTS_SHOP,
  53. {
  54. input: {
  55. groupByProduct: true,
  56. },
  57. },
  58. );
  59. expect(search.totalItems).toBe(20);
  60. });
  61. it('with search term', async () => {
  62. const { search } = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  63. SEARCH_PRODUCTS_SHOP,
  64. {
  65. input: {
  66. groupByProduct: true,
  67. term: 'laptop',
  68. },
  69. },
  70. );
  71. expect(search.totalItems).toBe(1);
  72. });
  73. it('with collectionId filter term', async () => {
  74. const { collections } = await shopClient.query<GetCollectionList.Query>(GET_COLLECTION_LIST);
  75. const { search } = await shopClient.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
  76. SEARCH_PRODUCTS_SHOP,
  77. {
  78. input: {
  79. groupByProduct: true,
  80. collectionId: collections.items[0].id,
  81. },
  82. },
  83. );
  84. expect(search.items).not.toEqual([]);
  85. });
  86. });
  87. const REINDEX = gql`
  88. mutation Reindex {
  89. reindex {
  90. id
  91. queueName
  92. state
  93. progress
  94. duration
  95. result
  96. }
  97. }
  98. `;
  99. const GET_COLLECTION_LIST = gql`
  100. query GetCollectionList {
  101. collections {
  102. items {
  103. id
  104. name
  105. }
  106. }
  107. }
  108. `;