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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 { TEST_SETUP_TIMEOUT_MS, testConfig } 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/vendurehq/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(21);
  60. });
  61. it('no term or filters grouped by SKU', async () => {
  62. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  63. SEARCH_PRODUCTS_SHOP,
  64. {
  65. input: {
  66. groupBySKU: true,
  67. },
  68. },
  69. );
  70. expect(search.totalItems).toBe(34);
  71. });
  72. it('with search term', async () => {
  73. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  74. SEARCH_PRODUCTS_SHOP,
  75. {
  76. input: {
  77. groupByProduct: true,
  78. term: 'laptop',
  79. },
  80. },
  81. );
  82. expect(search.totalItems).toBe(1);
  83. });
  84. it('with search term grouped by SKU', async () => {
  85. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  86. SEARCH_PRODUCTS_SHOP,
  87. {
  88. input: {
  89. groupBySKU: true,
  90. term: 'bonsai',
  91. },
  92. },
  93. );
  94. expect(search.totalItems).toBe(1);
  95. });
  96. it('with collectionId filter term', async () => {
  97. const { collections } = await shopClient.query<GetCollectionListQuery>(GET_COLLECTION_LIST);
  98. const { search } = await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariables>(
  99. SEARCH_PRODUCTS_SHOP,
  100. {
  101. input: {
  102. groupByProduct: true,
  103. collectionId: collections.items[0].id,
  104. },
  105. },
  106. );
  107. expect(search.items).not.toEqual([]);
  108. });
  109. });
  110. const REINDEX = gql`
  111. mutation Reindex {
  112. reindex {
  113. id
  114. queueName
  115. state
  116. progress
  117. duration
  118. result
  119. }
  120. }
  121. `;
  122. const GET_COLLECTION_LIST = gql`
  123. query GetCollectionList {
  124. collections {
  125. items {
  126. id
  127. name
  128. }
  129. }
  130. }
  131. `;