Skip to content

Commit 81dcf55

Browse files
author
Nicolas ERNY
authored
feat(model): Add modelData support + fix unit test warnings (#123)
* feat(model): Add modelData support + fix unit test warnings * Fix lint
1 parent 0f2ea98 commit 81dcf55

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-gojs",
3-
"version": "4.8.0",
3+
"version": "4.9.0",
44
"description": "GoJS React integration",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/GojsDiagram.test.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ describe('<GojsDiagram />', () => {
9999
};
100100

101101
const myDiagramId = 'myDiagramId';
102+
const modelData = { test: 'modeldatata' };
102103

103104
const defaultSelectedNodeKey = 'Beta';
104105

@@ -107,6 +108,10 @@ describe('<GojsDiagram />', () => {
107108
let modelChangeCallback;
108109
let keyIndex = 0;
109110

111+
const dom = document.body;
112+
const rootDiv = document.createElement('div');
113+
dom.appendChild(rootDiv);
114+
110115
beforeEach(() => {
111116
Element.prototype.getBoundingClientRect = function() {
112117
return {
@@ -116,20 +121,18 @@ describe('<GojsDiagram />', () => {
116121
left: 0,
117122
x: 0,
118123
y: 0,
119-
toJSON: () => {
120-
console.log('test');
121-
},
124+
toJSON: () => '',
122125
bottom: 0,
123126
right: 0
124127
};
125128
};
126129
keyIndex = 0;
127-
const dom = document.body;
128130
modelChangeCallback = jest.fn();
129131
wrapper = mount(
130132
<GojsDiagram
131133
diagramId={myDiagramId}
132134
model={model}
135+
initialModelData={modelData}
133136
createDiagram={id => {
134137
diagram = createDiagram(myDiagramId);
135138
return diagram;
@@ -158,7 +161,7 @@ describe('<GojsDiagram />', () => {
158161
}}
159162
defaultSelectedNode={defaultSelectedNodeKey}
160163
/>,
161-
{ attachTo: dom }
164+
{ attachTo: rootDiv }
162165
);
163166
jest.runAllTimers();
164167
});
@@ -167,6 +170,10 @@ describe('<GojsDiagram />', () => {
167170
expect(diagram.model.nodeCategoryProperty === 'category').toBeTruthy();
168171
});
169172

173+
it('should initialize the modelData', () => {
174+
expect(diagram.model.modelData).toBe(modelData);
175+
});
176+
170177
it('should render links and nodes in the diagram based on the model provided as prop', () => {
171178
checkIfDiagramRendersModel(model, diagram);
172179
});

src/GojsDiagram.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515

1616
export interface GojsDiagramProps<N extends BaseNodeModel, L extends LinkModel> {
1717
model: DiagramModel<N, L>;
18+
initialModelData?: ObjectData;
1819
createDiagram: (id: string) => Diagram;
1920
diagramId: string;
2021
className: string;
@@ -131,7 +132,8 @@ class GojsDiagram<N extends BaseNodeModel, L extends LinkModel> extends React.Pu
131132
nodeCategoryProperty: this.props.nodeCategoryProperty || 'category',
132133
linkKeyProperty: this.props.linkKeyProperty || '',
133134
makeUniqueLinkKeyFunction: this.props.makeUniqueLinkKeyFunction || null,
134-
copyNodeDataFunction: this.props.copyNodeDataFunction || null
135+
copyNodeDataFunction: this.props.copyNodeDataFunction || null,
136+
...(this.props.initialModelData && { modelData: this.props.initialModelData })
135137
});
136138

137139
if (defaultSelectedNode) {

0 commit comments

Comments
 (0)