From 64fc88ac2a4ebd5e55ca76c17c2277c6092b4025 Mon Sep 17 00:00:00 2001 From: James Pike Date: Wed, 24 Jul 2019 10:19:17 +0100 Subject: [PATCH] [Shallow] Add tests for effects with inputs and cleanup (#15275) --- .../ReactShallowRendererHooks-test.js | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js b/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js index c4f7da88182cb..455b1cbe3294d 100644 --- a/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js +++ b/packages/react-test-renderer/src/__tests__/ReactShallowRendererHooks-test.js @@ -280,6 +280,81 @@ describe('ReactShallowRenderer with hooks', () => { 'call effect', ]); }); + + it('should trigger effects and cleanup depending on inputs', () => { + let _setFriend; + const happenings = []; + + function SomeComponent() { + const [friend, setFriend] = React.useState('Bons'); + const [cat] = React.useState('Muskus'); + _setFriend = setFriend; + + React.useEffect( + () => { + happenings.push('call friend effect'); + return () => { + happenings.push('cleanup friend effect'); + }; + }, + [friend], + ); + + React.useEffect(() => { + happenings.push('call empty effect'); + return () => { + happenings.push('cleanup empty effect'); + }; + }); + + React.useEffect( + () => { + happenings.push('call cat effect'); + return () => { + happenings.push('cleanup cat effect'); + }; + }, + [cat], + ); + + React.useEffect( + () => { + happenings.push('call both effect'); + return () => { + happenings.push('cleanup both effect'); + }; + }, + [friend, cat], + ); + + return ( +
+ Hello {friend} with {cat} +
+ ); + } + + const shallowRenderer = createRenderer({callEffects: true}); + shallowRenderer.render(); + + expect(happenings).toEqual([ + 'call friend effect', + 'call empty effect', + 'call cat effect', + 'call both effect', + ]); + + happenings.splice(0); + _setFriend('Maryam'); + expect(happenings).toEqual([ + 'cleanup friend effect', + 'call friend effect', + 'cleanup empty effect', + 'call empty effect', + 'cleanup both effect', + 'call both effect', + ]); + }); }); it('should work with useRef', () => {