default-search-plugin.e2e-spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import path from 'path';
  2. import { SEARCH_PRODUCTS } from '../../admin-ui/src/app/data/definitions/product-definitions';
  3. import { SearchProducts } from '../../shared/generated-types';
  4. import { SimpleGraphQLClient } from '../mock-data/simple-graphql-client';
  5. import { DefaultSearchPlugin } from '../src/plugin/default-search-plugin/default-search-plugin';
  6. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  7. import { TestAdminClient, TestShopClient } from './test-client';
  8. import { TestServer } from './test-server';
  9. describe('Default search plugin', () => {
  10. const adminClient = new TestAdminClient();
  11. const shopClient = new TestShopClient();
  12. const server = new TestServer();
  13. beforeAll(async () => {
  14. const token = await server.init(
  15. {
  16. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  17. customerCount: 1,
  18. },
  19. {
  20. plugins: [new DefaultSearchPlugin()],
  21. },
  22. );
  23. await adminClient.init();
  24. await shopClient.init();
  25. }, TEST_SETUP_TIMEOUT_MS);
  26. afterAll(async () => {
  27. await server.destroy();
  28. });
  29. async function testGroupByProduct(client: SimpleGraphQLClient) {
  30. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  31. input: {
  32. groupByProduct: true,
  33. },
  34. });
  35. expect(result.search.totalItems).toBe(20);
  36. }
  37. async function testNoGrouping(client: SimpleGraphQLClient) {
  38. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  39. input: {
  40. groupByProduct: false,
  41. },
  42. });
  43. expect(result.search.totalItems).toBe(34);
  44. }
  45. async function testMatchSearchTerm(client: SimpleGraphQLClient) {
  46. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  47. input: {
  48. term: 'camera',
  49. groupByProduct: true,
  50. },
  51. });
  52. expect(result.search.items.map(i => i.productName)).toEqual([
  53. 'Instant Camera',
  54. 'Camera Lens',
  55. 'SLR Camera',
  56. ]);
  57. }
  58. async function testMatchFacetIds(client: SimpleGraphQLClient) {
  59. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  60. input: {
  61. facetIds: ['T_1', 'T_2'],
  62. groupByProduct: true,
  63. },
  64. });
  65. expect(result.search.items.map(i => i.productName)).toEqual([
  66. 'Laptop',
  67. 'Curvy Monitor',
  68. 'Gaming PC',
  69. 'Hard Drive',
  70. 'Clacky Keyboard',
  71. 'USB Cable',
  72. ]);
  73. }
  74. describe('admin api', () => {
  75. it('group by product', () => testGroupByProduct(adminClient));
  76. it('no grouping', () => testNoGrouping(adminClient));
  77. it('matches search term', () => testMatchSearchTerm(adminClient));
  78. it('matches by facetId', () => testMatchFacetIds(adminClient));
  79. });
  80. describe('shop api', () => {
  81. it('group by product', () => testGroupByProduct(shopClient));
  82. it('no grouping', () => testNoGrouping(shopClient));
  83. it('matches search term', () => testMatchSearchTerm(shopClient));
  84. it('matches by facetId', () => testMatchFacetIds(shopClient));
  85. });
  86. });