facet.e2e-spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import gql from 'graphql-tag';
  2. import {
  3. CREATE_FACET,
  4. CREATE_FACET_VALUES,
  5. GET_FACET_LIST,
  6. GET_FACET_WITH_VALUES,
  7. UPDATE_FACET,
  8. UPDATE_FACET_VALUES,
  9. } from '../../admin-ui/src/app/data/definitions/facet-definitions';
  10. import {
  11. GET_PRODUCT_LIST,
  12. GET_PRODUCT_WITH_VARIANTS,
  13. UPDATE_PRODUCT,
  14. UPDATE_PRODUCT_VARIANTS,
  15. } from '../../admin-ui/src/app/data/definitions/product-definitions';
  16. import {
  17. CreateFacet,
  18. CreateFacetValues,
  19. DeletionResult,
  20. FacetWithValues,
  21. GetFacetList,
  22. GetFacetWithValues,
  23. GetProductList,
  24. GetProductWithVariants,
  25. LanguageCode,
  26. UpdateFacet,
  27. UpdateFacetValues,
  28. UpdateProduct,
  29. UpdateProductVariants,
  30. } from '../../shared/generated-types';
  31. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  32. import { TestClient } from './test-client';
  33. import { TestServer } from './test-server';
  34. // tslint:disable:no-non-null-assertion
  35. describe('Facet resolver', () => {
  36. const client = new TestClient();
  37. const server = new TestServer();
  38. let brandFacet: FacetWithValues.Fragment;
  39. let speakerTypeFacet: FacetWithValues.Fragment;
  40. beforeAll(async () => {
  41. await server.init({
  42. productCount: 2,
  43. customerCount: 1,
  44. });
  45. await client.init();
  46. }, TEST_SETUP_TIMEOUT_MS);
  47. afterAll(async () => {
  48. await server.destroy();
  49. });
  50. it('createFacet', async () => {
  51. const result = await client.query<CreateFacet.Mutation, CreateFacet.Variables>(CREATE_FACET, {
  52. input: {
  53. code: 'speaker-type',
  54. translations: [{ languageCode: LanguageCode.en, name: 'Speaker Type' }],
  55. values: [
  56. {
  57. code: 'portable',
  58. translations: [{ languageCode: LanguageCode.en, name: 'Portable' }],
  59. },
  60. ],
  61. },
  62. });
  63. speakerTypeFacet = result.createFacet;
  64. expect(speakerTypeFacet).toMatchSnapshot();
  65. });
  66. it('updateFacet', async () => {
  67. const result = await client.query<UpdateFacet.Mutation, UpdateFacet.Variables>(UPDATE_FACET, {
  68. input: {
  69. id: speakerTypeFacet.id,
  70. translations: [{ languageCode: LanguageCode.en, name: 'Speaker Category' }],
  71. },
  72. });
  73. expect(result.updateFacet.name).toBe('Speaker Category');
  74. });
  75. it('createFacetValues', async () => {
  76. const result = await client.query<CreateFacetValues.Mutation, CreateFacetValues.Variables>(
  77. CREATE_FACET_VALUES,
  78. {
  79. input: [
  80. {
  81. facetId: speakerTypeFacet.id,
  82. code: 'pc',
  83. translations: [{ languageCode: LanguageCode.en, name: 'PC Speakers' }],
  84. },
  85. {
  86. facetId: speakerTypeFacet.id,
  87. code: 'hi-fi',
  88. translations: [{ languageCode: LanguageCode.en, name: 'Hi Fi Speakers' }],
  89. },
  90. ],
  91. },
  92. );
  93. expect(result.createFacetValues).toMatchSnapshot();
  94. });
  95. it('updateFacetValues', async () => {
  96. const result = await client.query<UpdateFacetValues.Mutation, UpdateFacetValues.Variables>(
  97. UPDATE_FACET_VALUES,
  98. {
  99. input: [
  100. {
  101. id: speakerTypeFacet.values[0].id,
  102. code: 'compact',
  103. },
  104. ],
  105. },
  106. );
  107. expect(result.updateFacetValues[0].code).toBe('compact');
  108. });
  109. it('facets', async () => {
  110. const result = await client.query<GetFacetList.Query>(GET_FACET_LIST);
  111. const { items } = result.facets;
  112. expect(items.length).toBe(2);
  113. expect(items[0].name).toBe('Brand');
  114. expect(items[1].name).toBe('Speaker Category');
  115. brandFacet = items[0];
  116. speakerTypeFacet = items[1];
  117. });
  118. it('facet', async () => {
  119. const result = await client.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
  120. GET_FACET_WITH_VALUES,
  121. {
  122. id: speakerTypeFacet.id,
  123. },
  124. );
  125. expect(result.facet!.name).toBe('Speaker Category');
  126. });
  127. describe('deletion', () => {
  128. let products: Array<GetProductList.Items & { variants: Array<{ id: string; name: string }> }>;
  129. beforeAll(async () => {
  130. // add the FacetValues to products and variants
  131. const result1 = await client.query(GET_PRODUCTS_LIST_WITH_VARIANTS);
  132. products = result1.products.items;
  133. await client.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  134. input: {
  135. id: products[0].id,
  136. facetValueIds: [speakerTypeFacet.values[0].id],
  137. },
  138. });
  139. await client.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  140. UPDATE_PRODUCT_VARIANTS,
  141. {
  142. input: [
  143. {
  144. id: products[0].variants[0].id,
  145. facetValueIds: [speakerTypeFacet.values[0].id],
  146. },
  147. ],
  148. },
  149. );
  150. await client.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  151. input: {
  152. id: products[1].id,
  153. facetValueIds: [speakerTypeFacet.values[1].id],
  154. },
  155. });
  156. });
  157. it('deleteFacetValues deletes unused facetValue', async () => {
  158. const facetValueToDelete = speakerTypeFacet.values[2];
  159. const result1 = await client.query(DELETE_FACET_VALUES, {
  160. ids: [facetValueToDelete.id],
  161. force: false,
  162. });
  163. const result2 = await client.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
  164. GET_FACET_WITH_VALUES,
  165. {
  166. id: speakerTypeFacet.id,
  167. },
  168. );
  169. expect(result1.deleteFacetValues).toEqual([
  170. {
  171. result: DeletionResult.DELETED,
  172. message: ``,
  173. },
  174. ]);
  175. expect(result2.facet!.values[0]).not.toEqual(facetValueToDelete);
  176. });
  177. it('deleteFacetValues for FacetValue in use returns NOT_DELETED', async () => {
  178. const facetValueToDelete = speakerTypeFacet.values[0];
  179. const result1 = await client.query(DELETE_FACET_VALUES, {
  180. ids: [facetValueToDelete.id],
  181. force: false,
  182. });
  183. const result2 = await client.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
  184. GET_FACET_WITH_VALUES,
  185. {
  186. id: speakerTypeFacet.id,
  187. },
  188. );
  189. expect(result1.deleteFacetValues).toEqual([
  190. {
  191. result: DeletionResult.NOT_DELETED,
  192. message: `The selected FacetValue is assigned to 1 Product, 1 ProductVariant`,
  193. },
  194. ]);
  195. expect(result2.facet!.values[0]).toEqual(facetValueToDelete);
  196. });
  197. it('deleteFacetValues for FacetValue in use can be force deleted', async () => {
  198. const facetValueToDelete = speakerTypeFacet.values[0];
  199. const result1 = await client.query(DELETE_FACET_VALUES, {
  200. ids: [facetValueToDelete.id],
  201. force: true,
  202. });
  203. expect(result1.deleteFacetValues).toEqual([
  204. {
  205. result: DeletionResult.DELETED,
  206. message: `The selected FacetValue was removed from 1 Product, 1 ProductVariant and deleted`,
  207. },
  208. ]);
  209. // FacetValue no longer in the Facet.values array
  210. const result2 = await client.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
  211. GET_FACET_WITH_VALUES,
  212. {
  213. id: speakerTypeFacet.id,
  214. },
  215. );
  216. expect(result2.facet!.values[0]).not.toEqual(facetValueToDelete);
  217. // FacetValue no longer in the Product.facetValues array
  218. const result3 = await client.query<
  219. GetProductWithVariants.Query,
  220. GetProductWithVariants.Variables
  221. >(GET_PRODUCT_WITH_VARIANTS, {
  222. id: products[0].id,
  223. });
  224. expect(result3.product!.facetValues).toEqual([]);
  225. });
  226. it('deleteFacet that is in use returns NOT_DELETED', async () => {
  227. const result1 = await client.query(DELETE_FACET, { id: speakerTypeFacet.id, force: false });
  228. const result2 = await client.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
  229. GET_FACET_WITH_VALUES,
  230. {
  231. id: speakerTypeFacet.id,
  232. },
  233. );
  234. expect(result1.deleteFacet).toEqual({
  235. result: DeletionResult.NOT_DELETED,
  236. message: `The selected Facet includes FacetValues which are assigned to 1 Product`,
  237. });
  238. expect(result2.facet).not.toBe(null);
  239. });
  240. it('deleteFacet that is in use can be force deleted', async () => {
  241. const result1 = await client.query(DELETE_FACET, { id: speakerTypeFacet.id, force: true });
  242. expect(result1.deleteFacet).toEqual({
  243. result: DeletionResult.DELETED,
  244. message: `The Facet was deleted and its FacetValues were removed from 1 Product`,
  245. });
  246. // FacetValue no longer in the Facet.values array
  247. const result2 = await client.query<GetFacetWithValues.Query, GetFacetWithValues.Variables>(
  248. GET_FACET_WITH_VALUES,
  249. {
  250. id: speakerTypeFacet.id,
  251. },
  252. );
  253. expect(result2.facet).toBe(null);
  254. // FacetValue no longer in the Product.facetValues array
  255. const result3 = await client.query<
  256. GetProductWithVariants.Query,
  257. GetProductWithVariants.Variables
  258. >(GET_PRODUCT_WITH_VARIANTS, {
  259. id: products[1].id,
  260. });
  261. expect(result3.product!.facetValues).toEqual([]);
  262. });
  263. });
  264. });
  265. const DELETE_FACET_VALUES = gql`
  266. mutation DeleteFacetValue($ids: [ID!]!, $force: Boolean) {
  267. deleteFacetValues(ids: $ids, force: $force) {
  268. result
  269. message
  270. }
  271. }
  272. `;
  273. const DELETE_FACET = gql`
  274. mutation DeleteFacet($id: ID!, $force: Boolean) {
  275. deleteFacet(id: $id, force: $force) {
  276. result
  277. message
  278. }
  279. }
  280. `;
  281. const GET_PRODUCTS_LIST_WITH_VARIANTS = gql`
  282. query GetProductListWithVariants {
  283. products {
  284. items {
  285. id
  286. name
  287. variants {
  288. id
  289. name
  290. }
  291. }
  292. totalItems
  293. }
  294. }
  295. `;