Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 10, 2025
1 parent 3d51c8b commit 17c20c9
Show file tree
Hide file tree
Showing 3 changed files with 5,435 additions and 4,404 deletions.
2 changes: 1 addition & 1 deletion packages/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"preact": "10.x"
},
"devDependencies": {
"preact": "10.9.0",
"preact": "10.25.4",
"preact-render-to-string": "^5.2.5"
},
"publishConfig": {
Expand Down
58 changes: 57 additions & 1 deletion packages/preact/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import type { ReadonlySignal } from "@preact/signals";
import { createElement, createRef, render, createContext } from "preact";
import type { ComponentChildren, FunctionComponent } from "preact";
import { useContext, useRef, useState } from "preact/hooks";
import { useContext, useEffect, useRef, useState } from "preact/hooks";
import { setupRerender, act } from "preact/test-utils";

const sleep = (ms?: number) => new Promise(r => setTimeout(r, ms));
Expand Down Expand Up @@ -829,4 +829,60 @@ describe("@preact/signals", () => {
expect(cleanup).to.have.been.calledOnceWith("foo", child);
});
});

it("Should take hooks-state settling in account", () => {
const renderSpy = sinon.spy();
const Context = createContext({
addModal: () => {},
removeModal: () => {},
});

function ModalProvider(props: any) {
let [modalCount, setModalCount] = useState(0);
renderSpy(modalCount);
let context = {
modalCount,
addModal() {
setModalCount(count => count + 1);
},
removeModal() {
setModalCount(count => count - 1);
},
};

return (
// @ts-expect-error
<Context.Provider value={context}>{props.children}</Context.Provider>
);
}

function useModal() {
let context = useContext(Context);
useEffect(() => {
context.addModal();
return () => {
context.removeModal();
};
}, [context]);
}

function Popover() {
useModal();
return <div>Popover</div>;
}

function App() {
return (
<ModalProvider>
<Popover />
</ModalProvider>
);
}

act(() => {
render(<App />, scratch);
});

expect(renderSpy).to.be.calledTwice;
});
});
Loading

0 comments on commit 17c20c9

Please sign in to comment.