product-option.e2e-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import { createTestEnvironment } from '@vendure/testing';
  2. import gql from 'graphql-tag';
  3. import path from 'path';
  4. import { initialData } from '../../../e2e-common/e2e-initial-data';
  5. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  6. import { omit } from '../../common/lib/omit';
  7. import { PRODUCT_OPTION_GROUP_FRAGMENT } from './graphql/fragments';
  8. import {
  9. AddOptionGroupToProduct,
  10. AddOptionGroupToProductMutation,
  11. AddOptionGroupToProductMutationVariables,
  12. CreateProduct,
  13. CreateProductMutation,
  14. CreateProductMutationVariables,
  15. CreateProductOption,
  16. CreateProductOptionGroup,
  17. CreateProductVariants,
  18. CreateProductVariantsMutation,
  19. CreateProductVariantsMutationVariables,
  20. DeleteProductOptionMutation,
  21. DeleteProductOptionMutationVariables,
  22. DeleteProductVariantMutation,
  23. DeleteProductVariantMutationVariables,
  24. DeletionResult,
  25. GetProductOptionGroupQuery,
  26. GetProductOptionGroupQueryVariables,
  27. LanguageCode,
  28. ProductOptionGroupFragment,
  29. ProductVariantFragment,
  30. UpdateProductOption,
  31. UpdateProductOptionGroup,
  32. } from './graphql/generated-e2e-admin-types';
  33. import {
  34. ADD_OPTION_GROUP_TO_PRODUCT,
  35. CREATE_PRODUCT,
  36. CREATE_PRODUCT_OPTION_GROUP,
  37. CREATE_PRODUCT_VARIANTS,
  38. DELETE_PRODUCT_VARIANT,
  39. } from './graphql/shared-definitions';
  40. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  41. // tslint:disable:no-non-null-assertion
  42. describe('ProductOption resolver', () => {
  43. const { server, adminClient } = createTestEnvironment(testConfig());
  44. let sizeGroup: ProductOptionGroupFragment;
  45. let mediumOption: CreateProductOption.CreateProductOption;
  46. beforeAll(async () => {
  47. await server.init({
  48. initialData,
  49. customerCount: 1,
  50. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  51. });
  52. await adminClient.asSuperAdmin();
  53. }, TEST_SETUP_TIMEOUT_MS);
  54. afterAll(async () => {
  55. await server.destroy();
  56. });
  57. it('createProductOptionGroup', async () => {
  58. const { createProductOptionGroup } = await adminClient.query<
  59. CreateProductOptionGroup.Mutation,
  60. CreateProductOptionGroup.Variables
  61. >(CREATE_PRODUCT_OPTION_GROUP, {
  62. input: {
  63. code: 'size',
  64. translations: [
  65. { languageCode: LanguageCode.en, name: 'Size' },
  66. { languageCode: LanguageCode.de, name: 'Größe' },
  67. ],
  68. options: [
  69. {
  70. code: 'small',
  71. translations: [
  72. { languageCode: LanguageCode.en, name: 'Small' },
  73. { languageCode: LanguageCode.de, name: 'Klein' },
  74. ],
  75. },
  76. {
  77. code: 'large',
  78. translations: [
  79. { languageCode: LanguageCode.en, name: 'Large' },
  80. { languageCode: LanguageCode.de, name: 'Groß' },
  81. ],
  82. },
  83. ],
  84. },
  85. });
  86. expect(omit(createProductOptionGroup, ['options', 'translations'])).toEqual({
  87. id: 'T_3',
  88. name: 'Size',
  89. code: 'size',
  90. });
  91. sizeGroup = createProductOptionGroup;
  92. });
  93. it('updateProductOptionGroup', async () => {
  94. const { updateProductOptionGroup } = await adminClient.query<
  95. UpdateProductOptionGroup.Mutation,
  96. UpdateProductOptionGroup.Variables
  97. >(UPDATE_PRODUCT_OPTION_GROUP, {
  98. input: {
  99. id: sizeGroup.id,
  100. translations: [
  101. { id: sizeGroup.translations[0].id, languageCode: LanguageCode.en, name: 'Bigness' },
  102. ],
  103. },
  104. });
  105. expect(updateProductOptionGroup.name).toBe('Bigness');
  106. });
  107. it(
  108. 'createProductOption throws with invalid productOptionGroupId',
  109. assertThrowsWithMessage(async () => {
  110. const { createProductOption } = await adminClient.query<
  111. CreateProductOption.Mutation,
  112. CreateProductOption.Variables
  113. >(CREATE_PRODUCT_OPTION, {
  114. input: {
  115. productOptionGroupId: 'T_999',
  116. code: 'medium',
  117. translations: [
  118. { languageCode: LanguageCode.en, name: 'Medium' },
  119. { languageCode: LanguageCode.de, name: 'Mittel' },
  120. ],
  121. },
  122. });
  123. }, "No ProductOptionGroup with the id '999' could be found"),
  124. );
  125. it('createProductOption', async () => {
  126. const { createProductOption } = await adminClient.query<
  127. CreateProductOption.Mutation,
  128. CreateProductOption.Variables
  129. >(CREATE_PRODUCT_OPTION, {
  130. input: {
  131. productOptionGroupId: sizeGroup.id,
  132. code: 'medium',
  133. translations: [
  134. { languageCode: LanguageCode.en, name: 'Medium' },
  135. { languageCode: LanguageCode.de, name: 'Mittel' },
  136. ],
  137. },
  138. });
  139. expect(omit(createProductOption, ['translations'])).toEqual({
  140. id: 'T_7',
  141. groupId: sizeGroup.id,
  142. code: 'medium',
  143. name: 'Medium',
  144. });
  145. mediumOption = createProductOption;
  146. });
  147. it('updateProductOption', async () => {
  148. const { updateProductOption } = await adminClient.query<
  149. UpdateProductOption.Mutation,
  150. UpdateProductOption.Variables
  151. >(UPDATE_PRODUCT_OPTION, {
  152. input: {
  153. id: 'T_7',
  154. translations: [
  155. { id: mediumOption.translations[0].id, languageCode: LanguageCode.en, name: 'Middling' },
  156. ],
  157. },
  158. });
  159. expect(updateProductOption.name).toBe('Middling');
  160. });
  161. describe('deletion', () => {
  162. let sizeOptionGroupWithOptions: NonNullable<GetProductOptionGroupQuery['productOptionGroup']>;
  163. let variants: CreateProductVariantsMutation['createProductVariants'];
  164. beforeAll(async () => {
  165. // Create a new product with a variant in each size option
  166. const { createProduct } = await adminClient.query<
  167. CreateProductMutation,
  168. CreateProductMutationVariables
  169. >(CREATE_PRODUCT, {
  170. input: {
  171. translations: [
  172. {
  173. languageCode: LanguageCode.en,
  174. name: 'T-shirt',
  175. slug: 't-shirt',
  176. description: 'A television set',
  177. },
  178. ],
  179. },
  180. });
  181. const result = await adminClient.query<
  182. AddOptionGroupToProductMutation,
  183. AddOptionGroupToProductMutationVariables
  184. >(ADD_OPTION_GROUP_TO_PRODUCT, {
  185. optionGroupId: sizeGroup.id,
  186. productId: createProduct.id,
  187. });
  188. const { productOptionGroup } = await adminClient.query<
  189. GetProductOptionGroupQuery,
  190. GetProductOptionGroupQueryVariables
  191. >(GET_PRODUCT_OPTION_GROUP, {
  192. id: sizeGroup.id,
  193. });
  194. const variantInput: CreateProductVariantsMutationVariables['input'] =
  195. productOptionGroup!.options.map((option, i) => ({
  196. productId: createProduct.id,
  197. sku: `TS-${option.code}`,
  198. optionIds: [option.id],
  199. translations: [{ languageCode: LanguageCode.en, name: `T-shirt ${option.code}` }],
  200. }));
  201. const { createProductVariants } = await adminClient.query<
  202. CreateProductVariantsMutation,
  203. CreateProductVariantsMutationVariables
  204. >(CREATE_PRODUCT_VARIANTS, {
  205. input: variantInput,
  206. });
  207. variants = createProductVariants;
  208. sizeOptionGroupWithOptions = productOptionGroup!;
  209. });
  210. it(
  211. 'attempting to delete a non-existent id throws',
  212. assertThrowsWithMessage(
  213. () =>
  214. adminClient.query<DeleteProductOptionMutation, DeleteProductOptionMutationVariables>(
  215. DELETE_PRODUCT_OPTION,
  216. {
  217. id: '999999',
  218. },
  219. ),
  220. "No ProductOption with the id '999999' could be found",
  221. ),
  222. );
  223. it('cannot delete ProductOption that is used by a ProductVariant', async () => {
  224. const { deleteProductOption } = await adminClient.query<
  225. DeleteProductOptionMutation,
  226. DeleteProductOptionMutationVariables
  227. >(DELETE_PRODUCT_OPTION, {
  228. id: sizeOptionGroupWithOptions.options.find(o => o.code === 'medium')!.id,
  229. });
  230. expect(deleteProductOption.result).toBe(DeletionResult.NOT_DELETED);
  231. expect(deleteProductOption.message).toBe(
  232. 'Cannot delete the option "medium" as it is being used by 1 ProductVariant',
  233. );
  234. });
  235. it('can delete ProductOption after deleting associated ProductVariant', async () => {
  236. const { deleteProductVariant } = await adminClient.query<
  237. DeleteProductVariantMutation,
  238. DeleteProductVariantMutationVariables
  239. >(DELETE_PRODUCT_VARIANT, {
  240. id: variants.find(v => v!.name.includes('medium'))!.id,
  241. });
  242. expect(deleteProductVariant.result).toBe(DeletionResult.DELETED);
  243. const { deleteProductOption } = await adminClient.query<
  244. DeleteProductOptionMutation,
  245. DeleteProductOptionMutationVariables
  246. >(DELETE_PRODUCT_OPTION, {
  247. id: sizeOptionGroupWithOptions.options.find(o => o.code === 'medium')!.id,
  248. });
  249. expect(deleteProductOption.result).toBe(DeletionResult.DELETED);
  250. });
  251. it('deleted ProductOptions not included in query result', async () => {
  252. const { productOptionGroup } = await adminClient.query<
  253. GetProductOptionGroupQuery,
  254. GetProductOptionGroupQueryVariables
  255. >(GET_PRODUCT_OPTION_GROUP, {
  256. id: sizeGroup.id,
  257. });
  258. expect(productOptionGroup?.options.length).toBe(2);
  259. expect(productOptionGroup?.options.findIndex(o => o.code === 'medium')).toBe(-1);
  260. });
  261. });
  262. });
  263. const GET_PRODUCT_OPTION_GROUP = gql`
  264. query GetProductOptionGroup($id: ID!) {
  265. productOptionGroup(id: $id) {
  266. id
  267. code
  268. name
  269. options {
  270. id
  271. code
  272. name
  273. }
  274. }
  275. }
  276. `;
  277. const UPDATE_PRODUCT_OPTION_GROUP = gql`
  278. mutation UpdateProductOptionGroup($input: UpdateProductOptionGroupInput!) {
  279. updateProductOptionGroup(input: $input) {
  280. ...ProductOptionGroup
  281. }
  282. }
  283. ${PRODUCT_OPTION_GROUP_FRAGMENT}
  284. `;
  285. const CREATE_PRODUCT_OPTION = gql`
  286. mutation CreateProductOption($input: CreateProductOptionInput!) {
  287. createProductOption(input: $input) {
  288. id
  289. code
  290. name
  291. groupId
  292. translations {
  293. id
  294. languageCode
  295. name
  296. }
  297. }
  298. }
  299. `;
  300. const UPDATE_PRODUCT_OPTION = gql`
  301. mutation UpdateProductOption($input: UpdateProductOptionInput!) {
  302. updateProductOption(input: $input) {
  303. id
  304. code
  305. name
  306. groupId
  307. }
  308. }
  309. `;
  310. const DELETE_PRODUCT_OPTION = gql`
  311. mutation DeleteProductOption($id: ID!) {
  312. deleteProductOption(id: $id) {
  313. result
  314. message
  315. }
  316. }
  317. `;