/
StringRenderer.ts
46 lines (37 loc) · 1.19 KB
/
StringRenderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { Renderer, RSTNode } from 'enzyme';
import type { ReactElement } from 'react';
import { h, render } from 'preact';
import type { EventDetails } from './MountRenderer';
import type { PreactAdapterOptions } from './Adapter';
export default class StringRenderer implements Renderer {
private _options: PreactAdapterOptions;
constructor(options: PreactAdapterOptions) {
this._options = options;
}
render(el: ReactElement, context?: any) {
if (this._options.renderToString) {
return this._options.renderToString(el, context);
} else {
const tempContainer = document.createElement('div');
render(el as any, tempContainer);
const html = tempContainer.innerHTML;
render(h('unmount-me', {}), tempContainer);
return html;
}
}
simulateError(nodeHierarchy: RSTNode[], rootNode: RSTNode, error: any) {
throw new Error('Static rendering does not support simulating errors');
}
simulateEvent(node: RSTNode, eventName: string, args: EventDetails) {
throw new Error('Static rendering does not support simulating events');
}
unmount() {
// No-op
}
getNode() {
return null;
}
batchedUpdates(fn: () => any) {
fn();
}
}