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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { DefaultJobQueuePlugin, mergeConfig, UuidIdStrategy } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  8. import {
  9. SearchProductsShopQuery,
  10. SearchProductsShopQueryVariables,
  11. } from '../../core/e2e/graphql/generated-e2e-shop-types';
  12. import { SEARCH_PRODUCTS_SHOP } from '../../core/e2e/graphql/shop-definitions';
  13. import { awaitRunningJobs } from '../../core/e2e/utils/await-running-jobs';
  14. import { ElasticsearchPlugin } from '../src/plugin';
  15. import { GetCollectionListQuery } from './graphql/generated-e2e-elasticsearch-plugin-types';
  16. // eslint-disable-next-line @typescript-eslint/no-var-requires
  17. const { elasticsearchHost, elasticsearchPort } = require('./constants');
  18. // https://github.com/vendure-ecommerce/vendure/issues/494
  19. describe('Elasticsearch plugin with UuidIdStrategy', () => {
  20. const { server, adminClient, shopClient } = createTestEnvironment(
  21. mergeConfig(testConfig(), {
  22. entityOptions: { entityIdStrategy: new UuidIdStrategy() },
  23. // logger: new DefaultLogger({ level: LogLevel.Info }),
  24. plugins: [
  25. ElasticsearchPlugin.init({
  26. indexPrefix: 'e2e-uuid-tests',
  27. port: elasticsearchPort,
  28. host: elasticsearchHost,
  29. }),
  30. DefaultJobQueuePlugin,
  31. ],
  32. }),
  33. );
  34. beforeAll(async () => {
  35. await server.init({
  36. initialData,
  37. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  38. customerCount: 1,
  39. });
  40. await adminClient.asSuperAdmin();
  41. // We have extra time here because a lot of jobs are
  42. // triggered from all the product updates
  43. await awaitRunningJobs(adminClient, 10_000, 1000);
  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<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  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<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  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<GetCollectionListQuery>(GET_COLLECTION_LIST);
  75. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  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. `;