validateMessage.spec.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import validateMessage from 'components/Topics/Topic/SendMessage/validateMessage';
  2. import { topicMessageSchema } from 'lib/fixtures/topics';
  3. import cloneDeep from 'lodash/cloneDeep';
  4. describe('validateMessage', () => {
  5. const defaultValidKey = `{"f1": 32, "f2": "multi-state", "schema": "Bedfordshire violet SAS"}`;
  6. const defaultValidContent = `{"f1": 21128, "f2": "Health Berkshire", "schema": "Dynamic"}`;
  7. it('should return empty error data if value is empty', () => {
  8. const key = ``;
  9. const content = ``;
  10. expect(validateMessage(key, content, topicMessageSchema)).toEqual([]);
  11. });
  12. it('should return empty error data if schema is empty', () => {
  13. const key = `{"f1": 32, "f2": "multi-state", "schema": "Bedfordshire violet SAS"}`;
  14. const content = `{"f1": 21128, "f2": "Health Berkshire", "schema": "Dynamic"}`;
  15. const schema = cloneDeep(topicMessageSchema);
  16. schema.key.schema = '';
  17. schema.value.schema = '';
  18. expect(validateMessage(key, content, schema)).toEqual([]);
  19. });
  20. it('should return parsing error data if schema is not parsed with type of key', () => {
  21. const schema = cloneDeep(topicMessageSchema);
  22. schema.key.schema = '{invalid';
  23. expect(
  24. validateMessage(defaultValidKey, defaultValidContent, schema)
  25. ).toEqual([`Error in parsing the "key" field schema`]);
  26. });
  27. it('should return parsing error data if schema is not parsed with type of value', () => {
  28. const schema = cloneDeep(topicMessageSchema);
  29. schema.value.schema = '{invalid';
  30. expect(
  31. validateMessage(defaultValidKey, defaultValidContent, schema)
  32. ).toEqual([`Error in parsing the "content" field schema`]);
  33. });
  34. it('should return empty error data if schema type is string', () => {
  35. const schema = cloneDeep(topicMessageSchema);
  36. schema.key.schema = `{"type": "string"}`;
  37. schema.value.schema = `{"type": "string"}`;
  38. expect(
  39. validateMessage(defaultValidKey, defaultValidContent, schema)
  40. ).toEqual([]);
  41. });
  42. it('should return error data if compile Ajv data throws an error', () => {
  43. expect(
  44. validateMessage(defaultValidKey, defaultValidContent, topicMessageSchema)
  45. ).toEqual([]);
  46. });
  47. it('returns no errors on correct input data', () => {
  48. expect(
  49. validateMessage(
  50. defaultValidContent,
  51. defaultValidContent,
  52. topicMessageSchema
  53. )
  54. ).toEqual([]);
  55. });
  56. it('returns errors on invalid input data', () => {
  57. const key = `{"f1": "32", "f2": "multi-state", "schema": "Bedfordshire violet SAS"}`;
  58. const content = `{"f1": "21128", "f2": "Health Berkshire", "schema": "Dynamic"}`;
  59. expect(validateMessage(key, content, topicMessageSchema)).toEqual([
  60. 'Key/properties/f1/type - must be integer',
  61. 'Content/properties/f1/type - must be integer',
  62. ]);
  63. });
  64. it('returns error on broken key value', () => {
  65. const key = `{"f1": "32", "f2": "multi-state", "schema": "Bedfordshire violet SAS"`;
  66. const content = `{"f1": 21128, "f2": "Health Berkshire", "schema": "Dynamic"}`;
  67. expect(validateMessage(key, content, topicMessageSchema)).toEqual([
  68. 'Error in parsing the "key" field value',
  69. ]);
  70. });
  71. it('returns error on broken content value', () => {
  72. const key = `{"f1": 32, "f2": "multi-state", "schema": "Bedfordshire violet SAS"}`;
  73. const content = `{"f1": 21128, "f2": "Health Berkshire", "schema": "Dynamic"`;
  74. expect(validateMessage(key, content, topicMessageSchema)).toEqual([
  75. 'Error in parsing the "content" field value',
  76. ]);
  77. });
  78. });