Testing Hook Name Integration Workflow in Preact Devtools
This test suite validates the addHookName functionality in Preact’s devtools, focusing on how custom hook names are handled and integrated with the options API. The tests ensure proper behavior both with and without options hooks present.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
preactjs/preact
devtools/test/browser/addHookName.test.js
import { createElement, render, options } from 'preact';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { useState } from 'preact/hooks';
import { addHookName } from 'preact/devtools';
/** @jsx createElement */
describe('addHookName', () => {
/** @type {HTMLDivElement} */
let scratch;
beforeEach(() => {
scratch = setupScratch();
});
afterEach(() => {
teardown(scratch);
delete options._addHookName;
});
it('should do nothing when no options hook is present', () => {
function useFoo() {
return addHookName(useState(0), 'foo');
}
function App() {
let [v] = useFoo();
return <div>{v}</div>;
}
expect(() => render(<App />, scratch)).to.not.throw();
});
it('should call options hook with value', () => {
let spy = (options._addHookName = sinon.spy());
function useFoo() {
return addHookName(useState(0), 'foo');
}
function App() {
let [v] = useFoo();
return <div>{v}</div>;
}
render(<App />, scratch);
expect(spy).to.be.calledOnce;
expect(spy).to.be.calledWith('foo');
});
});