Skip to content

Commit

Permalink
Merge pull request #46 from 3846masa-tmp/fix/use-molecule-when-return…
Browse files Browse the repository at this point in the history
…s-function

Fixes #47
  • Loading branch information
loganvolkers authored Feb 8, 2024
2 parents 9b10b5a + c0cd958 commit ee69296
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/react/useMolecule.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export const UserMolecule = molecule((_, getScope) => {

strictModeSuite(({ wrapper, isStrict }) => {
describe("useMolecule", () => {
test("useMolecule returns a function returned by a molecule", () => {
const returnFunction = () => {};
const FunctionMolecule = molecule(() => {
return returnFunction;
});
const useFunctionMolecule = () => {
return {
molecule: useMolecule(FunctionMolecule),
};
};
const { result } = renderHook(useFunctionMolecule, {});

expect(result.current.molecule).toBe(returnFunction);
});

test("Use molecule can have scope provided", () => {
const useUserMolecule = () => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/react/useMolecule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export function useMolecule<T>(
...flattenTuples(inputTuples),
]);

const [mutableValue, setMutableValue] = useState(value);
const [mutableValue, setMutableValue] = useState(() => value);

useEffect(() => {
// console.log("==== useEffect! =====");
const subbedValue = handle.start();
setMutableValue(subbedValue);
setMutableValue(() => subbedValue);
return () => {
// console.log("==== CLEANUP useEffect! =====");
handle.stop();
Expand Down

0 comments on commit ee69296

Please sign in to comment.