Skip to content

Commit

Permalink
fix: parent may omit some update notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
awmleer committed Feb 11, 2022
1 parent 81fa4df commit b865438
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/__snapshots__/main.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ exports[`depending 2`] = `
</DocumentFragment>
`;

exports[`parent child 1`] = `
<DocumentFragment>
1
<div>
1
</div>
</DocumentFragment>
`;

exports[`setState timing 1`] = `
<DocumentFragment>
<div>
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/main.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,36 @@ test("depending", async () => {
await sleep(10);
expect(renderer.asFragment()).toMatchSnapshot();
});

test("parent child", async () => {
const useCounterModel = createModel(function A() {
const [count, setCount] = useState(0);
const increment = () => setCount(count + 1);

return {
count,
increment
};
});

const Child: FC = () => {
const counter = useCounterModel();
useEffect(() => {
counter.increment();
}, []);
return <div>{counter.count}</div>;
};

const App: FC = () => {
const counterModel = useCounterModel();
return (
<>
{counterModel.count}
<Child />
</>
);
};

const renderer = testing.render(<App />);
expect(renderer.asFragment()).toMatchSnapshot();
});
3 changes: 3 additions & 0 deletions src/create-model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export function createModel<T, P>(hook: ModelHook<T, P>, hookArg?: P) {
}
}
container.subscribers.add(subscriber);
if (container.data !== state) {
setState(container.data);
}
return () => {
container.subscribers.delete(subscriber);
};
Expand Down

0 comments on commit b865438

Please sign in to comment.