Skip to content

Commit de198f9

Browse files
Increasing the test coverage (#167)
* added more test coverage * more coverage * didnt stage these * more stuff * more coverage (popup) * didnt know about that Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * same thing Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * again Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent cd7e797 commit de198f9

File tree

15 files changed

+681
-119
lines changed

15 files changed

+681
-119
lines changed

src/overlay/layer.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { Layer as OgmaLayer } from "@linkurious/ogma";
1111
import { useOgma } from "../context";
1212

1313
export interface LayerProps {
14+
/** Layer children */
1415
children?: ReactNode;
15-
/** Overlay container className */
16+
/** Layer container className */
1617
className?: string;
1718
/** Layer index */
1819
index?: number;
@@ -41,8 +42,11 @@ export const Layer = forwardRef(
4142
}, []);
4243

4344
useEffect(() => {
44-
if (layer) layer.element.className = className;
45-
}, [className]);
45+
if (layer) {
46+
if (className) layer.element.className = className;
47+
if (index !== undefined && isFinite(index)) layer.moveTo(index);
48+
}
49+
}, [className, index]);
4650

4751
if (!layer) return null;
4852

src/overlay/overlay.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ interface OverlayProps {
2121
position: Point | ((ogma: OgmaLib) => Point | null);
2222
/** Overlay size */
2323
size?: Size;
24+
/** Overlay index */
25+
index?: number;
26+
/** Overlay children */
2427
children?: ReactNode;
2528
/** Overlay container className */
2629
className?: string;
@@ -33,7 +36,7 @@ const offScreenPos: Point = { x: -9999, y: -9999 };
3336
// TODO: use props for these classes
3437
export const Overlay = forwardRef(
3538
(
36-
{ position, children, className = "", size, scaled }: OverlayProps,
39+
{ position, children, className = "", size, scaled, index }: OverlayProps,
3740
ref?: Ref<OverlayLayer>
3841
) => {
3942
const ogma = useOgma();
@@ -48,12 +51,15 @@ export const Overlay = forwardRef(
4851
newElement.className = className;
4952
// html = getContent(ogma, pos, undefined, children);
5053

51-
const overlay = ogma.layers.addOverlay({
52-
position: pos || offScreenPos,
53-
element: newElement,
54-
size: size || ({ width: "auto", height: "auto" } as any as Size),
55-
scaled
56-
});
54+
const overlay = ogma.layers.addOverlay(
55+
{
56+
position: pos || offScreenPos,
57+
element: newElement,
58+
size: size || ({ width: "auto", height: "auto" } as any as Size),
59+
scaled
60+
},
61+
index
62+
);
5763

5864
setLayer(overlay);
5965

@@ -70,9 +76,10 @@ export const Overlay = forwardRef(
7076
if (layer) {
7177
const pos = getPosition(position, ogma) || offScreenPos;
7278
if (className) layer.element.className = className;
79+
if (index !== undefined && isFinite(index)) layer.moveTo(index);
7380
layer.setPosition(pos);
7481
}
75-
}, [position, className]);
82+
}, [position, className, index]);
7683

7784
if (!layer) return null;
7885

src/overlay/types.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export type Content =
1111
| ((ogma: OgmaLib, position: Point | null) => ReactElement);
1212

1313
export type TooltipEventFunctions = {
14-
"backgroundClick": (target?: Point) => ReactNode,
15-
"backgroundDoubleclick": (target?: Point) => ReactNode,
16-
"backgroundRightclick": (target?: Point) => ReactNode,
17-
"edgeClick": (target?: Edge) => ReactNode,
18-
"edgeDoubleclick": (target?: Edge) => ReactNode,
19-
"edgeHover": (target?: Edge) => ReactNode,
20-
"edgeRightclick": (target?: Edge) => ReactNode,
21-
"nodeClick": (target?: Node) => ReactNode,
22-
"nodeDoubleclick": (target?: Node) => ReactNode,
23-
"nodeHover": (target?: Node) => ReactNode,
24-
"nodeRightclick": (target?: Node) => ReactNode
14+
"backgroundClick": (target: Point) => ReactNode,
15+
"backgroundDoubleclick": (target: Point) => ReactNode,
16+
"backgroundRightclick": (target: Point) => ReactNode,
17+
"edgeClick": (target: Edge) => ReactNode,
18+
"edgeDoubleclick": (target: Edge) => ReactNode,
19+
"edgeHover": (target: Edge) => ReactNode,
20+
"edgeRightclick": (target: Edge) => ReactNode,
21+
"nodeClick": (target: Node) => ReactNode,
22+
"nodeDoubleclick": (target: Node) => ReactNode,
23+
"nodeHover": (target: Node) => ReactNode,
24+
"nodeRightclick": (target: Node) => ReactNode
2525
}

src/overlay/utils.ts

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactNode, ReactElement } from "react";
22
import { renderToString } from "react-dom/server";
3-
import OgmaLib, { Node as OgmaNode, Edge, Point, Size } from "@linkurious/ogma";
3+
import OgmaLib, { Node as OgmaNode, Edge, Point } from "@linkurious/ogma";
44
import { Content, PositionGetter, Placement, TooltipEventFunctions } from "./types";
55

