Skip to content

Commit

Permalink
test: Add tests for useEffect & useState w/ NaN
Browse files Browse the repository at this point in the history
Co-authored-by: jayrobin <[email protected]>
  • Loading branch information
rschristian and jayrobin committed Feb 13, 2025
1 parent 4f47033 commit a4491f6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions hooks/test/browser/useEffect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,4 +636,26 @@ describe('useEffect', () => {
expect(calls.length).to.equal(1);
expect(calls).to.deep.equal(['doing effecthi']);
});

it('should not rerun when receiving NaN on subsequent renders', () => {
const calls = [];
const Component = ({ value }) => {
const [count, setCount] = useState(0);
useEffect(() => {
calls.push('doing effect' + count);
setCount(count + 1);
return () => {
calls.push('cleaning up' + count);
};
}, [value]);
return <p>{count}</p>;
};
const App = () => <Component value={NaN} />;

act(() => {
render(<App />, scratch);
});
expect(calls.length).to.equal(1);
expect(calls).to.deep.equal(['doing effect0']);
});
});
26 changes: 26 additions & 0 deletions hooks/test/browser/useState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,32 @@ describe('useState', () => {
expect(scratch.innerHTML).to.equal('<p>hello world!!!</p>');
});

it('should limit rerenders when setting state to NaN', () => {
const calls = [];
const App = ({ i }) => {
calls.push('rendering' + i);
const [greeting, setGreeting] = useState(0);

if (i === 2) {
setGreeting(NaN);
}

return <p>{greeting}</p>;
};

act(() => {
render(<App i={1} />, scratch);
});
expect(calls.length).to.equal(1);
expect(calls).to.deep.equal(['rendering1']);

act(() => {
render(<App i={2} />, scratch);
});
expect(calls.length).to.equal(3);
expect(calls.slice(1).every(c => c === 'rendering2')).to.equal(true);
});

describe('Global sCU', () => {
let prevScu;
before(() => {
Expand Down

0 comments on commit a4491f6

Please sign in to comment.