product-option.e2e-spec.ts 6.4 KB

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