Validating Hydration Compatibility Layer in PreactJS
This test suite validates the hydration functionality in Preact’s compatibility layer, focusing on React-style JSX rendering and callback handling. The tests ensure proper DOM element preservation and event handling during hydration.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
preactjs/preact
compat/test/browser/hydrate.test.js
import React, { hydrate } from 'preact/compat';
import { setupScratch, teardown } from '../../../test/_util/helpers';
describe('compat hydrate', () => {
/** @type {HTMLDivElement} */
let scratch;
beforeEach(() => {
scratch = setupScratch();
});
afterEach(() => {
teardown(scratch);
});
it('should render react-style jsx', () => {
const input = document.createElement('input');
scratch.appendChild(input);
input.focus();
expect(document.activeElement).to.equalNode(input);
hydrate(<input />, scratch);
expect(document.activeElement).to.equalNode(input);
});
it('should call the callback', () => {
scratch.innerHTML = '<div></div>';
let spy = sinon.spy();
hydrate(<div />, scratch, spy);
expect(spy).to.be.calledOnce;
expect(spy).to.be.calledWithExactly();
});
});