SendMessage.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React from 'react';
  2. import SendMessage, {
  3. Props,
  4. } from 'components/Topics/Topic/SendMessage/SendMessage';
  5. import { MessageSchemaSourceEnum } from 'generated-sources';
  6. import { fireEvent, render, screen, waitFor } from '@testing-library/react';
  7. const mockConvertToYup = jest
  8. .fn()
  9. .mockReturnValue(() => ({ validate: () => true }));
  10. jest.mock('yup-faker', () => ({
  11. getFakeData: () => ({
  12. f1: -93251214,
  13. schema: 'enim sit in fugiat dolor',
  14. f2: 'deserunt culpa sunt',
  15. }),
  16. }));
  17. const setupWrapper = (props?: Partial<Props>) => (
  18. <SendMessage
  19. clusterName="testCluster"
  20. topicName="testTopic"
  21. fetchTopicMessageSchema={jest.fn()}
  22. sendTopicMessage={jest.fn()}
  23. messageSchema={{
  24. key: {
  25. name: 'key',
  26. source: MessageSchemaSourceEnum.SCHEMA_REGISTRY,
  27. schema: `{
  28. "$schema": "http://json-schema.org/draft-07/schema#",
  29. "$id": "http://example.com/myURI.schema.json",
  30. "title": "TestRecord",
  31. "type": "object",
  32. "additionalProperties": false,
  33. "properties": {
  34. "f1": {
  35. "type": "integer"
  36. },
  37. "f2": {
  38. "type": "string"
  39. },
  40. "schema": {
  41. "type": "string"
  42. }
  43. }
  44. }
  45. `,
  46. },
  47. value: {
  48. name: 'value',
  49. source: MessageSchemaSourceEnum.SCHEMA_REGISTRY,
  50. schema: `{
  51. "$schema": "http://json-schema.org/draft-07/schema#",
  52. "$id": "http://example.com/myURI1.schema.json",
  53. "title": "TestRecord",
  54. "type": "object",
  55. "additionalProperties": false,
  56. "properties": {
  57. "f1": {
  58. "type": "integer"
  59. },
  60. "f2": {
  61. "type": "string"
  62. },
  63. "schema": {
  64. "type": "string"
  65. }
  66. }
  67. }
  68. `,
  69. },
  70. }}
  71. schemaIsFetched={false}
  72. messageIsSending={false}
  73. partitions={[
  74. {
  75. partition: 0,
  76. leader: 2,
  77. replicas: [
  78. {
  79. broker: 2,
  80. leader: false,
  81. inSync: true,
  82. },
  83. ],
  84. offsetMax: 0,
  85. offsetMin: 0,
  86. },
  87. {
  88. partition: 1,
  89. leader: 1,
  90. replicas: [
  91. {
  92. broker: 1,
  93. leader: false,
  94. inSync: true,
  95. },
  96. ],
  97. offsetMax: 0,
  98. offsetMin: 0,
  99. },
  100. ]}
  101. {...props}
  102. />
  103. );
  104. describe('SendMessage', () => {
  105. it('calls fetchTopicMessageSchema on first render', () => {
  106. const fetchTopicMessageSchemaMock = jest.fn();
  107. render(
  108. setupWrapper({ fetchTopicMessageSchema: fetchTopicMessageSchemaMock })
  109. );
  110. expect(fetchTopicMessageSchemaMock).toHaveBeenCalledTimes(1);
  111. });
  112. describe('when schema is fetched', () => {
  113. it('calls sendTopicMessage on submit', async () => {
  114. jest.mock('json-schema-yup-transformer', () => mockConvertToYup);
  115. jest.mock('../validateMessage', () => jest.fn().mockReturnValue(true));
  116. const mockSendTopicMessage = jest.fn();
  117. render(
  118. setupWrapper({
  119. schemaIsFetched: true,
  120. sendTopicMessage: mockSendTopicMessage,
  121. })
  122. );
  123. const select = await screen.findByLabelText('Partition');
  124. fireEvent.change(select, {
  125. target: { value: 2 },
  126. });
  127. await waitFor(async () => {
  128. fireEvent.click(await screen.findByText('Send'));
  129. expect(mockSendTopicMessage).toHaveBeenCalledTimes(1);
  130. });
  131. });
  132. });
  133. });