validate-custom-fields-config.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Type } from '@vendure/common/lib/shared-types';
  2. import { CustomFields } from '../config/custom-field/custom-field-types';
  3. import { coreEntitiesMap } from './entities';
  4. import { validateCustomFieldsConfig } from './validate-custom-fields-config';
  5. describe('validateCustomFieldsConfig()', () => {
  6. const allEntities = Object.values(coreEntitiesMap) as Array<Type<any>>;
  7. it('valid config', () => {
  8. const config: CustomFields = {
  9. Product: [{ name: 'foo', type: 'string' }, { name: 'bar', type: 'localeString' }],
  10. };
  11. const result = validateCustomFieldsConfig(config, allEntities);
  12. expect(result.valid).toBe(true);
  13. expect(result.errors.length).toBe(0);
  14. });
  15. it('invalid localeString', () => {
  16. const config: CustomFields = {
  17. User: [{ name: 'foo', type: 'string' }, { name: 'bar', type: 'localeString' }],
  18. };
  19. const result = validateCustomFieldsConfig(config, allEntities);
  20. expect(result.valid).toBe(false);
  21. expect(result.errors).toEqual(['User entity does not support custom fields of type "localeString"']);
  22. });
  23. it('valid names', () => {
  24. const config: CustomFields = {
  25. User: [
  26. { name: 'love2code', type: 'string' },
  27. { name: 'snake_case', type: 'string' },
  28. { name: 'camelCase', type: 'string' },
  29. { name: 'SHOUTY', type: 'string' },
  30. ],
  31. };
  32. const result = validateCustomFieldsConfig(config, allEntities);
  33. expect(result.valid).toBe(true);
  34. expect(result.errors).toEqual([]);
  35. });
  36. it('invalid names', () => {
  37. const config: CustomFields = {
  38. User: [
  39. { name: '2cool', type: 'string' },
  40. { name: 'has space', type: 'string' },
  41. { name: 'speci@alChar', type: 'string' },
  42. { name: 'has-dash', type: 'string' },
  43. ],
  44. };
  45. const result = validateCustomFieldsConfig(config, allEntities);
  46. expect(result.valid).toBe(false);
  47. expect(result.errors).toEqual([
  48. 'User entity has an invalid custom field name: "2cool"',
  49. 'User entity has an invalid custom field name: "has space"',
  50. 'User entity has an invalid custom field name: "speci@alChar"',
  51. 'User entity has an invalid custom field name: "has-dash"',
  52. ]);
  53. });
  54. it('duplicate names', () => {
  55. const config: CustomFields = {
  56. User: [
  57. { name: 'foo', type: 'string' },
  58. { name: 'bar', type: 'string' },
  59. { name: 'baz', type: 'string' },
  60. { name: 'foo', type: 'boolean' },
  61. { name: 'bar', type: 'boolean' },
  62. ],
  63. };
  64. const result = validateCustomFieldsConfig(config, allEntities);
  65. expect(result.valid).toBe(false);
  66. expect(result.errors).toEqual([
  67. 'User entity has duplicated custom field name: "foo"',
  68. 'User entity has duplicated custom field name: "bar"',
  69. ]);
  70. });
  71. it('duplicate names in translation', () => {
  72. const config: CustomFields = {
  73. Product: [
  74. { name: 'foo', type: 'string' },
  75. { name: 'bar', type: 'string' },
  76. { name: 'baz', type: 'string' },
  77. { name: 'foo', type: 'localeString' },
  78. { name: 'bar', type: 'boolean' },
  79. ],
  80. };
  81. const result = validateCustomFieldsConfig(config, allEntities);
  82. expect(result.valid).toBe(false);
  83. expect(result.errors).toEqual([
  84. 'Product entity has duplicated custom field name: "foo"',
  85. 'Product entity has duplicated custom field name: "bar"',
  86. ]);
  87. });
  88. it('name conflict with existing fields', () => {
  89. const config: CustomFields = {
  90. Product: [{ name: 'createdAt', type: 'string' }],
  91. };
  92. const result = validateCustomFieldsConfig(config, allEntities);
  93. expect(result.valid).toBe(false);
  94. expect(result.errors).toEqual(['Product entity already has a field named "createdAt"']);
  95. });
  96. it('name conflict with existing fields in translation', () => {
  97. const config: CustomFields = {
  98. Product: [{ name: 'name', type: 'string' }],
  99. };
  100. const result = validateCustomFieldsConfig(config, allEntities);
  101. expect(result.valid).toBe(false);
  102. expect(result.errors).toEqual(['Product entity already has a field named "name"']);
  103. });
  104. it('non-nullable must have defaultValue', () => {
  105. const config: CustomFields = {
  106. Product: [{ name: 'foo', type: 'string', nullable: false }],
  107. };
  108. const result = validateCustomFieldsConfig(config, allEntities);
  109. expect(result.valid).toBe(false);
  110. expect(result.errors).toEqual([
  111. 'Product entity custom field "foo" is non-nullable and must have a defaultValue',
  112. ]);
  113. });
  114. });