annotated.spec.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import Annotated from 'app/components/events/meta/annotated';
  4. import {withMeta} from 'app/components/events/meta/metaProxy';
  5. describe('Annotated', () => {
  6. const mock = jest.fn(() => null);
  7. const createEvent = (value, {err, rem, chunks} = {}) => {
  8. return withMeta({
  9. value,
  10. _meta: {
  11. value: {
  12. '': {
  13. err: err || [],
  14. rem: rem || [],
  15. chunks: chunks || [],
  16. },
  17. },
  18. },
  19. });
  20. };
  21. beforeEach(function() {
  22. mock.mockClear();
  23. });
  24. describe('without meta', () => {
  25. it('renders a string', () => {
  26. const obj = {
  27. value: 'foo',
  28. };
  29. mount(
  30. <Annotated object={obj} prop="value">
  31. {mock}
  32. </Annotated>
  33. );
  34. expect(mock).toHaveBeenCalledWith('foo');
  35. });
  36. it('does not error if prop does not exist on object', () => {
  37. const obj = {
  38. value: 'foo',
  39. };
  40. mount(<Annotated object={obj} prop="invalid" />);
  41. });
  42. it('renders a number', () => {
  43. const obj = {
  44. value: 0,
  45. };
  46. mount(
  47. <Annotated object={obj} prop="value">
  48. {mock}
  49. </Annotated>
  50. );
  51. expect(mock).toHaveBeenCalledWith(0);
  52. });
  53. it('renders a boolean', () => {
  54. const obj = {
  55. value: false,
  56. };
  57. mount(
  58. <Annotated object={obj} prop="value">
  59. {mock}
  60. </Annotated>
  61. );
  62. expect(mock).toHaveBeenCalledWith(false);
  63. });
  64. it('ignores empty meta data', () => {
  65. const obj = withMeta({
  66. value: 'foo',
  67. _meta: {
  68. value: {
  69. '': {
  70. err: [],
  71. rem: [],
  72. chunks: [],
  73. },
  74. },
  75. },
  76. });
  77. mount(
  78. <Annotated object={obj} prop="value">
  79. {mock}
  80. </Annotated>
  81. );
  82. expect(mock).toHaveBeenCalledWith('foo');
  83. });
  84. it('does not call render prop if required and value is falsy and no meta', () => {
  85. const obj = createEvent(null, {});
  86. mount(
  87. <Annotated object={obj} prop="value" required>
  88. {mock}
  89. </Annotated>
  90. );
  91. expect(mock).not.toHaveBeenCalled();
  92. });
  93. });
  94. describe('with meta', () => {
  95. it('annotates errors', () => {
  96. const obj = createEvent('foo', {err: ['something']});
  97. mount(
  98. <Annotated object={obj} prop="value">
  99. {mock}
  100. </Annotated>
  101. );
  102. expect(mock.mock.calls[0][0].props).toEqual(
  103. expect.objectContaining({
  104. value: 'foo',
  105. chunks: [],
  106. remarks: [],
  107. errors: ['something'],
  108. })
  109. );
  110. });
  111. it('annotates remarks and chunks', () => {
  112. const obj = createEvent('foo', {rem: [{type: 't'}], chunks: [{text: 'foo'}]});
  113. mount(
  114. <Annotated object={obj} prop="value">
  115. {mock}
  116. </Annotated>
  117. );
  118. expect(mock.mock.calls[0][0].props).toEqual(
  119. expect.objectContaining({
  120. value: 'foo',
  121. remarks: [{type: 't'}],
  122. chunks: [{text: 'foo'}],
  123. errors: [],
  124. })
  125. );
  126. });
  127. it('annotates redacted text', () => {
  128. const obj = createEvent(null, {err: ['something']});
  129. mount(
  130. <Annotated object={obj} prop="value">
  131. {mock}
  132. </Annotated>
  133. );
  134. expect(mock.mock.calls[0][0].props).toEqual(
  135. expect.objectContaining({
  136. value: null,
  137. chunks: [],
  138. remarks: [],
  139. errors: ['something'],
  140. })
  141. );
  142. });
  143. });
  144. });