@reltio/graph 1.4.1585 → 1.4.1586-mui5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/index.ts +1 -0
  2. package/package.json +26 -9
  3. package/public/bundle.js +205 -0
  4. package/public/bundle.js.LICENSE.txt +88 -0
  5. package/public/package.json +29 -0
  6. package/scripts/build/index.js +20 -0
  7. package/src/__tests__/GraphView.test.tsx +25 -0
  8. package/src/components/CircleLayout/CircleLayout.tsx +76 -0
  9. package/src/components/CircleLayout/__tests__/CircleLayout.test.tsx +194 -0
  10. package/src/components/GotoButton/GotoButton.tsx +23 -0
  11. package/src/components/GotoButton/__tests__/GotoButton.test.tsx +33 -0
  12. package/src/components/GotoButton/styles.ts +15 -0
  13. package/src/components/GraphArea/GraphArea.tsx +113 -0
  14. package/src/components/GraphArea/__tests__/GraphArea.test.tsx +202 -0
  15. package/src/components/GraphArea/styles.ts +30 -0
  16. package/src/components/GraphLayoutSelector/GraphLayoutSelector.tsx +43 -0
  17. package/src/components/GraphLayoutSelector/__tests__/GraphLayoutSelector.spec.tsx +44 -0
  18. package/src/components/GraphLayoutSelector/styles.ts +15 -0
  19. package/src/components/GraphPerspectiveView/GraphPerspectiveView.tsx +58 -0
  20. package/src/components/GraphPerspectiveView/__tests__/GraphPerspectiveView.test.tsx +115 -0
  21. package/src/components/GraphPerspectiveView/__tests__/useGraphLoader.test.tsx +1519 -0
  22. package/src/components/GraphPerspectiveView/__tests__/useRelationshipTable.test.tsx +1003 -0
  23. package/src/components/GraphPerspectiveView/__tests__/useSelectedEntity.test.tsx +373 -0
  24. package/src/components/GraphPerspectiveView/styles.ts +34 -0
  25. package/src/components/GraphRightSidePanel/GraphRightSidePanel.tsx +27 -0
  26. package/src/components/GraphRightSidePanel/__tests__/GraphRightSidePanel.test.tsx +18 -0
  27. package/src/components/GraphRightSidePanel/icons/relationship-icon.svg +3 -0
  28. package/src/components/GraphRightSidePanel/styles.ts +8 -0
  29. package/src/components/GraphTypeSelector/GraphTypeSelector.tsx +56 -0
  30. package/src/components/GraphTypeSelector/__tests__/GraphTypeSelector.test.tsx +109 -0
  31. package/src/components/GraphTypeSelector/styles.ts +17 -0
  32. package/src/components/HierarchyLayout/HierarchyLayout.ts +102 -0
  33. package/src/components/HierarchyLayout/__tests__/HierarchyLayout.spec.tsx +241 -0
  34. package/src/components/HierarchyLayout/__tests__/buildHierarchy.spec.ts +130 -0
  35. package/src/components/HierarchyLayout/__tests__/calculations.spec.ts +230 -0
  36. package/src/components/HierarchyLayout/buildHierarchy.ts +109 -0
  37. package/src/components/HierarchyLayout/calculations.ts +86 -0
  38. package/src/components/RightPanelEntityDetails/RightPanelEntityDetails.tsx +33 -0
  39. package/src/components/RightPanelEntityDetails/__tests__/RightPanelEntityDetails.test.tsx +39 -0
  40. package/src/components/RightPanelEntityDetails/styles.ts +12 -0
  41. package/src/components/RightPanelProfileBand/RightPanelProfileBand.tsx +33 -0
  42. package/src/components/RightPanelProfileBand/__tests__/RightPanelProfileBand.test.tsx +67 -0
  43. package/src/components/RightPanelProfileBand/styles.ts +8 -0
  44. package/src/components/RightPanelRelationship/RightPanelRelationship.tsx +25 -0
  45. package/src/components/RightPanelRelationship/__tests__/RightPanelRelationship.test.tsx +18 -0
  46. package/src/components/RightPanelRelationship/components/CollapsibleTable/CollapsibleTable.tsx +100 -0
  47. package/src/components/RightPanelRelationship/components/CollapsibleTable/TableHead.tsx +38 -0
  48. package/src/components/RightPanelRelationship/components/CollapsibleTable/__tests__/CollapsibleTable.test.tsx +169 -0
  49. package/src/components/RightPanelRelationship/components/CollapsibleTable/__tests__/TableHead.test.tsx +107 -0
  50. package/src/components/RightPanelRelationship/components/CollapsibleTable/styles.ts +38 -0
  51. package/src/components/RightPanelRelationship/components/CollapsibleTable/types.ts +20 -0
  52. package/src/components/RightPanelRelationship/components/DeleteRelationDialog/DeleteRelationDialog.tsx +34 -0
  53. package/src/components/RightPanelRelationship/components/DeleteRelationDialog/__tests__/DeleteRelationDialog.test.tsx +46 -0
  54. package/src/components/RightPanelRelationship/components/GraphAddRelationDialog/GraphAddRelationButton.tsx +43 -0
  55. package/src/components/RightPanelRelationship/components/GraphAddRelationDialog/GraphAddRelationDialog.tsx +121 -0
  56. package/src/components/RightPanelRelationship/components/GraphAddRelationDialog/__tests__/GraphAddRelationButton.test.tsx +80 -0
  57. package/src/components/RightPanelRelationship/components/GraphAddRelationDialog/__tests__/GraphAddRelationDialog.test.tsx +262 -0
  58. package/src/components/RightPanelRelationship/components/GraphAddRelationDialog/__tests__/mockdata.ts +75 -0
  59. package/src/components/RightPanelRelationship/components/GraphAddRelationDialog/styles.ts +25 -0
  60. package/src/components/RightPanelRelationship/components/GraphAddRelationDialog/useCreatableInOutRelationTypes.ts +13 -0
  61. package/src/components/RightPanelRelationship/components/RelationshipTable/RelationshipTable.tsx +126 -0
  62. package/src/components/RightPanelRelationship/components/RelationshipTable/__tests__/RelationshipTable.test.tsx +176 -0
  63. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/ControlsCellRenderer.tsx +59 -0
  64. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/DefaultCellRenderer.tsx +29 -0
  65. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/ExpandedRowRenderer.tsx +94 -0
  66. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/HeadCellRenderer.tsx +34 -0
  67. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/ProfileCellRenderer.tsx +41 -0
  68. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/__tests__/ControlsCellRenderer.test.tsx +80 -0
  69. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/__tests__/DefaultCellRenderer.test.tsx +47 -0
  70. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/__tests__/ExpandedRowRenderer.test.tsx +166 -0
  71. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/__tests__/HeadCellRenderer.test.tsx +23 -0
  72. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/__tests__/ProfileCellRenderer.test.tsx +47 -0
  73. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/components/DeleteRelationButton.tsx +32 -0
  74. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/components/__tests__/DeleteRelationButton.test.tsx +28 -0
  75. package/src/components/RightPanelRelationship/components/RelationshipTable/cell-renderers/styles.ts +113 -0
  76. package/src/components/RightPanelRelationship/components/RelationshipTable/styles.ts +36 -0
  77. package/src/components/RightPanelRelationship/components/RelationshipsFilters/RelationshipsFilters.tsx +41 -0
  78. package/src/components/RightPanelRelationship/components/RelationshipsFilters/RelationshipsFiltersDialog/RelationshipsFiltersDialog.tsx +91 -0
  79. package/src/components/RightPanelRelationship/components/RelationshipsFilters/RelationshipsFiltersDialog/__tests__/RelationshipsFilterDialog.test.tsx +192 -0
  80. package/src/components/RightPanelRelationship/components/RelationshipsFilters/RelationshipsFiltersDialog/styles.ts +34 -0
  81. package/src/components/RightPanelRelationship/components/RelationshipsFilters/RelationshipsSearchTextField/RelationshipsSearchTextField.tsx +31 -0
  82. package/src/components/RightPanelRelationship/components/RelationshipsFilters/RelationshipsSearchTextField/__tests__/RelationshipsSearchTextField.test.tsx +43 -0
  83. package/src/components/RightPanelRelationship/components/RelationshipsFilters/RelationshipsSearchTextField/styles.ts +17 -0
  84. package/src/components/RightPanelRelationship/components/RelationshipsFilters/__tests__/RelationshipsFilters.test.tsx +76 -0
  85. package/src/components/RightPanelRelationship/components/RelationshipsFilters/styles.ts +19 -0
  86. package/src/components/RightPanelRelationship/styles.ts +20 -0
  87. package/src/components/SigmaCustomRenderers/SigmaCustomRenderersContainer.tsx +16 -0
  88. package/src/components/SigmaGraphContainer/SigmaGraphContainer.tsx +55 -0
  89. package/src/components/SigmaGraphContainer/__tests__/SigmaGraphContainer.test.tsx +25 -0
  90. package/src/components/SigmaGraphResizer/SigmaGraphResizer.tsx +13 -0
  91. package/src/components/SigmaGraphResizer/__tests__/SigmaGraphResizer.test.tsx +28 -0
  92. package/src/components/TreeLayout/TreeLayout.tsx +33 -0
  93. package/src/components/TreeLayout/__tests__/TreeLayout.test.tsx +42 -0
  94. package/src/components/TreeLayout/styles.ts +10 -0
  95. package/src/components/ZoomSlider/ZoomSlider.tsx +90 -0
  96. package/src/components/ZoomSlider/__tests__/ZoomSlider.test.tsx +84 -0
  97. package/src/components/ZoomSlider/styles.ts +57 -0
  98. package/src/constants/index.ts +1 -0
  99. package/src/contexts/GraphStateContext.tsx +19 -0
  100. package/src/contexts/SigmaCustomRenderersContext.ts +20 -0
  101. package/src/helpers/relationsHelpers.ts +9 -0
  102. package/src/hooks/__tests__/useGraphLayout.test.tsx +825 -0
  103. package/src/hooks/__tests__/useGraphLayoutSelector.test.tsx +19 -0
  104. package/src/hooks/__tests__/useSelfRelationLoopsRenderer.test.ts +198 -0
  105. package/src/hooks/__tests__/useSigmaCustomRenderers.test.ts +273 -0
  106. package/src/hooks/__tests__/useTooltipRenderer.test.ts +289 -0
  107. package/src/hooks/useGraphLayout.ts +308 -0
  108. package/src/hooks/useGraphLayoutSelector.ts +19 -0
  109. package/src/hooks/useGraphLoader.ts +279 -0
  110. package/src/hooks/useGraphRightSidePanelElements.tsx +36 -0
  111. package/src/hooks/useGraphState.ts +40 -0
  112. package/src/hooks/useGraphType.ts +32 -0
  113. package/src/hooks/useRelationshipTable.ts +212 -0
  114. package/src/hooks/useRelationshipTableFilters.ts +43 -0
  115. package/src/hooks/useSelectedEntity.ts +89 -0
  116. package/src/hooks/useSelfRelationLoopsRenderer.ts +52 -0
  117. package/src/hooks/useSigmaCustomRenderers.ts +112 -0
  118. package/src/hooks/useTooltipRenderer.ts +80 -0
  119. package/src/index.tsx +37 -0
  120. package/src/rendering/canvas/__tests__/hover.spec.ts +31 -0
  121. package/src/rendering/canvas/__tests__/label.spec.ts +64 -0
  122. package/src/rendering/canvas/__tests__/nodeBackground.spec.ts +99 -0
  123. package/src/rendering/canvas/__tests__/selfRelationLoop.spec.ts +88 -0
  124. package/src/rendering/canvas/__tests__/tooltip.spec.ts +33 -0
  125. package/src/rendering/canvas/hover.ts +13 -0
  126. package/src/rendering/canvas/label.ts +33 -0
  127. package/src/rendering/canvas/nodeBackground.ts +61 -0
  128. package/src/rendering/canvas/selfRelationLoop.ts +108 -0
  129. package/src/rendering/canvas/tooltip.ts +38 -0
  130. package/src/rendering/icons/collapseIcon.inline.svg.ts +6 -0
  131. package/src/rendering/icons/expandIcon.inline.svg.ts +5 -0
  132. package/src/rendering/icons/no_photo.inline.svg.ts +13 -0
  133. package/src/rendering/webgl/edge.arrowHead.ts +87 -0
  134. package/src/rendering/webgl/edge.clamped.ts +246 -0
  135. package/src/rendering/webgl/edge.reversedArrowHead.ts +14 -0
  136. package/src/rendering/webgl/helpers/__tests__/imageHelper.spec.ts +36 -0
  137. package/src/rendering/webgl/helpers/imageHelper.ts +13 -0
  138. package/src/rendering/webgl/image.ts +115 -0
  139. package/src/rendering/webgl/node.border.ts +78 -0
  140. package/src/rendering/webgl/node.buttons.ts +241 -0
  141. package/src/rendering/webgl/node.image.ts +169 -0
  142. package/src/rendering/webgl/shaders/edge.clamped.vert.glsl.ts +55 -0
  143. package/src/rendering/webgl/shaders/node.border.frag.glsl.ts +30 -0
  144. package/src/rendering/webgl/shaders/node.border.vert.glsl.ts +37 -0
  145. package/src/rendering/webgl/shaders/node.buttons.frag.glsl.ts +32 -0
  146. package/src/rendering/webgl/shaders/node.buttons.vert.glsl.ts +49 -0
  147. package/src/rendering/webgl/shaders/node.image.frag.glsl.ts +46 -0
  148. package/src/rendering/webgl/shaders/node.image.vert.glsl.ts +42 -0
  149. package/src/types/graphDataTypes.ts +143 -0
  150. package/src/types/sigmaCustomRenderersTypes.ts +13 -0
  151. package/src/utils/__tests__/graph.spec.ts +152 -0
  152. package/src/utils/__tests__/graphLayout.spec.ts +125 -0
  153. package/src/utils/__tests__/hopsData.spec.ts +456 -0
  154. package/src/utils/__tests__/processResponceToGraph.spec.ts +165 -0
  155. package/src/utils/graph.ts +83 -0
  156. package/src/utils/graphLayout.ts +70 -0
  157. package/src/utils/hopsData.ts +198 -0
  158. package/src/utils/processResponseToGraph.ts +32 -0
  159. package/tsconfig.json +4 -0
  160. package/webpack.config.js +10 -0
  161. package/bundle.js +0 -2
  162. package/bundle.js.LICENSE.txt +0 -54
  163. /package/{5271a9e7b6651c852e93.png → public/5271a9e7b6651c852e93.png} +0 -0
  164. /package/{main.css → public/main.css} +0 -0
