product-category.e2e-spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import gql from 'graphql-tag';
  2. import {
  3. CREATE_PRODUCT_CATEGORY,
  4. GET_ASSET_LIST,
  5. GET_PRODUCT_CATEGORY,
  6. MOVE_PRODUCT_CATEGORY,
  7. UPDATE_PRODUCT_CATEGORY,
  8. } from '../../admin-ui/src/app/data/definitions/product-definitions';
  9. import {
  10. CreateProductCategory,
  11. GetAssetList,
  12. GetProductCategory,
  13. LanguageCode,
  14. MoveProductCategory,
  15. ProductCategory,
  16. UpdateProductCategory,
  17. } from '../../shared/generated-types';
  18. import { ROOT_CATEGORY_NAME } from '../../shared/shared-constants';
  19. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  20. import { TestClient } from './test-client';
  21. import { TestServer } from './test-server';
  22. describe('ProductCategory resolver', () => {
  23. const client = new TestClient();
  24. const server = new TestServer();
  25. let assets: GetAssetList.Items[];
  26. let electronicsCategory: ProductCategory.Fragment;
  27. let laptopsCategory: ProductCategory.Fragment;
  28. let appleCategory: ProductCategory.Fragment;
  29. beforeAll(async () => {
  30. const token = await server.init({
  31. productCount: 5,
  32. customerCount: 1,
  33. });
  34. await client.init();
  35. const assetsResult = await client.query<GetAssetList.Query, GetAssetList.Variables>(GET_ASSET_LIST);
  36. assets = assetsResult.assets.items;
  37. }, TEST_SETUP_TIMEOUT_MS);
  38. afterAll(async () => {
  39. await server.destroy();
  40. });
  41. describe('createProductCategory', () => {
  42. it('creates a root category', async () => {
  43. const result = await client.query<
  44. CreateProductCategory.Mutation,
  45. CreateProductCategory.Variables
  46. >(CREATE_PRODUCT_CATEGORY, {
  47. input: {
  48. assetIds: [assets[0].id, assets[1].id],
  49. featuredAssetId: assets[1].id,
  50. facetValueIds: ['T_1'],
  51. translations: [{ languageCode: LanguageCode.en, name: 'Electronics', description: '' }],
  52. },
  53. });
  54. electronicsCategory = result.createProductCategory;
  55. expect(electronicsCategory).toMatchSnapshot();
  56. expect(electronicsCategory.parent.name).toBe(ROOT_CATEGORY_NAME);
  57. });
  58. it('creates a nested category', async () => {
  59. const result = await client.query<
  60. CreateProductCategory.Mutation,
  61. CreateProductCategory.Variables
  62. >(CREATE_PRODUCT_CATEGORY, {
  63. input: {
  64. parentId: electronicsCategory.id,
  65. translations: [{ languageCode: LanguageCode.en, name: 'Laptops', description: '' }],
  66. facetValueIds: ['T_2'],
  67. },
  68. });
  69. laptopsCategory = result.createProductCategory;
  70. expect(laptopsCategory.parent.name).toBe(electronicsCategory.name);
  71. });
  72. it('creates a 2nd level nested category', async () => {
  73. const result = await client.query<
  74. CreateProductCategory.Mutation,
  75. CreateProductCategory.Variables
  76. >(CREATE_PRODUCT_CATEGORY, {
  77. input: {
  78. parentId: laptopsCategory.id,
  79. translations: [{ languageCode: LanguageCode.en, name: 'Apple', description: '' }],
  80. facetValueIds: ['T_3', 'T_4'],
  81. },
  82. });
  83. appleCategory = result.createProductCategory;
  84. expect(appleCategory.parent.name).toBe(laptopsCategory.name);
  85. });
  86. });
  87. describe('productCategory query', () => {
  88. it('returns a category', async () => {
  89. const result = await client.query<GetProductCategory.Query, GetProductCategory.Variables>(
  90. GET_PRODUCT_CATEGORY,
  91. { id: laptopsCategory.id },
  92. );
  93. if (!result.productCategory) {
  94. fail(`did not return the category`);
  95. return;
  96. }
  97. expect(result.productCategory.id).toBe(laptopsCategory.id);
  98. });
  99. it('resolves descendantFacetValues 1 level deep', async () => {
  100. const result = await client.query(GET_DECENDANT_FACET_VALUES, { id: laptopsCategory.id });
  101. if (!result.productCategory) {
  102. fail(`did not return the category`);
  103. return;
  104. }
  105. expect(result.productCategory.descendantFacetValues.map(v => v.id)).toEqual(['T_3', 'T_4']);
  106. });
  107. it('resolves descendantFacetValues 2 levels deep', async () => {
  108. const result = await client.query(GET_DECENDANT_FACET_VALUES, { id: electronicsCategory.id });
  109. if (!result.productCategory) {
  110. fail(`did not return the category`);
  111. return;
  112. }
  113. expect(result.productCategory.descendantFacetValues.map(v => v.id)).toEqual([
  114. 'T_2',
  115. 'T_3',
  116. 'T_4',
  117. ]);
  118. });
  119. });
  120. describe('updateProductCategory', () => {
  121. it('updates the details', async () => {
  122. const result = await client.query<
  123. UpdateProductCategory.Mutation,
  124. UpdateProductCategory.Variables
  125. >(UPDATE_PRODUCT_CATEGORY, {
  126. input: {
  127. id: appleCategory.id,
  128. assetIds: [assets[1].id],
  129. featuredAssetId: assets[1].id,
  130. facetValueIds: ['T_3'],
  131. translations: [{ languageCode: LanguageCode.en, description: 'Apple stuff ' }],
  132. },
  133. });
  134. expect(result.updateProductCategory).toMatchSnapshot();
  135. });
  136. });
  137. describe('moveProductCategory', () => {
  138. it('moves a category to a new parent', async () => {
  139. const result = await client.query<MoveProductCategory.Mutation, MoveProductCategory.Variables>(
  140. MOVE_PRODUCT_CATEGORY,
  141. {
  142. input: {
  143. categoryId: appleCategory.id,
  144. parentId: electronicsCategory.id,
  145. index: 0,
  146. },
  147. },
  148. );
  149. expect(result.moveProductCategory.parent.id).toBe(electronicsCategory.id);
  150. const positions = await getChildrenOf(electronicsCategory.id);
  151. expect(positions.map(i => i.id)).toEqual([appleCategory.id, laptopsCategory.id]);
  152. });
  153. it('alters the position in the current parent', async () => {
  154. await client.query<MoveProductCategory.Mutation, MoveProductCategory.Variables>(
  155. MOVE_PRODUCT_CATEGORY,
  156. {
  157. input: {
  158. categoryId: appleCategory.id,
  159. parentId: electronicsCategory.id,
  160. index: 1,
  161. },
  162. },
  163. );
  164. const afterResult = await getChildrenOf(electronicsCategory.id);
  165. expect(afterResult.map(i => i.id)).toEqual([laptopsCategory.id, appleCategory.id]);
  166. });
  167. it('corrects an out-of-bounds negative index value', async () => {
  168. await client.query<MoveProductCategory.Mutation, MoveProductCategory.Variables>(
  169. MOVE_PRODUCT_CATEGORY,
  170. {
  171. input: {
  172. categoryId: appleCategory.id,
  173. parentId: electronicsCategory.id,
  174. index: -3,
  175. },
  176. },
  177. );
  178. const afterResult = await getChildrenOf(electronicsCategory.id);
  179. expect(afterResult.map(i => i.id)).toEqual([appleCategory.id, laptopsCategory.id]);
  180. });
  181. it('corrects an out-of-bounds positive index value', async () => {
  182. await client.query<MoveProductCategory.Mutation, MoveProductCategory.Variables>(
  183. MOVE_PRODUCT_CATEGORY,
  184. {
  185. input: {
  186. categoryId: appleCategory.id,
  187. parentId: electronicsCategory.id,
  188. index: 10,
  189. },
  190. },
  191. );
  192. const afterResult = await getChildrenOf(electronicsCategory.id);
  193. expect(afterResult.map(i => i.id)).toEqual([laptopsCategory.id, appleCategory.id]);
  194. });
  195. it('throws if attempting to move into self', async () => {
  196. try {
  197. await client.query<MoveProductCategory.Mutation, MoveProductCategory.Variables>(
  198. MOVE_PRODUCT_CATEGORY,
  199. {
  200. input: {
  201. categoryId: appleCategory.id,
  202. parentId: appleCategory.id,
  203. index: 0,
  204. },
  205. },
  206. );
  207. fail('Should have thrown');
  208. } catch (err) {
  209. expect(err.message).toEqual(
  210. expect.stringContaining(`Cannot move a ProductCategory into itself`),
  211. );
  212. }
  213. });
  214. it('throws if attempting to move into a decendant of self', async () => {
  215. try {
  216. await client.query<MoveProductCategory.Mutation, MoveProductCategory.Variables>(
  217. MOVE_PRODUCT_CATEGORY,
  218. {
  219. input: {
  220. categoryId: appleCategory.id,
  221. parentId: appleCategory.id,
  222. index: 0,
  223. },
  224. },
  225. );
  226. fail('Should have thrown');
  227. } catch (err) {
  228. expect(err.message).toEqual(
  229. expect.stringContaining(`Cannot move a ProductCategory into itself`),
  230. );
  231. }
  232. });
  233. async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
  234. const result = await client.query(GET_CATEGORIES);
  235. return result.productCategories.items.filter(i => i.parent.id === parentId);
  236. }
  237. });
  238. });
  239. const GET_CATEGORIES = gql`
  240. query GetCategories {
  241. productCategories(languageCode: en) {
  242. items {
  243. id
  244. name
  245. position
  246. parent {
  247. id
  248. name
  249. }
  250. }
  251. }
  252. }
  253. `;
  254. const GET_DECENDANT_FACET_VALUES = gql`
  255. query GetDescendantFacetValues($id: ID!) {
  256. productCategory(id: $id) {
  257. id
  258. descendantFacetValues {
  259. id
  260. name
  261. }
  262. }
  263. }
  264. `;