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

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