@@ -0,0 +1,19 @@
1
+ import {GraphLayout} from '@reltio/mdm-sdk';
2
+ import {renderHook} from '@testing-library/react-hooks';
3
+ import {useGraphLayoutSelector} from '../useGraphLayoutSelector';
4
+
5
+ const startHook = (graphLayout) => {
6
+ return renderHook(useGraphLayoutSelector, {
7
+ initialProps: graphLayout
8
+ });
9
+ };
10
+
11
+ describe('useGraphLayoutSelector tests', () => {
12
+ it('should set graph layout from props if it is correct layout', () => {
13
+ expect(startHook(GraphLayout.TREE).result.current).toMatchObject({layout: GraphLayout.TREE});
14
+ });
15
+
16
+ it('should return GraphLayout.SIMPLE_NETWORK if layout from props is not correct', () => {
17
+ expect(startHook('abs').result.current).toMatchObject({layout: GraphLayout.SIMPLE_NETWORK});
18
+ });
19
+ });
@@ -0,0 +1,198 @@
1
+ import {renderHook, act} from '@testing-library/react-hooks';
2
+ import {MultiGraph} from 'graphology';
3
+ import {useSigma} from '@react-sigma/core';
4
+ import {HopsResponse, RelationTypeDirection} from '@reltio/mdm-sdk';
5
+ import {fillGraphWithHopsResponse} from '../../utils/graph';
6
+ import {useSelfRelationLoopsRenderer} from '../useSelfRelationLoopsRenderer';
7
+ import {useSigmaCustomRenderers} from '../../contexts/SigmaCustomRenderersContext';
8
+ import {drawSelfRelationLoop} from '../../rendering/canvas/selfRelationLoop';
9
+
10
+ jest.mock('@react-sigma/core');
11
+ jest.mock('../../contexts/SigmaCustomRenderersContext');
12
+ jest.mock('../../rendering/canvas/selfRelationLoop');
13
+
14
+ const data: HopsResponse = {
15
+ entities: [
16
+ {uri: 'node1', type: 'hcp', traversedRelations: 2, untraversedRelations: 0},
17
+ {uri: 'node2', type: 'hcp', traversedRelations: 3, untraversedRelations: 2},
18
+ {uri: 'node3', type: 'hca', traversedRelations: 1, untraversedRelations: 0}
19
+ ],
20
+ relations: [
21
+ {
22
+ uri: 'r1',
23
+ type: 'relType1',
24
+ startObject: {
25
+ objectURI: 'node1'
26
+ },
27
+ endObject: {
28
+ objectURI: 'node2'
29
+ },
30
+ direction: RelationTypeDirection.directed
31
+ },
32
+ {
33
+ uri: 'r2',
34
+ type: 'relType2',
35
+ startObject: {
36
+ objectURI: 'node3'
37
+ },
38
+ endObject: {
39
+ objectURI: 'node2'
40
+ },
41
+ direction: RelationTypeDirection.directed
42
+ },
43
+ {
44
+ uri: 'r3',
45
+ type: 'relType3',
46
+ startObject: {
47
+ objectURI: 'node2'
48
+ },
49
+ endObject: {
50
+ objectURI: 'node2'
51
+ },
52
+ direction: RelationTypeDirection.directed
53
+ },
54
+ {
55
+ uri: 'r4',
56
+ type: 'relType4',
57
+ startObject: {
58
+ objectURI: 'node1'
59
+ },
60
+ endObject: {
61
+ objectURI: 'node1'
62
+ },
63
+ direction: RelationTypeDirection.directed
64
+ }
65
+ ]
66
+ };
67
+
68
+ const edges = {
69
+ r1: {
70
+ size: 1,
71
+ color: 'rgba(0, 0, 0, 0.2)',
72
+ forceLabel: false,
73
+ loop: false,
74
+ relationTypeUri: 'relType1',
75
+ direction: RelationTypeDirection.directed
76
+ },
77
+ r2: {
78
+ size: 1,
79
+ color: 'rgba(0, 0, 0, 0.2)',
80
+ forceLabel: false,
81
+ loop: false,
82
+ relationTypeUri: 'relType2',
83
+ direction: RelationTypeDirection.directed
84
+ },
85
+ r3: {
86
+ size: 1,
87
+ color: 'rgba(0, 0, 0, 0.2)',
88
+ forceLabel: false,
89
+ hidden: true,
90
+ loop: true,
91
+ relationTypeUri: 'relType3',
92
+ direction: RelationTypeDirection.directed
93
+ },
94
+ r4: {
95
+ size: 1,
96
+ color: 'rgba(0, 0, 0, 0.2)',
97
+ forceLabel: true,
98
+ loop: true,
99
+ relationTypeUri: 'relType4',
100
+ direction: RelationTypeDirection.directed
101
+ }
102
+ };
103
+
104
+ const nodes = {
105
+ node1: {
106
+ size: 12,
107
+ label: 'node 1',
108
+ x: 0.5,
109
+ y: 0.4
110
+ },
111
+ node2: {
112
+ size: 12,
113
+ label: 'node 2',
114
+ x: 0.6,
115
+ y: 0.6
116
+ },
117
+ node3: {
118
+ size: 12,
119
+ label: 'node 3',
120
+ x: 0.8,
121
+ y: 0.7
122
+ }
123
+ };
124
+
125
+ const startHook = (graph) => renderHook(useSelfRelationLoopsRenderer, {initialProps: graph});
126
+
127
+ const createGraph = () => {
128
+ const graph = new MultiGraph();
129
+ fillGraphWithHopsResponse(graph, data);
130
+ return graph;
131
+ };
132
+
133
+ describe('useSelfRelationLoopsRenderer tests', () => {
134
+ const getEdgeDisplayData = jest.fn().mockImplementation((edgeId) => edges[edgeId]);
135
+ const getNodeDisplayData = jest.fn().mockImplementation((nodeId) => nodes[nodeId]);
136
+ const scaleSize = jest.fn().mockImplementation((size) => size * 2);
137
+ const framedGraphToViewport = jest.fn().mockImplementation(({x, y}) => ({x: x * 100, y: y * 100}));
138
+
139
+ const addRenderers = jest.fn();
140
+ const canvases = {selfRelationLoops: {}};
141
+
142
+ beforeAll(() => {
143
+ (drawSelfRelationLoop as jest.Mock).mockImplementation(
144
+ (_context, edgeData) => `path-${edgeData.relationTypeUri}`
145
+ );
146
+
147
+ (useSigmaCustomRenderers as jest.Mock).mockReturnValue({
148
+ addRenderers,
149
+ canvases
150
+ });
151
+
152
+ (useSigma as jest.Mock).mockReturnValue({
153
+ getEdgeDisplayData,
154
+ getNodeDisplayData,
155
+ scaleSize,
156
+ framedGraphToViewport
157
+ });
158
+ });
159
+
160
+ const getSelfRelationLoopsRenderer = () =>
161
+ addRenderers.mock.calls[addRenderers.mock.calls.length - 1][0].selfRelationLoops;
162
+
163
+ it('should draw self relations correctly and with correctly setting paths', () => {
164
+ const graph = createGraph();
165
+ const {result} = startHook(graph);
166
+
167
+ expect(result.current).toEqual({
168
+ selfRelationsPathsMap: {},
169
+ selfRelationLoopsCanvas: canvases.selfRelationLoops
170
+ });
171
+
172
+ const {renderer} = getSelfRelationLoopsRenderer();
173
+
174
+ act(() => {
175
+ renderer('canvas');
176
+ });
177
+
178
+ expect(drawSelfRelationLoop).toHaveBeenCalledTimes(1);
179
+ expect(drawSelfRelationLoop).toHaveBeenCalledWith(
180
+ 'canvas',
181
+ {
182
+ ...edges.r4,
183
+ size: 2
184
+ },
185
+ {
186
+ ...nodes.node1,
187
+ size: 24,
188
+ x: 50,
189
+ y: 40
190
+ }
191
+ );
192
+
193
+ expect(result.current).toEqual({
194
+ selfRelationsPathsMap: {r4: 'path-relType4'},
195
+ selfRelationLoopsCanvas: canvases.selfRelationLoops
196
+ });
197
+ });
198
+ });
@@ -0,0 +1,273 @@
1
+ import {renderHook, act} from '@testing-library/react-hooks';
2
+ import {useSigmaContext} from '@react-sigma/core';
3
+ import {pipe, findLast, path, nth, equals} from 'ramda';
4
+ import {useSigmaCustomRenderers} from '../useSigmaCustomRenderers';
5
+ import {getPixelRatio} from 'sigma/utils';
6
+
7
+ jest.mock('@react-sigma/core');
8
+ jest.mock('sigma/utils');
9
+
10
+ describe('useSigmaCustomRenderers tests', () => {
11
+ const addListener = jest.fn();
12
+ const off = jest.fn();
13
+
14
+ const getRendererByEventName = (eventName) =>
15
+ pipe(path(['mock', 'calls']), findLast(pipe(nth(0), equals(eventName))), nth(1))(addListener);
16
+
17
+ const sigmaContainerElement = {
18
+ querySelector: (val) => val,
19
+ insertBefore: jest.fn()
20
+ };
21
+
22
+ const sigmaElement = {
23
+ querySelector: jest.fn(() => sigmaContainerElement),
24
+ offsetWidth: 200,
25
+ offsetHeight: 300
26
+ };
27
+
28
+ const context = {
29
+ scale: jest.fn(),
30
+ clearRect: jest.fn()
31
+ };
32
+
33
+ const canvas = {
34
+ style: {} as CSSStyleDeclaration,
35
+ setAttribute: jest.fn(),
36
+ getContext: jest.fn(() => context),
37
+ remove: jest.fn()
38
+ };
39
+
40
+ beforeEach(() => {
41
+ (useSigmaContext as jest.Mock).mockReturnValue({
42
+ sigma: {addListener, off},
43
+ container: sigmaElement
44
+ });
45
+ });
46
+
47
+ afterEach(() => {
48
+ jest.restoreAllMocks();
49
+ jest.clearAllMocks();
50
+ canvas.style = {} as CSSStyleDeclaration;
51
+ });
52
+
53
+ it('should add listeners', () => {
54
+ renderHook(useSigmaCustomRenderers);
55
+ expect(addListener).toHaveBeenCalledWith('beforeRender', expect.any(Function));
56
+ expect(addListener).toHaveBeenCalledWith('afterRender', expect.any(Function));
57
+ expect(addListener).toHaveBeenCalledWith('resize', expect.any(Function));
58
+ });
59
+
60
+ it('should remove listeners after unmount component', () => {
61
+ const {unmount} = renderHook(useSigmaCustomRenderers);
62
+ unmount();
63
+ expect(off).toHaveBeenCalledWith('beforeRender', expect.any(Function));
64
+ expect(off).toHaveBeenCalledWith('afterRender', expect.any(Function));
65
+ expect(off).toHaveBeenCalledWith('resize', expect.any(Function));
66
+ });
67
+
68
+ it('beforeRender should work correctly', () => {
69
+ const {result, rerender} = renderHook(useSigmaCustomRenderers);
70
+
71
+ jest.spyOn(document, 'createElement').mockReturnValue(canvas as any);
72
+
73
+ act(() => {
74
+ result.current.addRenderers({customRenderer: {renderer: jest.fn(), insertBefore: 'element'}});
75
+ });
76
+
77
+ rerender();
78
+
79
+ const beforeRender = getRendererByEventName('beforeRender');
80
+
81
+ act(() => {
82
+ beforeRender();
83
+ });
84
+
85
+ expect(sigmaElement.querySelector).toHaveBeenCalledWith('.sigma-container');
86
+ expect(canvas.style.position).toBe('absolute');
87
+ expect(canvas.setAttribute).toHaveBeenCalledWith('class', 'sigma-customRenderer');
88
+ expect(canvas.getContext).toHaveBeenCalledWith('2d', {preserveDrawingBuffer: false, antialias: false});
89
+ expect(sigmaContainerElement.insertBefore).toHaveBeenCalledWith(canvas, '.sigma-element');
90
+ expect(result.current).toEqual({
91
+ addRenderers: expect.any(Function),
92
+ canvases: {customRenderer: {canvasElement: canvas, context}}
93
+ });
94
+ expect(getPixelRatio).toHaveBeenCalled();
95
+ });
96
+
97
+ it('beforeRender should work correctly if canvas already has been created', () => {
98
+ const {result, rerender} = renderHook(useSigmaCustomRenderers);
99
+
100
+ act(() => {
101
+ result.current.addRenderers({customRenderer: {renderer: jest.fn(), insertBefore: 'element'}});
102
+ });
103
+
104
+ rerender();
105
+
106
+ const beforeRender = getRendererByEventName('beforeRender');
107
+
108
+ act(() => {
109
+ beforeRender();
110
+ });
111
+
112
+ expect(sigmaElement.querySelector).toHaveBeenCalledTimes(1);
113
+
114
+ act(() => {
115
+ beforeRender();
116
+ });
117
+
118
+ expect(sigmaElement.querySelector).toHaveBeenCalledTimes(1);
119
+ });
120
+
121
+ it('resize should work correctly if canvases is empty', () => {
122
+ const {result, rerender} = renderHook(useSigmaCustomRenderers);
123
+
124
+ jest.spyOn(document, 'createElement').mockReturnValue(canvas as any);
125
+
126
+ act(() => {
127
+ result.current.addRenderers({customRenderer: {renderer: jest.fn(), insertBefore: 'element'}});
128
+ });
129
+
130
+ rerender();
131
+
132
+ const resize = getRendererByEventName('resize');
133
+
134
+ act(() => {
135
+ resize();
136
+ });
137
+
138
+ expect(getPixelRatio).toHaveBeenCalled();
139
+ expect(canvas.setAttribute).not.toHaveBeenCalled();
140
+ });
141
+
142
+ it('resize should work correctly if canvas exists and pixelRatio equals 1', () => {
143
+ (getPixelRatio as jest.Mock).mockReturnValue(1);
144
+
145
+ const {result, rerender} = renderHook(useSigmaCustomRenderers);
146
+
147
+ jest.spyOn(document, 'createElement').mockReturnValue(canvas as any);
148
+
149
+ act(() => {
150
+ result.current.addRenderers({customRenderer: {renderer: jest.fn(), insertBefore: 'element'}});
151
+ });
152
+
153
+ rerender();
154
+
155
+ const beforeRender = getRendererByEventName('beforeRender');
156
+
157
+ act(() => {
158
+ beforeRender();
159
+ });
160
+
161
+ expect(result.current.canvases.customRenderer).not.toBeUndefined();
162
+
163
+ const resize = getRendererByEventName('resize');
164
+
165
+ jest.clearAllMocks();
166
+
167
+ act(() => {
168
+ resize();
169
+ });
170
+
171
+ expect(canvas.setAttribute).toHaveBeenCalledTimes(2);
172
+ expect(canvas.setAttribute).toHaveBeenCalledWith('width', '200px');
173
+ expect(canvas.setAttribute).toHaveBeenCalledWith('height', '300px');
174
+ expect(canvas.style.width).toBe('200px');
175
+ expect(canvas.style.height).toBe('300px');
176
+ expect(context.scale).not.toHaveBeenCalled();
177
+ });
178
+
179
+ it('resize should work correctly if canvas exists and pixelRatio does not equal 1', () => {
180
+ (getPixelRatio as jest.Mock).mockReturnValue(0.9);
181
+
182
+ const {result, rerender} = renderHook(useSigmaCustomRenderers);
183
+
184
+ jest.spyOn(document, 'createElement').mockReturnValue(canvas as any);
185
+
186
+ act(() => {
187
+ result.current.addRenderers({customRenderer: {renderer: jest.fn(), insertBefore: 'element'}});
188
+ });
189
+
190
+ rerender();
191
+
192
+ const beforeRender = getRendererByEventName('beforeRender');
193
+
194
+ act(() => {
195
+ beforeRender();
196
+ });
197
+
198
+ expect(result.current.canvases.customRenderer).not.toBeUndefined();
199
+
200
+ const resize = getRendererByEventName('resize');
201
+
202
+ jest.clearAllMocks();
203
+
204
+ act(() => {
205
+ resize();
206
+ });
207
+
208
+ expect(canvas.setAttribute).toHaveBeenCalledTimes(2);
209
+ expect(canvas.setAttribute).toHaveBeenCalledWith('width', '180px');
210
+ expect(canvas.setAttribute).toHaveBeenCalledWith('height', '270px');
211
+ expect(canvas.style.width).toBe('200px');
212
+ expect(canvas.style.height).toBe('300px');
213
+ expect(context.scale).toHaveBeenCalledWith(0.9, 0.9);
214
+ });
215
+
216
+ it('afterRender should work correctly', () => {
217
+ const {result, rerender} = renderHook(useSigmaCustomRenderers);
218
+
219
+ jest.spyOn(document, 'createElement').mockReturnValue(canvas as any);
220
+
221
+ const customRenderer = jest.fn();
222
+
223
+ act(() => {
224
+ result.current.addRenderers({customRenderer: {renderer: customRenderer, insertBefore: 'element'}});
225
+ });
226
+
227
+ rerender();
228
+
229
+ const beforeRender = getRendererByEventName('beforeRender');
230
+
231
+ act(() => {
232
+ beforeRender();
233
+ });
234
+
235
+ expect(result.current.canvases.customRenderer).not.toBeUndefined();
236
+
237
+ const afterRender = getRendererByEventName('afterRender');
238
+
239
+ jest.clearAllMocks();
240
+
241
+ act(() => {
242
+ afterRender();
243
+ });
244
+
245
+ expect(context.clearRect).toHaveBeenCalledWith(0, 0, 200, 300);
246
+ expect(customRenderer).toHaveBeenCalledWith(context);
247
+ });
248
+
249
+ it('should remove canvas element from DOM and clear canvases ref if component has been unmounted', () => {
250
+ const {result, rerender, unmount} = renderHook(useSigmaCustomRenderers);
251
+
252
+ jest.spyOn(document, 'createElement').mockReturnValue(canvas as any);
253
+
254
+ act(() => {
255
+ result.current.addRenderers({customRenderer: {renderer: jest.fn(), insertBefore: 'element'}});
256
+ });
257
+
258
+ rerender();
259
+
260
+ const beforeRender = getRendererByEventName('beforeRender');
261
+
262
+ act(() => {
263
+ beforeRender();
264
+ });
265
+
266
+ expect(result.current.canvases.customRenderer).not.toBeUndefined();
267
+
268
+ unmount();
269
+
270
+ expect(canvas.remove).toHaveBeenCalled();
271
+ expect(result.current.canvases).toEqual({});
272
+ });
273
+ });