Skip to content

Commit

Permalink
chore: Correct some internal test types (#4588)
Browse files Browse the repository at this point in the history
* chore: Try to reduce type errors in test files

* chore: Clean up remaining `_nextDom` references

* chore: Low-hanging TS errors in tests

* chore: Even more types
  • Loading branch information
rschristian authored Nov 30, 2024
1 parent 65b4f6b commit f7ac8dc
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 63 deletions.
1 change: 1 addition & 0 deletions compat/test/browser/PureComponent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('PureComponent', () => {
it('should ignore the __source variable', () => {
const pureSpy = sinon.spy();
const appSpy = sinon.spy();
/** @type {(v) => void} */
let set;
class Pure extends React.PureComponent {
render() {
Expand Down
3 changes: 2 additions & 1 deletion compat/test/browser/cloneElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('compat cloneElement', () => {

it('should skip cloning on invalid element', () => {
let element = { foo: 42 };
// @ts-expect-error
let clone = cloneElement(element);
expect(clone).to.eql(element);
});
Expand All @@ -89,7 +90,7 @@ describe('compat cloneElement', () => {
return <div>{props.value}</div>;
}

let clone = cloneElement(preactH(Foo), { value: 'foo' });
let clone = cloneElement(preactH(Foo, {}), { value: 'foo' });
render(clone, scratch);
expect(scratch.textContent).to.equal('foo');
});
Expand Down
1 change: 1 addition & 0 deletions compat/test/browser/componentDidCatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('componentDidCatch', () => {
}
render() {
if (this.state.error) return <div />;
// @ts-expect-error
if (this.state.i === 0) return <ThrowErr />;
return null;
}
Expand Down
1 change: 1 addition & 0 deletions compat/test/browser/createElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('compat createElement()', () => {
});

it('should not normalize text nodes', () => {
// @ts-expect-error
String.prototype.capFLetter = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
};
Expand Down
1 change: 1 addition & 0 deletions compat/test/browser/forwardRef.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ describe('forwardRef', () => {
});

it('should support useImperativeHandle', () => {
/** @type {(v) => void} */
let setValue;
const Foo = forwardRef((props, ref) => {
const result = useState('');
Expand Down
1 change: 1 addition & 0 deletions compat/test/browser/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('React-18-hooks', () => {
it('runs transitions', () => {
const spy = sinon.spy();

/** @type {(v) => void} */
let go;
const App = () => {
const [isPending, start] = useTransition();
Expand Down
4 changes: 2 additions & 2 deletions compat/test/browser/isValidElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ describe('isValidElement', () => {
});

it('should detect a preact vnode', () => {
expect(isValidElement(preactCreateElement('div'))).to.equal(true);
expect(isValidElement(preactCreateElement('div', {}))).to.equal(true);
});

it('should detect a compat vnode', () => {
expect(isValidElement(React.createElement('div'))).to.equal(true);
expect(isValidElement(React.createElement('div', {}))).to.equal(true);
});
});
3 changes: 3 additions & 0 deletions compat/test/browser/memo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('memo()', () => {

let Memoized = memo(Foo);

/** @type {() => void} */
let update;
class App extends Component {
constructor() {
Expand Down Expand Up @@ -78,6 +79,7 @@ describe('memo()', () => {

let Memoized = memo(Foo);

/** @type {(v) => void} */
let update;
class App extends Component {
constructor() {
Expand Down Expand Up @@ -111,6 +113,7 @@ describe('memo()', () => {
let spy = sinon.spy(() => true);
let Memoized = memo(Foo, spy);

/** @type {(v) => void} */
let update;
class App extends Component {
constructor() {
Expand Down
29 changes: 25 additions & 4 deletions compat/test/browser/portals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Portal', () => {
});

it('should insert the portal', () => {
/** @type {() => void} */
let setFalse;
function Foo(props) {
const [mounted, setMounted] = useState(true);
Expand All @@ -66,6 +67,7 @@ describe('Portal', () => {
});

it('should order portal children well', () => {
/** @type {() => void} */
let bump;

function Modal() {
Expand Down Expand Up @@ -95,6 +97,7 @@ describe('Portal', () => {
});

it('should toggle the portal', () => {
/** @type {() => void} */
let toggle;

function Foo(props) {
Expand Down Expand Up @@ -130,6 +133,7 @@ describe('Portal', () => {
});

it('should notice prop changes on the portal', () => {
/** @type {(c) => void} */
let set;

function Foo(props) {
Expand All @@ -155,6 +159,7 @@ describe('Portal', () => {

it('should not unmount the portal component', () => {
let spy = sinon.spy();
/** @type {(c) => void} */
let set;
class Child extends Component {
componentWillUnmount() {
Expand Down Expand Up @@ -230,7 +235,10 @@ describe('Portal', () => {
});

it('should leave a working root after the portal', () => {
let toggle, toggle2;
/** @type {() => void} */
let toggle,
/** @type {() => void} */
toggle2;

function Foo(props) {
const [mounted, setMounted] = useState(false);
Expand Down Expand Up @@ -279,7 +287,10 @@ describe('Portal', () => {
});

it('should work with stacking portals', () => {
let toggle, toggle2;
/** @type {() => void} */
let toggle,
/** @type {() => void} */
toggle2;

function Foo(props) {
const [mounted, setMounted] = useState(false);
Expand Down Expand Up @@ -327,6 +338,7 @@ describe('Portal', () => {
});

it('should work with changing the container', () => {
/** @type {(c) => void} */
let set, ref;

function Foo(props) {
Expand Down Expand Up @@ -363,7 +375,10 @@ describe('Portal', () => {
});

it('should work with replacing placeholder portals', () => {
let toggle, toggle2;
/** @type {() => void} */
let toggle,
/** @type {() => void} */
toggle2;

function Foo(props) {
const [mounted, setMounted] = useState(false);
Expand Down Expand Up @@ -409,6 +424,7 @@ describe('Portal', () => {
});

it('should work with removing an element from stacked container to new one', () => {
/** @type {() => void} */
let toggle, root2;

function Foo(props) {
Expand Down Expand Up @@ -445,7 +461,10 @@ describe('Portal', () => {
});

it('should support nested portals', () => {
let toggle, toggle2, inner;
/** @type {() => void} */
let toggle,
/** @type {() => void} */
toggle2, inner;

function Bar() {
const [mounted, setMounted] = useState(false);
Expand Down Expand Up @@ -598,6 +617,7 @@ describe('Portal', () => {
});

it('should switch between non portal and portal node (Modal as lastChild)', () => {
/** @type {() => void} */
let toggle;
const Modal = ({ children, open }) =>
open ? createPortal(<div>{children}</div>, scratch) : <div>Closed</div>;
Expand Down Expand Up @@ -627,6 +647,7 @@ describe('Portal', () => {
});

it('should switch between non portal and portal node (Modal as firstChild)', () => {
/** @type {() => void} */
let toggle;
const Modal = ({ children, open }) =>
open ? createPortal(<div>{children}</div>, scratch) : <div>Closed</div>;
Expand Down
1 change: 1 addition & 0 deletions compat/test/browser/suspense-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ describe('suspense-list', () => {
const ComponentA = getSuspendableComponent('A');
const ComponentB = getSuspendableComponent('B');

/** @type {(v) => void} */
let showB;
function Container() {
const [showHidden, setShowHidden] = useState(false);
Expand Down
14 changes: 13 additions & 1 deletion compat/test/browser/suspense.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ describe('suspense', () => {
});

it('should reset hooks of components', () => {
/** @type {(v) => void} */
let set;
const LazyComp = ({ name }) => <div>Hello from {name}</div>;

Expand Down Expand Up @@ -155,6 +156,7 @@ describe('suspense', () => {
});

it('should call effect cleanups', () => {
/** @type {(v) => void} */
let set;
const effectSpy = sinon.spy();
const layoutEffectSpy = sinon.spy();
Expand Down Expand Up @@ -1374,6 +1376,7 @@ describe('suspense', () => {
it('should un-suspend when suspender unmounts', () => {
const [Suspender, suspend] = createSuspender(() => <div>Suspender</div>);

/** @type {() => void} */
let hide;

class Conditional extends Component {
Expand Down Expand Up @@ -1427,6 +1430,7 @@ describe('suspense', () => {
<div>Suspender 2</div>
));

/** @type {() => void} */
let hide, resolve;

class Conditional extends Component {
Expand Down Expand Up @@ -1495,7 +1499,10 @@ describe('suspense', () => {
return <div>{`Lazy ${value}`}</div>;
}

let hide, setValue;
/** @type {() => void} */
let hide,
/** @type {(v) => void} */
setValue;

class Conditional extends Component {
constructor(props) {
Expand Down Expand Up @@ -1560,6 +1567,7 @@ describe('suspense', () => {
it('should allow resolve suspense promise after unmounts', async () => {
const [Suspender, suspend] = createSuspender(() => <div>Suspender</div>);

/** @type {() => void} */
let hide, resolve;

class Conditional extends Component {
Expand Down Expand Up @@ -1607,6 +1615,7 @@ describe('suspense', () => {
it('should support updating state while suspended', async () => {
const [Suspender, suspend] = createSuspender(() => <div>Suspender</div>);

/** @type {() => void} */
let increment;

class Updater extends Component {
Expand Down Expand Up @@ -1672,6 +1681,7 @@ describe('suspense', () => {

Suspender.prototype.componentWillUnmount = cWUSpy;

/** @type {() => void} */
let hide;

let suspender = null;
Expand Down Expand Up @@ -1767,6 +1777,7 @@ describe('suspense', () => {
}
}

/** @type {Suspender} */
let suspender;
class Suspender extends Component {
constructor(props) {
Expand Down Expand Up @@ -1893,6 +1904,7 @@ describe('suspense', () => {
return content;
}

/** @type {Component} */
let parent;
class Parent extends Component {
constructor(props) {
Expand Down
1 change: 1 addition & 0 deletions compat/test/browser/unstable_batchedUpdates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('unstable_batchedUpdates', () => {

it('should call callback with only one arg', () => {
const spy = sinon.spy();
// @ts-expect-error
unstable_batchedUpdates(spy, 'foo', 'bar');
expect(spy).to.be.calledWithExactly('foo');
});
Expand Down
1 change: 0 additions & 1 deletion mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"$_args": "__H",
"$_factory": "__h",
"$_depth": "__b",
"$_nextDom": "__d",
"$_dirty": "__d",
"$_mask": "__m",
"$_detachOnNextRender": "__b",
Expand Down
23 changes: 0 additions & 23 deletions test/_util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,6 @@ export function clearOptions() {
export function teardown(scratch) {
if (!document.contains(scratch)) return;

if (
scratch &&
('__k' in scratch || '_children' in scratch) &&
scratch._children
) {
verifyVNodeTree(scratch._children);
}

if (scratch) {
scratch.parentNode.removeChild(scratch);
}
Expand All @@ -236,21 +228,6 @@ export function teardown(scratch) {
restoreElementAttributes();
}

/** @type {(vnode: import('../../src/internal').VNode) => void} */
function verifyVNodeTree(vnode) {
if (vnode._nextDom) {
expect.fail('vnode should not have _nextDom:' + vnode._nextDom);
}

if (vnode._children) {
for (let child of vnode._children) {
if (child) {
verifyVNodeTree(child);
}
}
}
}

const Foo = () => 'd';
export const getMixedArray = () =>
// Make it a function so each test gets a new copy of the array
Expand Down
4 changes: 2 additions & 2 deletions test/_util/logCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export function logCall(obj, method) {
if (args[1] === null && args.length === 2) {
operation = `${serialize(this)}.appendChild(${serialize(args[0])})`;
} else {
operation = `${serialize(this)}.${method}(${c})`;
operation = `${serialize(this)}.${String(method)}(${c})`;
}
break;
}
default: {
operation = `${serialize(this)}.${method}(${c})`;
operation = `${serialize(this)}.${String(method)}(${c})`;
break;
}
}
Expand Down
Loading

0 comments on commit f7ac8dc

Please sign in to comment.