Reputation: 61
In order to test this function...
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);
}
}
I made this testing suite...
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");
})
})
I get the error
Error, property was not loaded
at _callee29$ (src/format-updated/functions/format.ts:17:15)
The function doesn't seem to read the blueBlackToggleSetting when using Office.context.document.settings.get("blueBlackToggle"), although I think I recreated the structure of the Office object correctly. How do I structure my Office mock object to access the settings by the function being tested? There are very few examples of mocking an office object for unit testing in the office js documentation, and none of the examples have settings properties.
Upvotes: 1
Views: 528
Reputation: 76
This bug was fixed on version 1.0.4 of the office-addin-mock
package.
I tested your code with an older version and got the same bug.
But after updating the package to the latest version, the bug got fixed.
Upvotes: 1