OfficeDev / Office-Addin-Scripts

A set of scripts and packages that are consumed in Office add-ins projects.
MIT License
156 stars 99 forks source link

office-addin-mock does not provide member functions for ranges, worksheets, names, &c #811

Closed michayou closed 12 months ago

michayou commented 1 year ago

Look, I'm not 100% sure that this is a bug, or a lack of guidance, but when I try to use a member function from a mocked range, worksheet, or name it isn't there. If there is a way to make them happen, please let me know.

Current behavior

If I use the mock to get a range, the range does not contain, for example, getAbsoluteResizedRange. Names do not contain add, worksheets, does not contain getActiveWorksheet or getRange

Expected behavior

When I create a mock, I should have access to these functions.

Steps to Reproduce

const MockData = {
  context: {
    workbook: {
      range: {
        address: "G4",
        format: {
          fill: {},
        },
      },
      getSelectedRange: function () {
        return this.range;
      },
    },
  },
  run: async function(callback) {
    await callback(this.context);
  },
};

describe("Run", function () {
  it("Excel", async function () {
    const excelMock = new OfficeMockObject(MockData) as any;
    global.Excel = excelMock;
    let range: Excel.Range;
    await Excel.run(async (context) => {
      range = context.workbook.getSelectedRange();
      range = range.getAbsoluteResizedRange(1, 1); //this fails as getAbsoluteResizedRange is undefined 
      range.load("address, cellCount, rowCount, columnCount");

      await context.sync();
    });
  });
});

I mean, I can write my own versions of those functions, as you have for getSelectedRange but that defeats the purpose of trying to use Excel's capabilities.

millerds commented 12 months ago

Actually . . . you wrote the getSelectedRange method in the MockData object you created. The mock was not built to have all the methods and functionality that office.js has, it was created as an interface that allows you to return whatever you want in order to test your add-in code without having to run excel (and office.js doesn't work outside of an actual office application). The idea is not to test Excels capabilities, but the add-ins.

If you want to test a full interaction with Excel, then you need the full office,js running in an Excel host.