Skip to content

Commit

Permalink
💚 🐛 Fixing rollup configuration and useInterval cleanup function
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Russo committed Jul 16, 2020
1 parent 6b5ef3b commit eb78483
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,10 @@ online
### Fixed

- useStorage throws an error on server side rendering as the window object is not defined yet

## [0.27.2] - 2020-07-16

### Fixed

- useInterval clear function is now correctly used as useEffect cleanup
- Rollup configuration `preserveModules` bug
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beautiful-react-hooks",
"version": "0.27.1",
"version": "0.27.2",
"description": "A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development",
"main": "dist/index.js",
"module": "dist/esm/index.js",
Expand Down
9 changes: 6 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { version } from './package.json';
const name = 'beautiful-react-hooks';
const banner = `/* ${name} version: ${version} */`;

const standardOpts = {
name, banner, exports: 'named', minifyInternalExports: true, preserveModules: true,
};

// CommonJS (for Node) and ES module (for bundlers) build.
// (We could have three entries in the configuration array
// instead of two, but it's quicker to generate multiple
Expand All @@ -14,11 +18,10 @@ const banner = `/* ${name} version: ${version} */`;
// `file` and `format` for each target)
const config = [{
input: glob.sync('./src/**/*.js'),
preserveModules: true,
strictDeprecations: true,
output: [
{ name, banner, dir: 'dist', format: 'cjs', exports: 'named', minifyInternalExports: true },
{ name, banner, dir: 'dist/esm', format: 'esm', exports: 'named', minifyInternalExports: true },
{ ...standardOpts, dir: 'dist', format: 'cjs' },
{ ...standardOpts, dir: 'dist/esm', format: 'esm' },
],
external: ['react', 'react-dom', 'lodash.debounce', 'lodash.throttle'],
plugins: [
Expand Down
1 change: 0 additions & 1 deletion src/useDebouncedFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ const useDebouncedFn = (fn, wait = 100, options = defaultOptions, dependencies)
return useCallback(debounced, dependencies);
};


export default useDebouncedFn;
5 changes: 3 additions & 2 deletions src/useInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const useInterval = (fn, milliseconds, options = defaultOptions) => {
// the clear method
const clear = useCallback(() => {
if (timeout.current) {
clearInterval(timeout.current);
setIsCleared(true);
clearInterval(timeout.current);
}
}, []);

Expand All @@ -36,8 +36,9 @@ const useInterval = (fn, milliseconds, options = defaultOptions) => {
callback.current();
}, milliseconds);
}

// cleanup previous interval
return () => clear();
return clear;
}, [milliseconds]);

// when component unmount clear the timeout
Expand Down
42 changes: 14 additions & 28 deletions test/useInterval.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import React from 'react';
import { render, cleanup as cleanupReact } from '@testing-library/react';
import { cleanup as cleanupHooks, renderHook, act } from '@testing-library/react-hooks';
import { cleanup, renderHook, act } from '@testing-library/react-hooks';
import useInterval from '../dist/useInterval';
import promiseDelay from './utils/promiseDelay';

describe('useInterval', () => {
beforeEach(() => {
cleanupHooks();
cleanupReact();
});

afterEach(() => {
sinon.restore()
});
beforeEach(cleanup);
afterEach(sinon.restore);

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

it('should allow to define whether the interval should be cleared on unmount', async () => {
const ms = 50;
const spy = sinon.spy();

const TestComponent = () => {
useInterval(spy, ms, { cancelOnUnmount: false });
it('even if the provided options is null, it should keep working', () => {
const { result } = renderHook(() => useInterval(() => null, 1000, null));

return <div />;
};
expect(result.current).to.be.an('array');
});

const { rerender } = render(<TestComponent />);
rerender(null);

await promiseDelay(10 + ms);
/*it('should allow to define whether the interval should be cleared on unmount', async () => {
const noop = () => void 0;
const { result, unmount, wait } = renderHook(() => useInterval(noop, 10, { cancelOnUnmount: true }));
expect(spy.called).to.be.true;
});
await wait(() => true, { timeout: 250 });
it('even if the provided options is null, it should keep working', () => {
const { result } = renderHook(() => useInterval(() => null, 1000, null));
unmount();
expect(result.current).to.be.an('array');
});
expect(result.current[0]).to.be.true;
});*/

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

0 comments on commit eb78483

Please sign in to comment.