OfficeDev / Office-Addin-Scripts

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

office-addin-mock does not honor repeated Excel.run/load requests #810

Closed michayou closed 8 months ago

michayou commented 9 months ago

Expected behavior

If you have two instances of Excel.run where you get a range and then use range.load, the second one will not load items loaded by the first run

Current behavior

In the mock the second instance of Excel.run will contain items loaded by the first one.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

///run this test code: 

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

describe(`Run`, function () {
  it("Excel", async function () {
    await Excel.run(async (context) => {
      const range: Excel.Range = context.workbook.getSelectedRange();
      range.load("address, cellCount, rowCount, columnCount");
      expect(range.address).toEqual("G4");
      expect(range.cellCount).toEqual(1);
      expect(range.rowCount).toEqual(1);
      expect(range.columnCount).toEqual(1);
    });

    await Excel.run(async (context) => {
      const range: Excel.Range = context.workbook.getSelectedRange();
      range.load("address, cellCount");
      expect(range.address).toEqual("G4");
      expect(range.cellCount).toEqual(1);
      expect(range.rowCount).toEqual("Error, property was not loaded");  //this will fail; it will be 1
      expect(range.columnCount).toEqual("Error, property was not loaded"); //this will fail; it will be 1
    });
  });
});

now try running those two Excel.run commands in a function in an Excel plug in. range.rowCount range.columnCount will not have been loaded in the second one

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

millerds commented 9 months ago

Your example doesn't indicate how you hooked the mock data object up, but I'm assume you are running code that creates the excelMock out of the MockData object and assigning it to the global excel object. All you would have to do is create a "fresh state" by assigning the global excel object a fresh excel mock object.

The mock object is not intended to be fully functional (which would require the actual Excel host and office.js); it's meant to be an interface to allow test data to be passed and only track the load and sync calls. Being able to reset that state seems like a nice improvement. You could suggest that improvement on https://aka.ms/M365dev-suggestions or contribute to this repo yourself (we welcome community involvement).