graphql-custom-fields.spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { CustomFields } from 'shared/shared-types';
  2. import { addGraphQLCustomFields } from './graphql-custom-fields';
  3. describe('addGraphQLCustomFields()', () => {
  4. it('uses JSON scalar if no custom fields defined', () => {
  5. const input = `
  6. type Product {
  7. id: ID
  8. }
  9. `;
  10. const customFieldConfig: CustomFields = {
  11. Product: [],
  12. };
  13. const result = addGraphQLCustomFields(input, customFieldConfig);
  14. expect(result).toMatchSnapshot();
  15. });
  16. it('extends a type', () => {
  17. const input = `
  18. type Product {
  19. id: ID
  20. }
  21. `;
  22. const customFieldConfig: CustomFields = {
  23. Product: [{ name: 'available', type: 'boolean' }],
  24. };
  25. const result = addGraphQLCustomFields(input, customFieldConfig);
  26. expect(result).toMatchSnapshot();
  27. });
  28. it('extends a type with a translation', () => {
  29. const input = `
  30. type Product {
  31. id: ID
  32. translations: [ProductTranslation!]!
  33. }
  34. type ProductTranslation {
  35. id: ID
  36. }
  37. `;
  38. const customFieldConfig: CustomFields = {
  39. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  40. };
  41. const result = addGraphQLCustomFields(input, customFieldConfig);
  42. expect(result).toMatchSnapshot();
  43. });
  44. it('extends a type with a Create input', () => {
  45. const input = `
  46. type Product {
  47. id: ID
  48. }
  49. input CreateProductInput {
  50. image: String
  51. }
  52. `;
  53. const customFieldConfig: CustomFields = {
  54. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  55. };
  56. const result = addGraphQLCustomFields(input, customFieldConfig);
  57. expect(result).toMatchSnapshot();
  58. });
  59. it('extends a type with an Update input', () => {
  60. const input = `
  61. type Product {
  62. id: ID
  63. }
  64. input UpdateProductInput {
  65. image: String
  66. }
  67. `;
  68. const customFieldConfig: CustomFields = {
  69. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  70. };
  71. const result = addGraphQLCustomFields(input, customFieldConfig);
  72. expect(result).toMatchSnapshot();
  73. });
  74. it('extends a type with a Create input and a translation', () => {
  75. const input = `
  76. type Product {
  77. id: ID
  78. }
  79. type ProductTranslation {
  80. id: ID
  81. }
  82. input ProductTranslationInput {
  83. id: ID
  84. }
  85. input CreateProductInput {
  86. image: String
  87. }
  88. `;
  89. const customFieldConfig: CustomFields = {
  90. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  91. };
  92. const result = addGraphQLCustomFields(input, customFieldConfig);
  93. expect(result).toMatchSnapshot();
  94. });
  95. it('extends a type with SortParameters', () => {
  96. const input = `
  97. type Product {
  98. id: ID
  99. }
  100. input ProductSortParameter {
  101. id: SortOrder
  102. }
  103. enum SortOrder {
  104. ASC
  105. DESC
  106. }
  107. `;
  108. const customFieldConfig: CustomFields = {
  109. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  110. };
  111. const result = addGraphQLCustomFields(input, customFieldConfig);
  112. expect(result).toMatchSnapshot();
  113. });
  114. it('extends a type with FilterParameters', () => {
  115. const input = `
  116. type Product {
  117. name: String
  118. }
  119. input ProductFilterParameter {
  120. id: StringOperators
  121. }
  122. input StringOperators {
  123. eq: String
  124. }
  125. input NumberOperators {
  126. eq: Float
  127. }
  128. input DateOperators {
  129. eq: String
  130. }
  131. input BooleanOperators {
  132. eq: Boolean
  133. }
  134. `;
  135. const customFieldConfig: CustomFields = {
  136. Product: [
  137. { name: 'available', type: 'boolean' },
  138. { name: 'shortName', type: 'localeString' },
  139. { name: 'rating', type: 'float' },
  140. { name: 'published', type: 'datetime' },
  141. ],
  142. };
  143. const result = addGraphQLCustomFields(input, customFieldConfig);
  144. expect(result).toMatchSnapshot();
  145. });
  146. });