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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { DefaultJobQueuePlugin, mergeConfig, UuidIdStrategy } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import path from 'path';
  4. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  7. import { searchProductsShopDocument } from '../../core/e2e/graphql/shop-definitions';
  8. import { awaitRunningJobs } from '../../core/e2e/utils/await-running-jobs';
  9. import { ElasticsearchPlugin } from '../src/plugin';
  10. import { graphql } from './graphql/graphql-admin';
  11. import { graphql as shopGraphql } from './graphql/graphql-shop';
  12. // eslint-disable-next-line @typescript-eslint/no-var-requires
  13. const { elasticsearchHost, elasticsearchPort } = require('./constants');
  14. // https://github.com/vendure-ecommerce/vendure/issues/494
  15. describe('Elasticsearch plugin with UuidIdStrategy', () => {
  16. const { server, adminClient, shopClient } = createTestEnvironment(
  17. mergeConfig(testConfig(), {
  18. entityOptions: { entityIdStrategy: new UuidIdStrategy() },
  19. // logger: new DefaultLogger({ level: LogLevel.Info }),
  20. plugins: [
  21. ElasticsearchPlugin.init({
  22. indexPrefix: 'e2e-uuid-tests',
  23. port: elasticsearchPort,
  24. host: elasticsearchHost,
  25. }),
  26. DefaultJobQueuePlugin,
  27. ],
  28. }),
  29. );
  30. beforeAll(async () => {
  31. await server.init({
  32. initialData,
  33. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  34. customerCount: 1,
  35. });
  36. await adminClient.asSuperAdmin();
  37. // We have extra time here because a lot of jobs are
  38. // triggered from all the product updates
  39. await awaitRunningJobs(adminClient, 10_000, 1000);
  40. await adminClient.query(reindexDocument);
  41. await awaitRunningJobs(adminClient);
  42. }, TEST_SETUP_TIMEOUT_MS);
  43. afterAll(async () => {
  44. await server.destroy();
  45. });
  46. it('no term or filters', async () => {
  47. const { search } = await shopClient.query(searchProductsShopDocument, {
  48. input: {
  49. groupByProduct: true,
  50. },
  51. });
  52. expect(search.totalItems).toBe(21);
  53. });
  54. it('no term or filters grouped by SKU', async () => {
  55. const { search } = await shopClient.query(searchProductsShopDocument, {
  56. input: {
  57. groupBySKU: true,
  58. },
  59. } as any);
  60. expect(search.totalItems).toBe(34);
  61. });
  62. it('with search term', async () => {
  63. const { search } = await shopClient.query(searchProductsShopDocument, {
  64. input: {
  65. groupByProduct: true,
  66. term: 'laptop',
  67. },
  68. });
  69. expect(search.totalItems).toBe(1);
  70. });
  71. it('with search term grouped by SKU', async () => {
  72. const { search } = await shopClient.query(searchProductsShopDocument, {
  73. input: {
  74. groupBySKU: true,
  75. term: 'bonsai',
  76. },
  77. });
  78. expect(search.totalItems).toBe(1);
  79. });
  80. it('with collectionId filter term', async () => {
  81. const { collections } = await shopClient.query(getCollectionListDocument);
  82. const { search } = await shopClient.query(searchProductsShopDocument, {
  83. input: {
  84. groupByProduct: true,
  85. collectionId: collections.items[0].id,
  86. },
  87. });
  88. expect(search.items).not.toEqual([]);
  89. });
  90. });
  91. const reindexDocument = graphql(`
  92. mutation Reindex {
  93. reindex {
  94. id
  95. queueName
  96. state
  97. progress
  98. duration
  99. result
  100. }
  101. }
  102. `);
  103. const getCollectionListDocument = shopGraphql(`
  104. query GetCollectionList {
  105. collections {
  106. items {
  107. id
  108. name
  109. }
  110. }
  111. }
  112. `);