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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. await adminClient.query(REINDEX);
  42. await awaitRunningJobs(adminClient);
  43. }, TEST_SETUP_TIMEOUT_MS);
  44. afterAll(async () => {
  45. await server.destroy();
  46. });
  47. it('no term or filters', async () => {
  48. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  49. SEARCH_PRODUCTS_SHOP,
  50. {
  51. input: {
  52. groupByProduct: true,
  53. },
  54. },
  55. );
  56. expect(search.totalItems).toBe(20);
  57. });
  58. it('with search term', async () => {
  59. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  60. SEARCH_PRODUCTS_SHOP,
  61. {
  62. input: {
  63. groupByProduct: true,
  64. term: 'laptop',
  65. },
  66. },
  67. );
  68. expect(search.totalItems).toBe(1);
  69. });
  70. it('with collectionId filter term', async () => {
  71. const { collections } = await shopClient.query<GetCollectionListQuery>(GET_COLLECTION_LIST);
  72. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  73. SEARCH_PRODUCTS_SHOP,
  74. {
  75. input: {
  76. groupByProduct: true,
  77. collectionId: collections.items[0].id,
  78. },
  79. },
  80. );
  81. expect(search.items).not.toEqual([]);
  82. });
  83. });
  84. const REINDEX = gql`
  85. mutation Reindex {
  86. reindex {
  87. id
  88. queueName
  89. state
  90. progress
  91. duration
  92. result
  93. }
  94. }
  95. `;
  96. const GET_COLLECTION_LIST = gql`
  97. query GetCollectionList {
  98. collections {
  99. items {
  100. id
  101. name
  102. }
  103. }
  104. }
  105. `;