66
export function getContent(
@@ -37,33 +37,6 @@ export function getCloseButton(
3737
return "";
3838
}
3939

40-
export function getAdjustedPlacement(
41-
coords: Point,
42-
placement: Placement,
43-
dimensions: Size,
44-
ogma: OgmaLib
45-
): Placement {
46-
const { width: screenWidth, height: screenHeight } = ogma.view.getSize();
47-
const { x, y } = ogma.view.graphToScreenCoordinates(coords);
48-
let res = placement;
49-
const { width, height } = dimensions;
50-
51-
if (placement === "left" && x - width < 0) res = "right";
52-
else if (placement === "right" && x + width > screenWidth) res = "left";
53-
else if (placement === "bottom" && y + height > screenHeight) res = "top";
54-
else if (placement === "top" && y - height < 0) res = "bottom";
55-
56-
if (res === "right" || res === "left") {
57-
if (y + height / 2 > screenHeight) res = "top";
58-
else if (y - height / 2 < 0) res = "bottom";
59-
} else {
60-
if (x + width / 2 > screenWidth) res = "left";
61-
else if (x - width / 2 < 0) res = "right";
62-
}
63-
64-
return res;
65-
}
66-
6740
export function getEventNameFromTooltipEvent(eventName: keyof TooltipEventFunctions): "mouseover" | "click" | "doubleclick" {
6841
if (eventName.endsWith("Doubleclick")) {
6942
return "doubleclick";
@@ -109,14 +82,19 @@ export function getOffset(target: OgmaNode | Edge | "background", zoom: number,
10982
// Get the offset of the layer based on the placement
11083
const offsetAmount = getOffsetAmount(target, zoom);
11184
const offset = { x: 0, y: 0 };
112-
if (placement === "top") {
113-
offset.y = -offsetAmount;
114-
} else if (placement === "bottom") {
115-
offset.y = offsetAmount;
116-
} else if (placement === "left") {
117-
offset.x = -offsetAmount;
118-
} else if (placement === "right") {
119-
offset.x = offsetAmount;
85+
switch (placement) {
86+
case "top":
87+
offset.y = -offsetAmount;
88+
break;
89+
case "bottom":
90+
offset.y = offsetAmount;
91+
break;
92+
case "left":
93+
offset.x = -offsetAmount;
94+
break;
95+
case "right":
96+
offset.x = offsetAmount;
97+
break;
12098
}
12199
return offset;
122100
}

src/uuid.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/unit/geo.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import OgmaLib from "@linkurious/ogma";
2+
import { Ogma } from "../../src";
3+
import { createRef } from "react";
4+
import { Geo } from "../../src";
5+
import { render } from "./utils";
6+
7+
import * as L from "leaflet";
8+
OgmaLib.libraries["leaflet"] = L;
9+
10+
describe("Geo Mode", () => {
11+
it("enables geo mode when enabled prop is true", () => {
12+
const ref = createRef<OgmaLib>();
13+
render(
14+
<Ogma ref={ref}>
15+
<Geo enabled />
16+
</Ogma>
17+
);
18+
expect(ref.current?.geo.enabled()).toBe(true);
19+
});
20+
});

test/unit/ogma.test.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ describe("Ogma", () => {
2222
let div: HTMLDivElement;
2323
beforeEach(() => (div = document.createElement("div")));
2424

25-
it("Ogma container renders without crashing", () => {
25+
it("renders without crashing", () => {
2626
render(<Ogma graph={graph} />, div);
2727
});
2828

29-
it("Supports ref interface", () => {
29+
it("supports ref interface", () => {
3030
const ref = React.createRef<OgmaLib>();
3131
render(<Ogma ref={ref} graph={graph} />, div);
3232
expect(ref.current).toBeDefined();
3333
expect(ref.current).toBeInstanceOf(OgmaLib);
3434
});
3535

36-
it("Ogma container renders with onReady callback", () => {
36+
it("renders with onReady callback", () => {
3737
return new Promise((resolve) => {
3838
render(<Ogma graph={graph} onReady={(ogma) => resolve(ogma)} />, div);
3939
}).then((ogma) => {
4040
expect(ogma).toBeInstanceOf(OgmaLib);
4141
});
4242
});
4343

44-
it("Ogma container renders and takes options", () => {
44+
it("renders and takes options", () => {
4545
const backgroundColor = "red";
4646
const minimumWidth = 500;
4747
return new Promise((resolve) => {
@@ -62,7 +62,23 @@ describe("Ogma", () => {
6262
});
6363
});
6464

65-
it("Ogma container passes the ogma instance to children", () => {
65+
it("supports options changes", () => {
66+
const backgroundColor = "red";
67+
const ref = React.createRef<OgmaLib>();
68+
const { rerender } = render(
69+
<Ogma ref={ref} graph={graph} options={{ backgroundColor }} />,
70+
div
71+
);
72+
73+
expect(ref.current?.getOptions().backgroundColor).toBe(backgroundColor);
74+
75+
const newBackgroundColor = "blue";
76+
rerender(<Ogma ref={ref} graph={graph} options={{ backgroundColor: newBackgroundColor }} />);
77+
78+
expect(ref.current?.getOptions().backgroundColor).toBe(newBackgroundColor);
79+
});
80+
81+
it("passes the ogma instance to children", () => {
6682
return new Promise((resolve) => {
6783
const Component = () => {
6884
const ogma = useOgma();

0 commit comments

Comments
 (0)