From 0b0580cd82e6cb97e336fe94ab155459e0c3cd47 Mon Sep 17 00:00:00 2001 From: Mateus Moraes Date: Tue, 3 Aug 2021 16:21:38 -0300 Subject: [PATCH] Update README.md jest mock sample Change mock to allow render as component --- README.md | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 75920a8b2..e4fa1f522 100644 --- a/README.md +++ b/README.md @@ -217,10 +217,24 @@ export default function App() { ### How to mock the library for testing with [jest](https://jestjs.io)? ```js -jest.mock('react-native-toast-message', () => ({ - show: jest.fn(), - hide: jest.fn() -})); +jest.mock('react-native-toast-message', () => { + const RealComponent = jest.requireActual('react-native-toast-message'); + const ReactInternal = require('react'); + class Toast extends ReactInternal.Component { + static show = jest.fn(); + static hide = jest.fn(); + + render() { + return ReactInternal.createElement( + 'Toast', + this.props, + this.props.children, + ); + } + } + Toast.propTypes = RealComponent.propTypes; + return Toast; +}); ``` ### How to show the Toast inside a Modal?