【问题标题】:Mocking properties using office-addin-mock yields "Error, property was not loaded"使用 office-addin-mock 模拟属性会产生“错误,未加载属性”
【发布时间】: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


    【解决方案1】:

    感谢您提供如此详细的报告! 我在我们的 repo 中创建了一个问题来跟踪它:Office-Addin-Scripts Issues

    这确实是我们代码中的一个错误。发生的事情是在 OfficeMockObject 的构造函数中分配的属性值不适用于 Outlook。 一种解决方法,虽然没有实现解决方案,但在构造函数之后分配变量值:

    officeMock.context.mailbox.userProfile.displayName = "John Doe";
    

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多