Skip to content
This repository was archived by the owner on Mar 17, 2024. It is now read-only.

Commit da58581

Browse files
authored
Merge pull request #69 from ts-graphviz/fix-web-module-api
Fix Graphviz component api
2 parents 418894e + 333c846 commit da58581

File tree

18 files changed

+247
-53
lines changed

18 files changed

+247
-53
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
lib

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Temporary Items
131131
# End of https://www.gitignore.io/api/macos
132132

133133
# Output Modules
134-
core
135-
web
134+
lib
136135
docs
136+
!src/**/*
137137
*.d.ts

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
lib

README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @ts-graphviz/react
22

3-
Graphviz-dot Renderer for React.
3+
Graphviz-dot Renderer using React.
44

55
## Installation
66

@@ -25,20 +25,16 @@ $ npm install @ts-graphviz/react
2525
# Peer Dependencies
2626
$ yarn add react ts-graphviz
2727
# Optional Peer Dependencies
28-
$ yarn add @hpcc-js/wasm
28+
$ yarn add @hpcc-js/wasm react-dom
2929
```
3030

31-
## API
32-
33-
### Core Module
31+
## DEMO
3432

35-
The core is designed to be independent of the execution environment.
33+
- [Storybook](https://ts-graphviz.github.io/react/)
3634

37-
It is designed to work in both browsers and Node.js.
38-
39-
It provides core components and hooks of `@ts-graphviz/react` and render functions.
35+
## API
4036

41-
#### Script
37+
### Script
4238

4339
```tsx
4440
import React, { FC } from 'react';
@@ -81,7 +77,7 @@ const dot = renderToDot(<Example />);
8177
console.log(dot);
8278
```
8379

84-
#### Output dot
80+
### Output dot
8581

8682
```dot
8783
digraph {
@@ -113,9 +109,9 @@ digraph {
113109

114110
![dot](./example/example.svg)
115111

116-
### Web Module
112+
### Web
117113

118-
The `Graphviz` component of `@ts-graphviz/react/web` can be rendered directly in the browser.
114+
The `Graphviz` component can be rendered directly in the browser.
119115

120116
Since this component uses the function of `@hpcc-js/wasm` internally, it is necessary to host `@hpcc-js/wasm/dist/graphviz.wasm` and specify its directory with `wasmFolder`.
121117

@@ -124,8 +120,7 @@ For development, I recommend using the one hosted by unpkg.
124120
```tsx
125121
import React, { FC } from 'react';
126122
import ReactDOM from 'react-dom';
127-
import { Digraph, Node, Edge } from '@ts-graphviz/react';
128-
import { Graphviz } from '@ts-graphviz/react/web';
123+
import { Graphviz, Digraph, Node, Edge } from '@ts-graphviz/react';
129124
import { wasmFolder } from '@hpcc-js/wasm';
130125

131126
wasmFolder('https://unpkg.com/@hpcc-js/wasm/dist/');

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
preset: 'ts-jest',
3-
testEnvironment: 'node',
3+
testEnvironment: 'jsdom',
44
verbose: true,
55
collectCoverage: true,
66
setupFilesAfterEnv: ['<rootDir>/config/jest/setup-jest.ts'],

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"publishConfig": {
2424
"access": "public"
2525
},
26-
"main": "core/index.js",
27-
"module": "core/index.mjs",
28-
"types": "core/index.d.ts",
26+
"main": "lib/index.js",
27+
"module": "lib/index.mjs",
28+
"types": "lib/index.d.ts",
2929
"scripts": {
3030
"example": "ts-node example/example",
3131
"build": "ts-node rollup.build",
@@ -55,6 +55,8 @@
5555
"@storybook/addon-knobs": "^5.3.18",
5656
"@storybook/addon-storysource": "^5.3.18",
5757
"@storybook/react": "^5.3.18",
58+
"@testing-library/jest-dom": "^5.5.0",
59+
"@testing-library/react": "^10.0.4",
5860
"@testing-library/react-hooks": "^3.2.1",
5961
"@types/fs-extra": "^8.1.0",
6062
"@types/prop-types": "^15.7.3",

rollup.build.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { rollup } from 'rollup';
22
import typescript from 'rollup-plugin-typescript2';
33
import { terser } from 'rollup-plugin-terser';
44
import commonjs from '@rollup/plugin-commonjs';
5-
import { move, remove } from 'fs-extra';
5+
import { remove } from 'fs-extra';
66

77
async function build({
88
input,
@@ -43,21 +43,11 @@ async function build({
4343
}
4444

4545
(async (): Promise<void> => {
46-
await Promise.all([remove('core'), remove('web')]);
47-
await Promise.all([
48-
build({
49-
input: 'src/index.ts',
50-
output: 'core',
51-
declaration: true,
52-
external: ['react', 'react-dom/server', 'ts-graphviz', 'prop-types', 'react-reconciler'],
53-
}),
54-
build({
55-
input: 'src/web/index.ts',
56-
output: 'core/web',
57-
declaration: false,
58-
external: ['react', 'react-dom/server', 'ts-graphviz', 'react-reconciler', '@hpcc-js/wasm'],
59-
}),
60-
]);
61-
62-
await move('core/web', 'web');
46+
await remove('lib');
47+
await build({
48+
input: 'src/index.ts',
49+
output: 'lib',
50+
declaration: true,
51+
external: ['react', 'react-dom/server', 'ts-graphviz', 'prop-types', 'react-reconciler', '@hpcc-js/wasm'],
52+
});
6353
})();

src/web/components/Graphviz.tsx renamed to src/components/Graphviz.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable react/no-danger */
22
/* eslint-disable react/prop-types */
3-
import React, { FC, ReactElement, useMemo } from 'react';
4-
import { renderToDot } from '../../renderer/render';
5-
import { useRendered, Engine, Image, File } from '../hooks/rendered';
3+
import React, { FC, useMemo, ReactElement } from 'react';
4+
import { renderToDot } from '../renderer/render';
5+
import { useRendered, Engine, Image, File } from '../hooks/use-rendered';
66

77
type Props = {
88
children: ReactElement;
@@ -11,6 +11,9 @@ type Props = {
1111
files?: File[];
1212
};
1313

14+
/**
15+
* Web only
16+
*/
1417
export const Graphviz: FC<Props> = ({ children, engine, images, files }) => {
1518
const dot = useMemo(() => renderToDot(children), [children]);
1619
const rendered = useRendered(dot, engine, 'svg', { images, files });
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import { Graphviz } from '../Graphviz';
4+
import { Engine, Format, Image } from '../../hooks/use-rendered';
5+
import { Digraph } from '../Digraph';
6+
import { Node } from '../Node';
7+
import { Edge } from '../Edge';
8+
9+
jest.mock('../../hooks/use-rendered', () => {
10+
return {
11+
useRendered: (
12+
dot: string,
13+
engine?: Engine,
14+
format?: Format,
15+
ext?: {
16+
images?: Image[];
17+
files?: File[];
18+
},
19+
): string => JSON.stringify({ dot, engine, format, ext }),
20+
};
21+
});
22+
23+
describe('Graphviz component', () => {
24+
test('render dot element', () => {
25+
const { container } = render(
26+
<Graphviz>
27+
<Digraph>
28+
<Node id="n1" />
29+
<Node id="n2" />
30+
<Node id="n3" />
31+
<Edge targets={['n1', 'n2', 'n3']} />
32+
<Edge targets={['n1', 'n3']} />
33+
</Digraph>
34+
</Graphviz>,
35+
);
36+
expect(container).toMatchSnapshot();
37+
});
38+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Graphviz component render dot element 1`] = `
4+
<div>
5+
<div>
6+
{"dot":"digraph {\\n \\"n1\\";\\n \\"n2\\";\\n \\"n3\\";\\n \\"n1\\" -&gt; \\"n2\\" -&gt; \\"n3\\";\\n \\"n1\\" -&gt; \\"n3\\";\\n}","format":"svg","ext":{"images":[],"files":[]}}
7+
</div>
8+
</div>
9+
`;

0 commit comments

Comments
 (0)