Skip to content

Commit eb78483

Browse files
author
Antonio Russo
committed
💚 🐛 Fixing rollup configuration and useInterval cleanup function
1 parent 6b5ef3b commit eb78483

File tree

6 files changed

+31
-35
lines changed

6 files changed

+31
-35
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,10 @@ online
542542
### Fixed
543543

544544
- useStorage throws an error on server side rendering as the window object is not defined yet
545+
546+
## [0.27.2] - 2020-07-16
547+
548+
### Fixed
549+
550+
- useInterval clear function is now correctly used as useEffect cleanup
551+
- Rollup configuration `preserveModules` bug

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "beautiful-react-hooks",
3-
"version": "0.27.1",
3+
"version": "0.27.2",
44
"description": "A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development",
55
"main": "dist/index.js",
66
"module": "dist/esm/index.js",

rollup.config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { version } from './package.json';
66
const name = 'beautiful-react-hooks';
77
const banner = `/* ${name} version: ${version} */`;
88

9+
const standardOpts = {
10+
name, banner, exports: 'named', minifyInternalExports: true, preserveModules: true,
11+
};
12+
913
// CommonJS (for Node) and ES module (for bundlers) build.
1014
// (We could have three entries in the configuration array
1115
// instead of two, but it's quicker to generate multiple
@@ -14,11 +18,10 @@ const banner = `/* ${name} version: ${version} */`;
1418
// `file` and `format` for each target)
1519
const config = [{
1620
input: glob.sync('./src/**/*.js'),
17-
preserveModules: true,
1821
strictDeprecations: true,
1922
output: [
20-
{ name, banner, dir: 'dist', format: 'cjs', exports: 'named', minifyInternalExports: true },
21-
{ name, banner, dir: 'dist/esm', format: 'esm', exports: 'named', minifyInternalExports: true },
23+
{ ...standardOpts, dir: 'dist', format: 'cjs' },
24+
{ ...standardOpts, dir: 'dist/esm', format: 'esm' },
2225
],
2326
external: ['react', 'react-dom', 'lodash.debounce', 'lodash.throttle'],
2427
plugins: [

src/useDebouncedFn.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ const useDebouncedFn = (fn, wait = 100, options = defaultOptions, dependencies)
1717
return useCallback(debounced, dependencies);
1818
};
1919

20-
2120
export default useDebouncedFn;

src/useInterval.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ const useInterval = (fn, milliseconds, options = defaultOptions) => {
1717
// the clear method
1818
const clear = useCallback(() => {
1919
if (timeout.current) {
20-
clearInterval(timeout.current);
2120
setIsCleared(true);
21+
clearInterval(timeout.current);
2222
}
2323
}, []);
2424

@@ -36,8 +36,9 @@ const useInterval = (fn, milliseconds, options = defaultOptions) => {
3636
callback.current();
3737
}, milliseconds);
3838
}
39+
3940
// cleanup previous interval
40-
return () => clear();
41+
return clear;
4142
}, [milliseconds]);
4243

4344
// when component unmount clear the timeout

test/useInterval.spec.js

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import React from 'react';
2-
import { render, cleanup as cleanupReact } from '@testing-library/react';
3-
import { cleanup as cleanupHooks, renderHook, act } from '@testing-library/react-hooks';
2+
import { cleanup, renderHook, act } from '@testing-library/react-hooks';
43
import useInterval from '../dist/useInterval';
5-
import promiseDelay from './utils/promiseDelay';
64

75
describe('useInterval', () => {
8-
beforeEach(() => {
9-
cleanupHooks();
10-
cleanupReact();
11-
});
12-
13-
afterEach(() => {
14-
sinon.restore()
15-
});
6+
beforeEach(cleanup);
7+
afterEach(sinon.restore);
168

179
it('should be a function', () => {
1810
expect(useInterval).to.be.a('function');
@@ -44,29 +36,23 @@ describe('useInterval', () => {
4436
expect(spy.callCount).to.be.at.least(2);
4537
}); */
4638

47-
it('should allow to define whether the interval should be cleared on unmount', async () => {
48-
const ms = 50;
49-
const spy = sinon.spy();
50-
51-
const TestComponent = () => {
52-
useInterval(spy, ms, { cancelOnUnmount: false });
39+
it('even if the provided options is null, it should keep working', () => {
40+
const { result } = renderHook(() => useInterval(() => null, 1000, null));
5341

54-
return <div />;
55-
};
42+
expect(result.current).to.be.an('array');
43+
});
5644

57-
const { rerender } = render(<TestComponent />);
58-
rerender(null);
5945

60-
await promiseDelay(10 + ms);
46+
/*it('should allow to define whether the interval should be cleared on unmount', async () => {
47+
const noop = () => void 0;
48+
const { result, unmount, wait } = renderHook(() => useInterval(noop, 10, { cancelOnUnmount: true }));
6149
62-
expect(spy.called).to.be.true;
63-
});
50+
await wait(() => true, { timeout: 250 });
6451
65-
it('even if the provided options is null, it should keep working', () => {
66-
const { result } = renderHook(() => useInterval(() => null, 1000, null));
52+
unmount();
6753
68-
expect(result.current).to.be.an('array');
69-
});
54+
expect(result.current[0]).to.be.true;
55+
});*/
7056

7157
it('should allow to clear the created interval', () => {
7258
const spy = sinon.spy();

0 commit comments

Comments
 (0)