Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug (tests WIP) - return function from selector #34

Merged
merged 1 commit into from Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions __tests__/reusable.test.js
Expand Up @@ -164,4 +164,40 @@ describe('reusable', () => {

expect(result.error.toString()).toMatchSnapshot();
});
it('should allow to return a function value', () => {
const callback = jest.fn();
const useSomething = reusable(() => callback);
const {result} = renderHook(useSomething, {
wrapper: ReusableProvider
});

expect(result.current).toBe(callback);
expect(callback.mock.calls.length).toBe(0);
});
});
describe('selectors', () => {

it('should allow to use a selector', () => {
const useSomething = reusable(() => useState(1));
const useSelector = () => useSomething(state => state[0]);
const { result } = renderHook(useSelector, {
wrapper: ReusableProvider
});
const state = result.current;

expect(state).toBe(1);
});

it('should allow to use a selector that returns a function', () => {
const callback = jest.fn();
const useSomething = reusable(() => ({ callback }));
const useSelector = () => useSomething(state => state.callback);
const {result} = renderHook(useSelector, {
wrapper: ReusableProvider
});

expect(result.current).toBe(callback);
expect(callback.mock.calls.length).toBe(0);
});

});
12 changes: 11 additions & 1 deletion sandbox/src/index.js
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useCallback } from "react";
import ReactDOM from "react-dom";
import { ReusableProvider, reusable } from "../../dist/reusable";

Expand All @@ -10,6 +10,14 @@ const useStep = reusable(() => {
return useState(0);
});

const useFnObject = reusable(() => {
const [state, setState] = useState(0);
const callback = () => console.log('callback') || setState(10);
return {
callback
};
});

const useMultiply = reusable(() => {
const [counter] = useCounter();
const [step] = useStep();
Expand Down Expand Up @@ -55,6 +63,8 @@ const Multiply = () => {

const App = () => {
const [showMultiply, setShowMultiply] = useState(true);
const callback = useFnObject(state => state.callback);
setTimeout(() => callback(), 1000);

return (
<div>
Expand Down
2 changes: 1 addition & 1 deletion src/react-reusable.js
Expand Up @@ -55,7 +55,7 @@ const useReuse = (fn, selector = identity, areEqual = shallowEqual) => {
return unit.subscribe((newValue) => {
const selectedNewValue = selector(newValue);
if (!areEqual(selectedNewValue, localCopy)) {
setLocalCopy(selectedNewValue);
setLocalCopy(() => selectedNewValue);
}
});
}, [unit, localCopy, selector, areEqual]);
Expand Down