|
| 1 | +// We can't test wrap with mock and non mocked components, otherwise it will break the RN testing library. |
| 2 | +import { render } from '@testing-library/react-native'; |
| 3 | +import * as React from 'react'; |
| 4 | +import type { ReactNativeWrapperOptions } from 'src/js/options'; |
| 5 | + |
| 6 | +jest.doMock('../src/js/touchevents', () => { |
| 7 | + return { |
| 8 | + TouchEventBoundary: ({ children }: { children: React.ReactNode }) => ( |
| 9 | + // eslint-disable-next-line react/no-unknown-property |
| 10 | + <div testID="touch-boundaryID">{children}</div> |
| 11 | + ), |
| 12 | + } |
| 13 | +}); |
| 14 | + |
| 15 | +jest.doMock('../src/js/tracing', () => { |
| 16 | + return { |
| 17 | + ReactNativeProfiler: jest.fn(({ children }: { children: React.ReactNode }) => ( |
| 18 | + // eslint-disable-next-line react/no-unknown-property |
| 19 | + <div testID="profilerID">{children}</div> |
| 20 | + )), |
| 21 | + } |
| 22 | +}); |
| 23 | + |
| 24 | +jest.doMock('../src/js/feedback/FeedbackWidgetManager', () => { |
| 25 | + return { |
| 26 | + FeedbackWidgetProvider: ({ children }: { children: React.ReactNode }) => ( |
| 27 | + // eslint-disable-next-line react/no-unknown-property |
| 28 | + <div testID="feedback-widgetID">{children}</div> |
| 29 | + ), |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | + |
| 34 | +import { wrap } from '../src/js/sdk'; |
| 35 | +import { ReactNativeProfiler } from '../src/js/tracing'; |
| 36 | + |
| 37 | +describe('Sentry.wrap', () => { |
| 38 | + |
| 39 | + const DummyComponent: React.FC<{ value?: string }> = ({ value }) => <div>{value}</div>; |
| 40 | + |
| 41 | + it('should not enforce any keys on the wrapped component', () => { |
| 42 | + const Mock: React.FC<{ test: 23 }> = () => <></>; |
| 43 | + const ActualWrapped = wrap(Mock); |
| 44 | + |
| 45 | + expect(typeof ActualWrapped.defaultProps).toBe(typeof Mock.defaultProps); |
| 46 | + }); |
| 47 | + |
| 48 | + it('wraps components with Sentry wrappers', () => { |
| 49 | + const Wrapped = wrap(DummyComponent); |
| 50 | + const renderResult = render(<Wrapped value="wrapped" />); |
| 51 | + |
| 52 | + expect(renderResult.toJSON()).toMatchInlineSnapshot(` |
| 53 | + <div |
| 54 | + testID="touch-boundaryID" |
| 55 | + > |
| 56 | + <div |
| 57 | + testID="profilerID" |
| 58 | + > |
| 59 | + <div |
| 60 | + testID="feedback-widgetID" |
| 61 | + > |
| 62 | + <div> |
| 63 | + wrapped |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | +`); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('ReactNativeProfiler', () => { |
| 72 | + it('uses given options when set', () => { |
| 73 | + const options: ReactNativeWrapperOptions = { |
| 74 | + profilerProps: { disabled: false, includeRender: true, includeUpdates: true }, |
| 75 | + }; |
| 76 | + const Wrapped = wrap(DummyComponent, options); |
| 77 | + render(<Wrapped value="wrapped" />); |
| 78 | + |
| 79 | + expect(ReactNativeProfiler).toHaveBeenCalledWith( |
| 80 | + expect.objectContaining({ |
| 81 | + name: 'Root', |
| 82 | + disabled: false, |
| 83 | + includeRender: true, |
| 84 | + includeUpdates: true |
| 85 | + }), |
| 86 | + expect.anything(), |
| 87 | + ); |
| 88 | + |
| 89 | + expect(ReactNativeProfiler).not.toHaveBeenCalledWith( |
| 90 | + expect.objectContaining({ |
| 91 | + updateProps: expect.anything(), |
| 92 | + }) |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('ignore updateProps when set', () => { |
| 97 | + const { wrap } = jest.requireActual('../src/js/sdk'); |
| 98 | + |
| 99 | + const Wrapped = wrap(DummyComponent, { updateProps: ['prop'] }); |
| 100 | + render(<Wrapped value="wrapped" />); |
| 101 | + |
| 102 | + expect(ReactNativeProfiler).toHaveBeenCalledWith( |
| 103 | + expect.objectContaining({ |
| 104 | + name: 'Root', |
| 105 | + updateProps: {}, |
| 106 | + }), |
| 107 | + expect.anything(), |
| 108 | + ); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
0 commit comments