From 10f05cca648c91f3ffbcea50b32c31905e3dad08 Mon Sep 17 00:00:00 2001 From: gaeaehrlich Date: Mon, 3 May 2021 14:33:40 +0300 Subject: [PATCH] fix(#7): Upgraded to React 17, including fix to Scrollable and effect cleanup timing --- package.json | 2 +- src/hooks/useDebounce/useDebounce.js | 5 +---- src/hooks/useDebounce/useDebounce.test.js | 14 ++------------ src/hooks/useThrottle/useThrottle.js | 7 +------ src/hooks/useThrottle/useThrottle.test.js | 14 ++------------ 5 files changed, 7 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index 97fcde9..48df8d1 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@rollup/plugin-babel": "5.2.1", "@rollup/plugin-commonjs": "16.0.0", "@rollup/plugin-node-resolve": "10.0.0", - "@wojtekmaj/enzyme-adapter-react-17": "^0.6.1", + "@wojtekmaj/enzyme-adapter-react-17": "0.6.1", "babel-eslint": "10.1.0", "babel-loader": "8.2.2", "babel-plugin-module-resolver": "4.0.0", diff --git a/src/hooks/useDebounce/useDebounce.js b/src/hooks/useDebounce/useDebounce.js index c7ab498..cb71f9e 100644 --- a/src/hooks/useDebounce/useDebounce.js +++ b/src/hooks/useDebounce/useDebounce.js @@ -24,10 +24,7 @@ export default (callback, wait) => { useEffect(() => {cb.current = callback}, [callback]); // Cleanup pending timeouts when unmounting. - useEffect(() => { - const _timeout = timeout.current; - return () => clearTimeout(_timeout); - }, []); + useEffect(() => () => clearTimeout(timeout.curren), []); return useCallback((...args) => { clearTimeout(timeout.current); diff --git a/src/hooks/useDebounce/useDebounce.test.js b/src/hooks/useDebounce/useDebounce.test.js index d949843..5e5d88f 100644 --- a/src/hooks/useDebounce/useDebounce.test.js +++ b/src/hooks/useDebounce/useDebounce.test.js @@ -18,18 +18,6 @@ const Elem = () => { }; describe('useDebounce()', () => { - let spy; - beforeEach(() => { - spy = sinon.spy(global, 'clearTimeout') - }); - afterEach(() => { - spy.resetHistory(); - spy.restore(); - }); - after(() => { - spy.restore(); - }); - it('Should delay calls', async () => { let wrapper = null; act(() => {wrapper = mount()}); @@ -52,8 +40,10 @@ describe('useDebounce()', () => { }); it('Should cleanup', async () => { let wrapper = null; + const spy = sinon.spy(global, 'clearTimeout'); act(() => {wrapper = mount()}); act(() => {wrapper.unmount()}); expect(spy.callCount).to.eql(1); + spy.restore(); }); }); \ No newline at end of file diff --git a/src/hooks/useThrottle/useThrottle.js b/src/hooks/useThrottle/useThrottle.js index 9f1e595..888d4c8 100644 --- a/src/hooks/useThrottle/useThrottle.js +++ b/src/hooks/useThrottle/useThrottle.js @@ -20,12 +20,7 @@ export default (callback, threshold) => { const wait = useRef(false); const timeout = useRef(-1); - useEffect(() => { - const _timeout = timeout.current; - return () => { - clearTimeout(_timeout); - } - }, []); // No need for deps here since 'timeout' is mutated + useEffect(() => () => clearTimeout(timeout.current), []); // No need for deps here since 'timeout' is mutated return useCallback((...args) => { if (!wait.current) { diff --git a/src/hooks/useThrottle/useThrottle.test.js b/src/hooks/useThrottle/useThrottle.test.js index 60e8760..7c64e15 100644 --- a/src/hooks/useThrottle/useThrottle.test.js +++ b/src/hooks/useThrottle/useThrottle.test.js @@ -19,18 +19,6 @@ const Elem = () => { }; describe('useThrottle()', () => { - let spy; - beforeEach(() => { - spy = sinon.spy(global, 'clearTimeout') - }); - afterEach(() => { - spy.resetHistory(); - spy.restore(); - }); - after(() => { - spy.restore(); - }); - it('Should delay calls', async () => { let wrapper = null; act(() => {wrapper = mount()}); @@ -53,8 +41,10 @@ describe('useThrottle()', () => { }); it('Should cleanup', async () => { let wrapper = null; + const spy = sinon.spy(global, 'clearTimeout'); act(() => {wrapper = mount()}); act(() => {wrapper.unmount()}); expect(spy.callCount).to.eql(1); + spy.restore(); }); }); \ No newline at end of file