annotated.spec.jsx 4.1 KB

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