【发布时间】:2022-01-22 04:15:32
【问题描述】:
考虑以下Greeter.tsx React 组件:
import React from "react";
export const Greeter: React.FC = () => {
return <p>Hello, {Office.context.mailbox.userProfile.displayName}</p>;
};
以及以下Greeter.test.tsx 测试套件:
import React from "react";
import { render } from "@testing-library/react";
import { OfficeMockObject } from "office-addin-mock";
import { Greeter } from "./Greeter";
const mockData = {
context: {
mailbox: {
userProfile: {
displayName: "John Doe",
},
},
},
};
const officeMock = new OfficeMockObject(mockData);
global.Office = officeMock;
describe("Greeter", () => {
it("greets the current user", () => {
const { getByText } = render(<Greeter />);
expect(getByText(/Hello, John Doe/)).toBeInTheDocument();
});
});
我的测试失败了,因为Office.context.mailbox.userProfile.displayName 属性产生了字符串Error, property was not loaded。
进一步检查模拟对象,看起来我的预期值确实保存在模拟对象中但没有使用。
我在office-addin-mock 模块中做了一些进一步的挖掘,似乎需要事先同步才能将this.loaded 属性设置为true,因此将this.valueBeforeLoaded 分配给this.value。以下是office-addin-mock 包的摘录:
async sync() {
this.properties.forEach(async (property: OfficeMockObject, key: string) => {
await property.sync();
this.updatePropertyCall(key);
});
if (this.loaded) {
this.value = this.valueBeforeLoaded;
}
}
我的理解是context.sync 是 Excel Web 插件开发中普遍存在的概念,但 Outlook 不是。然而,此时我停止了调查,因为微软可能更容易进一步挖掘。非常感谢任何帮助!
【问题讨论】:
标签: outlook jestjs mocking office-js outlook-web-addins