Skip to content

Commit c376d8e

Browse files
committed
Fix test
1 parent e8dd9b8 commit c376d8e

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

hooks/src/index.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,6 @@ function getHookState(index, type) {
159159
return hooks._list[index];
160160
}
161161

162-
options._afterRender = (newVNode, oldVNode) => {
163-
if (newVNode._component && newVNode._component.__hooks) {
164-
const hooks = newVNode._component.__hooks._list;
165-
const stateHooksThatExecuted = hooks.filter(
166-
/** @type {(x: import('./internal').HookState) => x is import('./internal').ReducerHookState} */
167-
// @ts-expect-error
168-
x => x._component && x._didExecute
169-
);
170-
171-
if (
172-
stateHooksThatExecuted.length &&
173-
!stateHooksThatExecuted.some(x => x._didUpdate) &&
174-
oldVNode.props === newVNode.props
175-
) {
176-
newVNode._component.__hooks._pendingEffects = [];
177-
newVNode._flags |= SKIP_CHILDREN;
178-
}
179-
180-
stateHooksThatExecuted.some(hook => {
181-
hook._didExecute = hook._didUpdate = false;
182-
});
183-
}
184-
185-
if (oldAfterRender) oldAfterRender(newVNode, oldVNode);
186-
};
187-
188162
/**
189163
* @template {unknown} S
190164
* @param {import('./index').Dispatch<import('./index').StateUpdater<S>>} [initialState]

hooks/test/browser/useState.test.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { setupRerender, act } from 'preact/test-utils';
22
import { createElement, render, createContext, Component } from 'preact';
3-
import { vi } from 'vitest';
3+
import { afterAll, beforeAll, expect, vi } from 'vitest';
44
import {
55
useState,
66
useContext,
@@ -380,7 +380,7 @@ describe('useState', () => {
380380
expect(scratch.innerHTML).to.equal('<p>hello world!!!</p>');
381381
});
382382

383-
it('should limit rerenders when setting state to NaN', () => {
383+
it('should exhaust renders when NaN state is set as a result of a props update', () => {
384384
const calls = [];
385385
const App = ({ i }) => {
386386
calls.push('rendering' + i);
@@ -402,10 +402,57 @@ describe('useState', () => {
402402
act(() => {
403403
render(<App i={2} />, scratch);
404404
});
405-
expect(calls.length).to.equal(3);
405+
expect(calls.length).to.equal(27);
406406
expect(calls.slice(1).every(c => c === 'rendering2')).to.equal(true);
407407
});
408408

409+
it('should bail correctly when setting NaN twice', () => {
410+
const calls = [];
411+
let set;
412+
const Greeting = ({ greeting }) => {
413+
calls.push('rendering ' + greeting);
414+
return <p>{greeting}</p>;
415+
};
416+
const App = () => {
417+
const [greeting, setGreeting] = useState(0);
418+
set = setGreeting;
419+
420+
return <Greeting greeting={greeting} />;
421+
};
422+
423+
act(() => {
424+
render(<App />, scratch);
425+
});
426+
expect(calls.length).to.equal(1);
427+
expect(calls).to.deep.equal(['rendering 0']);
428+
429+
act(() => {
430+
set(1);
431+
});
432+
expect(calls.length).to.equal(2);
433+
expect(calls).to.deep.equal(['rendering 0', 'rendering 1']);
434+
435+
act(() => {
436+
set(NaN);
437+
});
438+
expect(calls.length).to.equal(3);
439+
expect(calls).to.deep.equal([
440+
'rendering 0',
441+
'rendering 1',
442+
'rendering NaN'
443+
]);
444+
445+
act(() => {
446+
set(NaN);
447+
});
448+
expect(calls.length).to.equal(3);
449+
expect(calls).to.deep.equal([
450+
'rendering 0',
451+
'rendering 1',
452+
'rendering NaN'
453+
]);
454+
});
455+
409456
describe('Global sCU', () => {
410457
let prevScu;
411458
beforeAll(() => {

0 commit comments

Comments
 (0)