add-custom-fields.spec.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { DocumentNode, FieldNode, FragmentDefinitionNode } from 'graphql';
  2. import { CustomFields } from '../../../../shared/shared-types';
  3. import { addCustomFields } from './add-custom-fields';
  4. // tslint:disable:no-non-null-assertion
  5. describe('addCustomFields()', () => {
  6. let documentNode: DocumentNode;
  7. function generateFragmentDefinitionFor(type: keyof CustomFields): FragmentDefinitionNode {
  8. return {
  9. kind: 'FragmentDefinition',
  10. name: {
  11. kind: 'Name',
  12. value: type,
  13. },
  14. typeCondition: {
  15. kind: 'NamedType',
  16. name: {
  17. kind: 'Name',
  18. value: type,
  19. },
  20. },
  21. directives: [],
  22. selectionSet: {
  23. kind: 'SelectionSet',
  24. selections: [],
  25. },
  26. };
  27. }
  28. beforeEach(() => {
  29. documentNode = {
  30. kind: 'Document',
  31. definitions: [
  32. {
  33. kind: 'OperationDefinition',
  34. operation: 'query',
  35. name: {
  36. kind: 'Name',
  37. value: 'GetProductWithVariants',
  38. },
  39. variableDefinitions: [],
  40. directives: [],
  41. selectionSet: {
  42. kind: 'SelectionSet',
  43. selections: [
  44. {
  45. kind: 'Field',
  46. name: {
  47. kind: 'Name',
  48. value: 'product',
  49. },
  50. arguments: [],
  51. directives: [],
  52. selectionSet: {
  53. kind: 'SelectionSet',
  54. selections: [
  55. {
  56. kind: 'FragmentSpread',
  57. name: {
  58. kind: 'Name',
  59. value: 'ProductWithVariants',
  60. },
  61. directives: [],
  62. },
  63. ],
  64. },
  65. },
  66. ],
  67. },
  68. },
  69. {
  70. kind: 'FragmentDefinition',
  71. name: {
  72. kind: 'Name',
  73. value: 'ProductWithVariants',
  74. },
  75. typeCondition: {
  76. kind: 'NamedType',
  77. name: {
  78. kind: 'Name',
  79. value: 'Product',
  80. },
  81. },
  82. directives: [],
  83. selectionSet: {
  84. kind: 'SelectionSet',
  85. selections: [
  86. {
  87. kind: 'Field',
  88. name: {
  89. kind: 'Name',
  90. value: 'id',
  91. },
  92. arguments: [],
  93. directives: [],
  94. },
  95. {
  96. kind: 'Field',
  97. name: {
  98. kind: 'Name',
  99. value: 'translations',
  100. },
  101. arguments: [],
  102. directives: [],
  103. selectionSet: {
  104. kind: 'SelectionSet',
  105. selections: [
  106. {
  107. kind: 'Field',
  108. name: {
  109. kind: 'Name',
  110. value: 'languageCode',
  111. },
  112. arguments: [],
  113. directives: [],
  114. },
  115. {
  116. kind: 'Field',
  117. name: {
  118. kind: 'Name',
  119. value: 'name',
  120. },
  121. arguments: [],
  122. directives: [],
  123. },
  124. ],
  125. },
  126. },
  127. ],
  128. },
  129. },
  130. generateFragmentDefinitionFor('ProductVariant'),
  131. generateFragmentDefinitionFor('ProductOptionGroup'),
  132. generateFragmentDefinitionFor('ProductOption'),
  133. generateFragmentDefinitionFor('User'),
  134. generateFragmentDefinitionFor('Customer'),
  135. generateFragmentDefinitionFor('Address'),
  136. ],
  137. };
  138. });
  139. it('Adds customFields to Product fragment', () => {
  140. const customFieldsConfig: CustomFields = {
  141. Product: [{ name: 'custom1', type: 'string' }, { name: 'custom2', type: 'boolean' }],
  142. };
  143. const result = addCustomFields(documentNode, customFieldsConfig);
  144. const productFragmentDef = result.definitions[1] as FragmentDefinitionNode;
  145. const customFieldsDef = productFragmentDef.selectionSet.selections[2] as FieldNode;
  146. expect(productFragmentDef.selectionSet.selections.length).toBe(3);
  147. expect(customFieldsDef.selectionSet!.selections.length).toBe(2);
  148. expect((customFieldsDef.selectionSet!.selections[0] as FieldNode).name.value).toBe('custom1');
  149. expect((customFieldsDef.selectionSet!.selections[1] as FieldNode).name.value).toBe('custom2');
  150. });
  151. it('Adds customFields to Product translations', () => {
  152. const customFieldsConfig: CustomFields = {
  153. Product: [{ name: 'customLocaleString', type: 'localeString' }],
  154. };
  155. const result = addCustomFields(documentNode, customFieldsConfig);
  156. const productFragmentDef = result.definitions[1] as FragmentDefinitionNode;
  157. const translationsField = productFragmentDef.selectionSet.selections[1] as FieldNode;
  158. const customTranslationFieldsDef = translationsField.selectionSet!.selections[2] as FieldNode;
  159. expect(translationsField.selectionSet!.selections.length).toBe(3);
  160. expect((customTranslationFieldsDef.selectionSet!.selections[0] as FieldNode).name.value).toBe(
  161. 'customLocaleString',
  162. );
  163. });
  164. function addsCustomFieldsToType(type: keyof CustomFields, indexOfDefinition: number) {
  165. const customFieldsConfig: CustomFields = {
  166. [type]: [{ name: 'custom', type: 'boolean' }],
  167. };
  168. const result = addCustomFields(documentNode, customFieldsConfig);
  169. const fragmentDef = result.definitions[indexOfDefinition] as FragmentDefinitionNode;
  170. const customFieldsDef = fragmentDef.selectionSet.selections[0] as FieldNode;
  171. expect(fragmentDef.selectionSet.selections.length).toBe(1);
  172. expect(customFieldsDef.selectionSet!.selections.length).toBe(1);
  173. expect((customFieldsDef.selectionSet!.selections[0] as FieldNode).name.value).toBe('custom');
  174. }
  175. it('Adds customFields to ProductVariant fragment', () => {
  176. addsCustomFieldsToType('ProductVariant', 2);
  177. });
  178. it('Adds customFields to ProductOptionGroup fragment', () => {
  179. addsCustomFieldsToType('ProductOptionGroup', 3);
  180. });
  181. it('Adds customFields to ProductOption fragment', () => {
  182. addsCustomFieldsToType('ProductOption', 4);
  183. });
  184. it('Adds customFields to User fragment', () => {
  185. addsCustomFieldsToType('User', 5);
  186. });
  187. it('Adds customFields to Customer fragment', () => {
  188. addsCustomFieldsToType('Customer', 6);
  189. });
  190. it('Adds customFields to Address fragment', () => {
  191. addsCustomFieldsToType('Address', 7);
  192. });
  193. });