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

feat: implement createSVG to render react-plot svg in server-side #459

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ extends:
rules:
'@typescript-eslint/no-dynamic-delete': off
'import/namespace': off
'jest/no-standalone-expect': off
9 changes: 9 additions & 0 deletions src/createSVG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createElement } from 'react';
import { renderToString } from 'react-dom/server';

import { PlotObject, PlotObjectProps } from './components/PlotObject';

export function createSVG(param: PlotObjectProps) {
const plot = createElement(PlotObject, param);
return renderToString(plot);
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ export {
usePlotControls,
usePlotEvents,
} from './contexts/plotController/plotControllerContext';

export * from './createSVG';
16 changes: 12 additions & 4 deletions tests/serverside.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { test, expect } from '@playwright/experimental-ct-react';
import { ServerSide } from './utils';

import { InfraredPlotTest, ServerSide } from './utils';

test('should render a plot in server-side mode', async ({ mount }) => {
const plot = await mount(<ServerSide />);
const html = await plot.innerHTML();
expect(html).toContain('svg');
const serverPlot = await mount(<ServerSide />);
const clientPlot = await mount(<InfraredPlotTest />);
const ServerHtml = await serverPlot.innerHTML();
const ClientHtml = await clientPlot.innerHTML();
expect(ServerHtml).toBe(ClientHtml);

// compare screen shots
const serverImage = await serverPlot.screenshot();
const clientImage = await clientPlot.screenshot();
expect(serverImage).toStrictEqual(clientImage);
});
46 changes: 41 additions & 5 deletions tests/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { ReactNode, useState } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { ReactNode, useState, useEffect } from 'react';
import { hydrate } from 'react-dom';

import { Annotations, Plot, ScatterSeries } from '../src';
import {
PlotObject,
PlotObjectPlot,
Annotations,
createSVG,
Plot,
ScatterSeries,
} from '../src';
import { Arrow } from '../src/components/Annotations/Arrow';
import { DirectedEllipse } from '../src/components/Annotations/DirectedEllipse';
import infrared from '../stories/data/infrared.json';
Expand All @@ -14,9 +21,38 @@ interface ChildrenProps {
export function DefaultPlotTest({ children }: ChildrenProps) {
return <Plot {...DEFAULT_PLOT_CONFIG}>{children}</Plot>;
}
const plot: PlotObjectPlot = {
dimensions: DEFAULT_PLOT_CONFIG,
axes: [
{
type: 'main',
position: 'bottom',
},
{
type: 'main',
position: 'left',
},
],
content: [
{
type: 'line',
data: infrared,
},
],
};
export function ServerSide() {
const html = renderToStaticMarkup(<InfraredPlotTest />);
return <div dangerouslySetInnerHTML={{ __html: html }} />;
const [result, setResult] = useState<any>(null);

useEffect(() => {
const html = createSVG({ plot });
const rootElement = document.querySelector('root');
if (rootElement === null) throw new Error('Root element not found');
rootElement.innerHTML = html;
const result = hydrate(<PlotObject plot={plot} />, rootElement);
setResult(result);
}, []);

return result;
}
export function InfraredPlotTest({ children }: ChildrenProps) {
return (
Expand Down