I have a function which converts plain data to yjs object.
import * as Y from "yjs";
export type YImage = Y.Text | Y.Map<string | null> | string;
export const getYImageComponentsImage = ({
image,
footnotes,
__typename,
}): Y.Map<YImage> => {
const yValue = new Y.Map<YImage>();
const yImage = new Y.Map<string | null>();
yImage.set("id", image.data?.id || null);
yImage.set("url", image.data?.attributes.url || "");
const yFootnotes = new Y.Text();
yFootnotes.insert(0, footnotes);
yValue.set("image", yImage);
yValue.set("footnotes", yFootnotes);
yValue.set("__typename", __typename);
return yValue;
};
Now I need to write jest test to verify the shape of the yjs object is correct.
import { getYImageComponentsImage } from "./getYImageComponentsImage";
const initialData = {
id: "5",
__typename: "ComponentImageComponentsImage",
image: {
data: {
id: "10",
attributes: {
url: "/mock/image/url.jpg",
alternativeText: "",
width: 1000,
height: 600,
},
},
},
footnotes: "Morbi leo risus, porta ac consectetur ac, vestibulum at eros.",
};
describe("getImageFormReadyForRevision", () => {
it("correctly converts components data of the image ready to save", async () => {
const result = getYImageComponentsImage(initialData);
expect(result.get("__typename")).toBe("ComponentImageComponentsImage");
});
});
This function actually works in my app, but in jest not. result.get("__typename")
returns undefined
. What am I doing wrong?