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

Commit 4b44134

Browse files
authored
Merge pull request #148 from tokidrill/feat/allow-xlabel-to-describe-with-htlm-like
Allow xlabel to be decribed with "Node" HTML like element
2 parents a9b49c5 + 670d545 commit 4b44134

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

src/components/Node.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import PropTypes from 'prop-types';
33
import { useNode, NodeProps } from '../hooks/use-node';
44
import { useRenderedID } from '../hooks/use-rendered-id';
55

6-
type Props = Omit<NodeProps, 'label'> & {
6+
type Props = Omit<NodeProps, 'label' | 'xlabel'> & {
77
label?: ReactElement | string;
8+
xlabel?: ReactElement | string;
89
};
910

10-
export const Node: FC<Props> = ({ children, label, ...props }) => {
11+
export const Node: FC<Props> = ({ children, label, xlabel, ...props }) => {
1112
const renderedLabel = useRenderedID(label);
13+
const renderedXlabel = useRenderedID(xlabel);
14+
1215
if (renderedLabel !== undefined) Object.assign(props, { label: renderedLabel });
16+
if (renderedXlabel !== undefined) Object.assign(props, { xlabel: renderedXlabel });
1317
useNode(props);
1418
return <>{children}</>;
1519
};
@@ -20,9 +24,11 @@ Node.propTypes = {
2024
id: PropTypes.string.isRequired,
2125
comment: PropTypes.string,
2226
label: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
27+
xlabel: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
2328
};
2429

2530
Node.defaultProps = {
2631
comment: undefined,
2732
label: undefined,
33+
xlabel: undefined,
2834
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from 'react';
2+
import 'jest-graphviz';
3+
import { Digraph } from '../Digraph';
4+
import { Node } from '../Node';
5+
import { DOT } from '../HtmlLike';
6+
import { renderToDot } from '../../renderer/render';
7+
8+
describe('Node', () => {
9+
test('pass without optional props and render correctly', () => {
10+
const dot = renderToDot(
11+
<Digraph>
12+
<Node id="aaa" />
13+
</Digraph>,
14+
);
15+
expect(dot).toBeValidDotAndMatchSnapshot();
16+
});
17+
18+
test('pass label with string and render correctly', () => {
19+
const dot = renderToDot(
20+
<Digraph>
21+
<Node id="aaa" label="label test" />
22+
</Digraph>,
23+
);
24+
expect(dot).toBeValidDotAndMatchSnapshot();
25+
});
26+
27+
test('pass label with HTMLLike ReactElement and render correctly', () => {
28+
const dot = renderToDot(
29+
<Digraph>
30+
<Node
31+
id="aaa"
32+
label={
33+
<DOT.TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
34+
<DOT.TR>
35+
<DOT.TD>left</DOT.TD>
36+
<DOT.TD PORT="m">middle</DOT.TD>
37+
<DOT.TD PORT="r">right</DOT.TD>
38+
</DOT.TR>
39+
</DOT.TABLE>
40+
}
41+
/>
42+
</Digraph>,
43+
);
44+
expect(dot).toBeValidDotAndMatchSnapshot();
45+
});
46+
47+
test('pass xlabel with string and render correctly', () => {
48+
const dot = renderToDot(
49+
<Digraph>
50+
<Node id="aaa" xlabel="xlabel test" />
51+
</Digraph>,
52+
);
53+
expect(dot).toBeValidDotAndMatchSnapshot();
54+
});
55+
56+
test('pass xlabel with HTMLLike ReactElement and render correctly', () => {
57+
const dot = renderToDot(
58+
<Digraph>
59+
<Node
60+
id="aaa"
61+
xlabel={
62+
<DOT.TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
63+
<DOT.TR>
64+
<DOT.TD>left</DOT.TD>
65+
<DOT.TD PORT="m">middle</DOT.TD>
66+
<DOT.TD PORT="r">right</DOT.TD>
67+
</DOT.TR>
68+
</DOT.TABLE>
69+
}
70+
/>
71+
</Digraph>,
72+
);
73+
expect(dot).toBeValidDotAndMatchSnapshot();
74+
});
75+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Node pass label with HTMLLike ReactElement and render correctly 1`] = `
4+
digraph {
5+
"aaa" [
6+
label = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD>left</TD><TD PORT="m">middle</TD><TD PORT="r">right</TD></TR></TABLE>>,
7+
];
8+
}
9+
`;
10+
11+
exports[`Node pass label with string and render correctly 1`] = `
12+
digraph {
13+
"aaa" [
14+
label = "label test",
15+
];
16+
}
17+
`;
18+
19+
exports[`Node pass without optional props and render correctly 1`] = `
20+
digraph {
21+
"aaa";
22+
}
23+
`;
24+
25+
exports[`Node pass xlabel with HTMLLike ReactElement and render correctly 1`] = `
26+
digraph {
27+
"aaa" [
28+
xlabel = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD>left</TD><TD PORT="m">middle</TD><TD PORT="r">right</TD></TR></TABLE>>,
29+
];
30+
}
31+
`;
32+
33+
exports[`Node pass xlabel with string and render correctly 1`] = `
34+
digraph {
35+
"aaa" [
36+
xlabel = "xlabel test",
37+
];
38+
}
39+
`;

0 commit comments

Comments
 (0)