1

为了测试这个功能...

export async function blueBlackToggle() {
  try {
    await Excel.run(async context => {
      const range = context.workbook.getSelectedRange();
      let blueBlackToggleNum = Office.context.document.settings.get("blueBlackToggle");
      console.log(blueBlackToggleNum);
      if (blueBlackToggleNum === 0) {
        const propertiesToGet = range.getCellProperties({
          format: {
            font: {
              color: true
            }
          }
        });
        await context.sync();
        Office.context.document.settings.set(
          "blueBlackToggle",
          propertiesToGet.value[0][0].format.font.color === "#0000FF" ? 1 : 2
        );
        Office.context.document.settings.saveAsync();
      }
      Office.context.document.settings.set("blueBlackToggle", blueBlackToggleNum + 1);
      Office.context.document.settings.saveAsync();
      range.format.font.color = blueBlackToggleNum % 2 === 0 ? "blue" : "black";
      await context.sync();
    });
  } catch (error) {
    console.error(error);
  }
}

我做了这个测试套件...

import 'regenerator-runtime/runtime';
//import add in feature
import * as format from "../format";
//import mock library
import { OfficeMockObject } from "office-addin-mock";



const excelHostMockData = {
  context: {
    workbook: {
      range: {
        format:{
            font:{
                color:"#000000"
            }
        },
        address:"C2:G3",
        value:[[
            {
                format:{
                    font:{
                        color:"#000000"
                    }
                }
            }
        ]],
        getCellProperties: function(obj){
            return this;
        }
      },
      // Mock getSelectedRange method.
      getSelectedRange: function () {
        return this.range;
      },
    },
  },
  // Mock the Excel.run method.
  run: async function(callback) {
    await callback(this.context);
  },
};

const officeMockData = {
    context:{
        document:{
            settings:{
                blueBlackToggle:0,
                get: function(setting){
                    if(setting==="blueBlackToggle"){
                        return this.blueBlackToggle;
                    }
                },
                set: function(setting, value){
                    if(setting==="blueBlackToggle"){
                        this.blueBlackToggle=value;
                    }
                },
                saveAsync: function(){

                }
            }
        }
    }
}

// Create the final mock object from the seed object.
const excelMock = new OfficeMockObject(excelHostMockData);
const officeMock = new OfficeMockObject(officeMockData);

// Define and initialize the Excel object that is called in the changeCellColorYellow function.
global.Excel = excelMock;
global.Office = officeMock;


// Jest test set
describe("format.blueBlackToggle", () => {

    it("should change color to blue or black", async () => {
        await format.blueBlackToggle();        
        expect(excelMock.context.workbook.range.format.font.color).toBe("black");
        
    })

})

我得到错误

错误,未加载属性

在 _callee29$ (src/format-updated/functions/format.ts:17:15)

使用 Office.context.document.settings.get("blueBlackToggle") 时,该函数似乎没有读取 blueBlackToggleSetting,尽管我认为我正确地重新创建了 Office 对象的结构。如何构建我的 Office 模拟对象以通过正在测试的功能访问设置?在 office js 文档中,很少有模拟 office 对象进行单元测试的示例,并且没有一个示例具有设置属性。

4

1 回答 1

1

此错误已在office-addin-mock包的 1.0.4 版本中修复。我用旧版本测试了你的代码并得到了同样的错误。

但是在将软件包更新到最新版本后,该错误得到了修复。

于 2021-12-20T13:51:05.370 回答