Skip to content

Commit

Permalink
fix: Repair some tests related to i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
miyanokomiya committed Oct 30, 2024
1 parent abd2f6d commit f710a23
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
30 changes: 24 additions & 6 deletions src/composables/states/appCanvas/contextMenuItems.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getMenuItemsForSelectedShapes,
groupShapes,
handleContextItemEvent,
isSameContextItem,
ungroupShapes,
} from "./contextMenuItems";

Expand Down Expand Up @@ -48,16 +49,33 @@ describe("getMenuItemsForSelectedShapes", () => {
expect(getMenuItemsForSelectedShapes(ctx)).toEqual([]);

ctx.getSelectedShapeIdMap.mockReturnValue({ locked: true });
expect(getMenuItemsForSelectedShapes(ctx)).toContain(CONTEXT_MENU_ITEM_SRC.UNLOCK);
expect(getMenuItemsForSelectedShapes(ctx)).not.toContain(CONTEXT_MENU_ITEM_SRC.LOCK);
expect(getMenuItemsForSelectedShapes(ctx).some((a) => isSameContextItem(a, CONTEXT_MENU_ITEM_SRC.UNLOCK))).toBe(
true,
);
expect(getMenuItemsForSelectedShapes(ctx).some((a) => isSameContextItem(a, CONTEXT_MENU_ITEM_SRC.LOCK))).toBe(
false,
);

ctx.getSelectedShapeIdMap.mockReturnValue({ unlocked: true });
expect(getMenuItemsForSelectedShapes(ctx)).not.toContain(CONTEXT_MENU_ITEM_SRC.UNLOCK);
expect(getMenuItemsForSelectedShapes(ctx)).toContain(CONTEXT_MENU_ITEM_SRC.LOCK);
expect(getMenuItemsForSelectedShapes(ctx).some((a) => isSameContextItem(a, CONTEXT_MENU_ITEM_SRC.UNLOCK))).toBe(
false,
);
expect(getMenuItemsForSelectedShapes(ctx).some((a) => isSameContextItem(a, CONTEXT_MENU_ITEM_SRC.LOCK))).toBe(true);

ctx.getSelectedShapeIdMap.mockReturnValue({ locked: true, unlocked: true });
expect(getMenuItemsForSelectedShapes(ctx)).toContain(CONTEXT_MENU_ITEM_SRC.UNLOCK);
expect(getMenuItemsForSelectedShapes(ctx)).toContain(CONTEXT_MENU_ITEM_SRC.LOCK);
expect(getMenuItemsForSelectedShapes(ctx).some((a) => isSameContextItem(a, CONTEXT_MENU_ITEM_SRC.UNLOCK))).toBe(
true,
);
expect(getMenuItemsForSelectedShapes(ctx).some((a) => isSameContextItem(a, CONTEXT_MENU_ITEM_SRC.LOCK))).toBe(true);
});
});

describe("isSameContextItem", () => {
test("should return true when two items are same ones", () => {
expect(isSameContextItem(CONTEXT_MENU_ITEM_SRC.LOCK, CONTEXT_MENU_ITEM_SRC.LOCK)).toBe(true);
expect(isSameContextItem(CONTEXT_MENU_ITEM_SRC.LOCK, CONTEXT_MENU_ITEM_SRC.UNLOCK)).toBe(false);
expect(isSameContextItem(CONTEXT_MENU_ITEM_SRC.LOCK, { separator: true })).toBe(false);
expect(isSameContextItem({ separator: true }, { separator: true })).toBe(true);
});
});

Expand Down
14 changes: 13 additions & 1 deletion src/composables/states/appCanvas/contextMenuItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getPatchByLayouts } from "../../shapeLayoutHandler";
import { newShapeRenderer } from "../../shapeRenderer";
import { newShapeSVGRenderer } from "../../shapeSVGRenderer";
import { TransitionValue } from "../core";
import { ContextMenuItem } from "../types";
import { ContextMenuItem, ContextMenuSeparatorItem } from "../types";
import { AppCanvasStateContext, ContextMenuItemEvent } from "./core";
import { IRectangle } from "okageo";
import { getIntRectFromFloatRect } from "../../../utils/geometry";
Expand Down Expand Up @@ -137,6 +137,18 @@ export const CONTEXT_MENU_ITEM_SRC = {
SEPARATOR: { separator: true },
} satisfies { [key: string]: ContextMenuItem };

export function isContextSeparatorItem(item: ContextMenuItem): item is ContextMenuSeparatorItem {
return "separator" in item;
}

export function isSameContextItem(a: ContextMenuItem, b: ContextMenuItem): boolean {
const isSeparatorA = isContextSeparatorItem(a);
const isSeparatorB = isContextSeparatorItem(b);
if (isSeparatorA && isSeparatorB) return true;
if (!isSeparatorA && !isSeparatorB) return a.key === b.key;
return false;
}

export function getMenuItemsForSelectedShapes(
ctx: Pick<AppCanvasStateContext, "getSelectedShapeIdMap" | "getShapeComposite">,
): ContextMenuItem[] {
Expand Down
18 changes: 10 additions & 8 deletions src/composables/states/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ export type MouseOptions = {
button: number;
} & ModifierOptions;

export type ContextMenuItem =
| {
label: string;
key: string;
meta?: any;
children?: ContextMenuItem[];
}
| { separator: true };
export type ContextMenuItem = ContextMenuActionItem | ContextMenuSeparatorItem;

export type ContextMenuActionItem = {
label: string;
key: string;
meta?: any;
children?: ContextMenuItem[];
};

export type ContextMenuSeparatorItem = { separator: true };

export type CommandExam = { command?: string; title: string };

Expand Down
4 changes: 2 additions & 2 deletions src/utils/terminology.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { describe, test, expect } from "vitest";
import { parseTerminologies } from "./terminology";
import { i18n } from "../i18n";

describe("parseTerminologies", () => {
test("should parse terminology information", () => {
expect(parseTerminologies("This is [[FOLLY_SVG]] and [[unknown_key]].")).toEqual([
{ text: "This is " },
{
text: "Folly SVG",
description:
"Folly SVG contains meta data of shapes. You can restore the shapes by dropping Folly SVG to the canvas.",
description: i18n.t("term.follysvg"),
},
{ text: " and " },
{ text: "unknown_key" },
Expand Down

0 comments on commit f710a23

Please sign in to comment.