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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import path from 'path';
  2. import {
  3. CREATE_COLLECTION,
  4. UPDATE_COLLECTION,
  5. } from '../../admin-ui/src/app/data/definitions/collection-definitions';
  6. import { SEARCH_PRODUCTS, UPDATE_PRODUCT } from '../../admin-ui/src/app/data/definitions/product-definitions';
  7. import {
  8. ConfigArgType,
  9. CreateCollection,
  10. LanguageCode,
  11. SearchProducts,
  12. UpdateCollection,
  13. UpdateProduct,
  14. } from '../../shared/generated-types';
  15. import { SimpleGraphQLClient } from '../mock-data/simple-graphql-client';
  16. import { facetValueCollectionFilter } from '../src/config/collection/default-collection-filters';
  17. import { DefaultSearchPlugin } from '../src/plugin/default-search-plugin/default-search-plugin';
  18. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  19. import { TestAdminClient, TestShopClient } from './test-client';
  20. import { TestServer } from './test-server';
  21. describe('Default search plugin', () => {
  22. const adminClient = new TestAdminClient();
  23. const shopClient = new TestShopClient();
  24. const server = new TestServer();
  25. beforeAll(async () => {
  26. const token = await server.init(
  27. {
  28. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  29. customerCount: 1,
  30. },
  31. {
  32. plugins: [new DefaultSearchPlugin()],
  33. },
  34. );
  35. await adminClient.init();
  36. await shopClient.init();
  37. }, TEST_SETUP_TIMEOUT_MS);
  38. afterAll(async () => {
  39. await server.destroy();
  40. });
  41. async function testGroupByProduct(client: SimpleGraphQLClient) {
  42. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  43. input: {
  44. groupByProduct: true,
  45. },
  46. });
  47. expect(result.search.totalItems).toBe(20);
  48. }
  49. async function testNoGrouping(client: SimpleGraphQLClient) {
  50. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  51. input: {
  52. groupByProduct: false,
  53. },
  54. });
  55. expect(result.search.totalItems).toBe(34);
  56. }
  57. async function testMatchSearchTerm(client: SimpleGraphQLClient) {
  58. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  59. input: {
  60. term: 'camera',
  61. groupByProduct: true,
  62. },
  63. });
  64. expect(result.search.items.map(i => i.productName)).toEqual([
  65. 'Instant Camera',
  66. 'Camera Lens',
  67. 'SLR Camera',
  68. ]);
  69. }
  70. async function testMatchFacetIds(client: SimpleGraphQLClient) {
  71. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  72. input: {
  73. facetIds: ['T_1', 'T_2'],
  74. groupByProduct: true,
  75. },
  76. });
  77. expect(result.search.items.map(i => i.productName)).toEqual([
  78. 'Laptop',
  79. 'Curvy Monitor',
  80. 'Gaming PC',
  81. 'Hard Drive',
  82. 'Clacky Keyboard',
  83. 'USB Cable',
  84. ]);
  85. }
  86. async function testMatchCollectionId(client: SimpleGraphQLClient) {
  87. const result = await client.query<SearchProducts.Query, SearchProducts.Variables>(SEARCH_PRODUCTS, {
  88. input: {
  89. collectionId: 'T_2',
  90. groupByProduct: true,
  91. },
  92. });
  93. expect(result.search.items.map(i => i.productName)).toEqual([
  94. 'Spiky Cactus',
  95. 'Orchid',
  96. 'Bonsai Tree',
  97. ]);
  98. }
  99. describe('shop api', () => {
  100. it('group by product', () => testGroupByProduct(shopClient));
  101. it('no grouping', () => testNoGrouping(shopClient));
  102. it('matches search term', () => testMatchSearchTerm(shopClient));
  103. it('matches by facetId', () => testMatchFacetIds(shopClient));
  104. it('matches by collectionId', () => testMatchCollectionId(shopClient));
  105. });
  106. describe('admin api', () => {
  107. it('group by product', () => testGroupByProduct(adminClient));
  108. it('no grouping', () => testNoGrouping(adminClient));
  109. it('matches search term', () => testMatchSearchTerm(adminClient));
  110. it('matches by facetId', () => testMatchFacetIds(adminClient));
  111. it('matches by collectionId', () => testMatchCollectionId(adminClient));
  112. it('updates index when a Product is changed', async () => {
  113. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  114. input: {
  115. id: 'T_1',
  116. facetValueIds: [],
  117. },
  118. });
  119. const result = await adminClient.query<SearchProducts.Query, SearchProducts.Variables>(
  120. SEARCH_PRODUCTS,
  121. {
  122. input: {
  123. facetIds: ['T_2'],
  124. groupByProduct: true,
  125. },
  126. },
  127. );
  128. expect(result.search.items.map(i => i.productName)).toEqual([
  129. 'Curvy Monitor',
  130. 'Gaming PC',
  131. 'Hard Drive',
  132. 'Clacky Keyboard',
  133. 'USB Cable',
  134. ]);
  135. });
  136. it('updates index when a Collection is changed', async () => {
  137. await adminClient.query<UpdateCollection.Mutation, UpdateCollection.Variables>(
  138. UPDATE_COLLECTION,
  139. {
  140. input: {
  141. id: 'T_2',
  142. filters: [
  143. {
  144. code: facetValueCollectionFilter.code,
  145. arguments: [
  146. {
  147. name: 'facetValueIds',
  148. value: `["T_4"]`,
  149. type: ConfigArgType.FACET_VALUE_IDS,
  150. },
  151. ],
  152. },
  153. ],
  154. },
  155. },
  156. );
  157. const result = await adminClient.query<SearchProducts.Query, SearchProducts.Variables>(
  158. SEARCH_PRODUCTS,
  159. {
  160. input: {
  161. collectionId: 'T_2',
  162. groupByProduct: true,
  163. },
  164. },
  165. );
  166. expect(result.search.items.map(i => i.productName)).toEqual([
  167. 'Road Bike',
  168. 'Skipping Rope',
  169. 'Boxing Gloves',
  170. 'Tent',
  171. 'Cruiser Skateboard',
  172. 'Football',
  173. 'Running Shoe',
  174. ]);
  175. });
  176. it('updates index when a Collection created', async () => {
  177. const { createCollection } = await adminClient.query<
  178. CreateCollection.Mutation,
  179. CreateCollection.Variables
  180. >(CREATE_COLLECTION, {
  181. input: {
  182. translations: [
  183. {
  184. languageCode: LanguageCode.en,
  185. name: 'Photo',
  186. description: '',
  187. },
  188. ],
  189. filters: [
  190. {
  191. code: facetValueCollectionFilter.code,
  192. arguments: [
  193. {
  194. name: 'facetValueIds',
  195. value: `["T_3"]`,
  196. type: ConfigArgType.FACET_VALUE_IDS,
  197. },
  198. ],
  199. },
  200. ],
  201. },
  202. });
  203. const result = await adminClient.query<SearchProducts.Query, SearchProducts.Variables>(
  204. SEARCH_PRODUCTS,
  205. {
  206. input: {
  207. collectionId: createCollection.id,
  208. groupByProduct: true,
  209. },
  210. },
  211. );
  212. expect(result.search.items.map(i => i.productName)).toEqual([
  213. 'Instant Camera',
  214. 'Camera Lens',
  215. 'Tripod',
  216. 'SLR Camera',
  217. ]);
  218. });
  219. });
  220. });