product-option.e2e-spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  6. import { omit } from '../../common/lib/omit';
  7. import {
  8. CreateProductOption,
  9. CreateProductOptionGroup,
  10. LanguageCode,
  11. ProductOptionGroupFragment,
  12. UpdateProductOption,
  13. UpdateProductOptionGroup,
  14. } from './graphql/generated-e2e-admin-types';
  15. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  16. // tslint:disable:no-non-null-assertion
  17. describe('ProductOption resolver', () => {
  18. const { server, adminClient } = createTestEnvironment(testConfig);
  19. let sizeGroup: ProductOptionGroupFragment;
  20. let mediumOption: CreateProductOption.CreateProductOption;
  21. beforeAll(async () => {
  22. await server.init({
  23. dataDir: path.join(__dirname, '__data__'),
  24. initialData,
  25. customerCount: 1,
  26. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  27. });
  28. await adminClient.asSuperAdmin();
  29. }, TEST_SETUP_TIMEOUT_MS);
  30. afterAll(async () => {
  31. await server.destroy();
  32. });
  33. it('createProductOptionGroup', async () => {
  34. const { createProductOptionGroup } = await adminClient.query<
  35. CreateProductOptionGroup.Mutation,
  36. CreateProductOptionGroup.Variables
  37. >(CREATE_PRODUCT_OPTION_GROUP, {
  38. input: {
  39. code: 'size',
  40. translations: [
  41. { languageCode: LanguageCode.en, name: 'Size' },
  42. { languageCode: LanguageCode.de, name: 'Größe' },
  43. ],
  44. options: [
  45. {
  46. code: 'small',
  47. translations: [
  48. { languageCode: LanguageCode.en, name: 'Small' },
  49. { languageCode: LanguageCode.de, name: 'Klein' },
  50. ],
  51. },
  52. {
  53. code: 'large',
  54. translations: [
  55. { languageCode: LanguageCode.en, name: 'Large' },
  56. { languageCode: LanguageCode.de, name: 'Groß' },
  57. ],
  58. },
  59. ],
  60. },
  61. });
  62. expect(omit(createProductOptionGroup, ['options', 'translations'])).toEqual({
  63. id: 'T_3',
  64. name: 'Size',
  65. code: 'size',
  66. });
  67. sizeGroup = createProductOptionGroup;
  68. });
  69. it('updateProductOptionGroup', async () => {
  70. const { updateProductOptionGroup } = await adminClient.query<
  71. UpdateProductOptionGroup.Mutation,
  72. UpdateProductOptionGroup.Variables
  73. >(UPDATE_PRODUCT_OPTION_GROUP, {
  74. input: {
  75. id: sizeGroup.id,
  76. translations: [
  77. { id: sizeGroup.translations[0].id, languageCode: LanguageCode.en, name: 'Bigness' },
  78. ],
  79. },
  80. });
  81. expect(updateProductOptionGroup.name).toBe('Bigness');
  82. });
  83. it(
  84. 'createProductOption throws with invalid productOptionGroupId',
  85. assertThrowsWithMessage(async () => {
  86. const { createProductOption } = await adminClient.query<
  87. CreateProductOption.Mutation,
  88. CreateProductOption.Variables
  89. >(CREATE_PRODUCT_OPTION, {
  90. input: {
  91. productOptionGroupId: 'T_999',
  92. code: 'medium',
  93. translations: [
  94. { languageCode: LanguageCode.en, name: 'Medium' },
  95. { languageCode: LanguageCode.de, name: 'Mittel' },
  96. ],
  97. },
  98. });
  99. }, "No ProductOptionGroup with the id '999' could be found"),
  100. );
  101. it('createProductOption', async () => {
  102. const { createProductOption } = await adminClient.query<
  103. CreateProductOption.Mutation,
  104. CreateProductOption.Variables
  105. >(CREATE_PRODUCT_OPTION, {
  106. input: {
  107. productOptionGroupId: sizeGroup.id,
  108. code: 'medium',
  109. translations: [
  110. { languageCode: LanguageCode.en, name: 'Medium' },
  111. { languageCode: LanguageCode.de, name: 'Mittel' },
  112. ],
  113. },
  114. });
  115. expect(omit(createProductOption, ['translations'])).toEqual({
  116. id: 'T_7',
  117. groupId: sizeGroup.id,
  118. code: 'medium',
  119. name: 'Medium',
  120. });
  121. mediumOption = createProductOption;
  122. });
  123. it('updateProductOption', async () => {
  124. const { updateProductOption } = await adminClient.query<
  125. UpdateProductOption.Mutation,
  126. UpdateProductOption.Variables
  127. >(UPDATE_PRODUCT_OPTION, {
  128. input: {
  129. id: 'T_7',
  130. translations: [
  131. { id: mediumOption.translations[0].id, languageCode: LanguageCode.en, name: 'Middling' },
  132. ],
  133. },
  134. });
  135. expect(updateProductOption.name).toBe('Middling');
  136. });
  137. });
  138. const PRODUCT_OPTION_GROUP_FRAGMENT = gql`
  139. fragment ProductOptionGroup on ProductOptionGroup {
  140. id
  141. code
  142. name
  143. options {
  144. id
  145. code
  146. name
  147. }
  148. translations {
  149. id
  150. languageCode
  151. name
  152. }
  153. }
  154. `;
  155. const CREATE_PRODUCT_OPTION_GROUP = gql`
  156. mutation CreateProductOptionGroup($input: CreateProductOptionGroupInput!) {
  157. createProductOptionGroup(input: $input) {
  158. ...ProductOptionGroup
  159. }
  160. }
  161. ${PRODUCT_OPTION_GROUP_FRAGMENT}
  162. `;
  163. const UPDATE_PRODUCT_OPTION_GROUP = gql`
  164. mutation UpdateProductOptionGroup($input: UpdateProductOptionGroupInput!) {
  165. updateProductOptionGroup(input: $input) {
  166. ...ProductOptionGroup
  167. }
  168. }
  169. ${PRODUCT_OPTION_GROUP_FRAGMENT}
  170. `;
  171. const CREATE_PRODUCT_OPTION = gql`
  172. mutation CreateProductOption($input: CreateProductOptionInput!) {
  173. createProductOption(input: $input) {
  174. id
  175. code
  176. name
  177. groupId
  178. translations {
  179. id
  180. languageCode
  181. name
  182. }
  183. }
  184. }
  185. `;
  186. const UPDATE_PRODUCT_OPTION = gql`
  187. mutation UpdateProductOption($input: UpdateProductOptionInput!) {
  188. updateProductOption(input: $input) {
  189. id
  190. code
  191. name
  192. groupId
  193. }
  194. }
  195. `;