使用 office-addin-mock 模拟属性会产生 "Error, property was not loaded"
Mocking properties using office-addin-mock yields "Error, property was not loaded"
考虑以下 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 中却没有。然而,在这一点上我停止了调查,因为微软可能更容易进一步挖掘。非常感谢任何帮助!
感谢您提供如此详细的报告!
我在我们的回购中创建了这个错误的问题来跟踪它:Office-Addin-Scripts Issues
这确实是我们代码中的一个错误。发生的事情是在 OfficeMockObject 的构造函数中分配的属性值不适用于 Outlook。
一种解决方法,虽然未实现解决方案,但在构造函数之后分配变量值:
officeMock.context.mailbox.userProfile.displayName = "John Doe";
考虑以下 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 中却没有。然而,在这一点上我停止了调查,因为微软可能更容易进一步挖掘。非常感谢任何帮助!
感谢您提供如此详细的报告! 我在我们的回购中创建了这个错误的问题来跟踪它:Office-Addin-Scripts Issues
这确实是我们代码中的一个错误。发生的事情是在 OfficeMockObject 的构造函数中分配的属性值不适用于 Outlook。 一种解决方法,虽然未实现解决方案,但在构造函数之后分配变量值:
officeMock.context.mailbox.userProfile.displayName = "John Doe";