Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hydrate): ensure beforeHydrateFn and afterHydrateFn always return a function #5890

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/hydrate/runner/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async function render(win: MockWindow, opts: HydrateFactoryOptions, results: Hyd
}

initializeWindow(win, win.document, opts, results);
const beforeHydrateFn = typeof opts.beforeHydrate === 'function' ? opts.beforeHydrate(win.document) : NOOP;
const beforeHydrateFn = typeof opts.beforeHydrate === 'function' ? opts.beforeHydrate : NOOP;
try {
await Promise.resolve(beforeHydrateFn(win.document));
return new Promise<HydrateResults>((resolve) => hydrateFactory(win, opts, results, afterHydrate, resolve));
Expand Down Expand Up @@ -162,7 +162,7 @@ async function afterHydrate(
results: HydrateResults,
resolve: (results: HydrateResults) => void,
) {
const afterHydrateFn = typeof opts.afterHydrate === 'function' ? opts.afterHydrate(win.document) : NOOP;
const afterHydrateFn = typeof opts.afterHydrate === 'function' ? opts.afterHydrate : NOOP;
try {
await Promise.resolve(afterHydrateFn(win.document));
return resolve(finalizeHydrate(win, win.document, opts, results));
Expand Down
14 changes: 14 additions & 0 deletions test/end-to-end/src/declarative-shadow-dom/test.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,18 @@ describe('renderToString', () => {
`<car-detail class=\"sc-car-list\" custom-hydrate-flag=\"\" c-id=\"2.4.2.0\" s-id=\"4\"><!--r.4--><section class=\"sc-car-list\" c-id=\"4.0.0.0\"><!--t.4.1.1.0-->2023 VW Beetle</section></car-detail>`,
);
});

it('calls beforeHydrate and afterHydrate function hooks', async () => {
const beforeHydrate = jest.fn((doc) => (doc.querySelector('div').textContent = 'Hello Universe'));
const afterHydrate = jest.fn();

const { html } = await renderToString('<div>Hello World</div>', {
beforeHydrate,
afterHydrate,
});

expect(beforeHydrate).toHaveBeenCalledTimes(1);
expect(afterHydrate).toHaveBeenCalledTimes(1);
expect(html).toContain('<body><div>Hello Universe</div></body>');
});
});