office-addin-mock

2.4.0 • Public • Published

Office-Addin-Mock

This library makes it much easier to unit test the Office JavaScript API. It works with any JavaScript unit testing framework.

For details about the library and how to use it, see Unit testing in Office Add-ins.

Installation

Install office-addin-mock in the root of an add-in project with this command:

npm i office-addin-mock --save-dev

Examples

For basic examples that use the Jest framework, see Unit testing in Office Add-ins - Examples. Below are some examples using other frameworks.

Testing with Mocha for Excel platform

import { OfficeMockObject } from "office-addin-mock";

function run() {
  try {
    await Excel.run(async (context) => {
      const range: Excel.Range = context.workbook.getSelectedRange();
      range.load("address");

      // Update the cell color
      range.format.fill.color = "yellow";

      await context.sync();
      console.log(`The range address was ${range.address}.`);
    });
  } catch (error) {
    console.error(error);
  }
}

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;
    await run();
    assert.strictEqual(excelMock.context.workbook.range.format.fill.color, "yellow");
  });
});

Testing a function with Mocha for PowerPoint platform

import { OfficeMockObject } from "office-addin-mock";

async function run() {
  const options = { coercionType: Office.CoercionType.Text };

  await Office.context.document.setSelectedDataAsync(" ", options);
  await Office.context.document.setSelectedDataAsync("Hello World!", options);
}

const PowerPointMockData = {
  context: {
    document: {
      setSelectedDataAsync: function (data, options?) {
        this.data = data;
        this.options = options;
      },
    },
  },
  CoercionType: {
    Text: {},
  },
};

describe(`PowerPoint`, function () {
  it("Run", async function () {
    const officeMock = new OfficeMockObject(PowerPointMockData);
    global.Office = officeMock;

    await run();

    assert.strictEqual(officeMock.context.document.data, "Hello World!");
  });
});

Reference

OfficeMockObject class

Represents a mock Office object.

Constructor

The object parameter provides initial values for the mock object. (Optional) The host parameter identifies the host of the tests. (Optional)

constructor(object?: Object, host?: OfficeApp | undefined); 

Host can be any of the following:

OfficeApp {
  Excel = "excel",
  OneNote = "onenote",
  Outlook = "outlook",
  PowerPoint = "powerpoint",
  Project = "project",
  Word = "word",
}

Methods

load

Mock implementation of the load method in the application-specific Office.js APIs.

  • The propertyArgument specifies the properties that should be loaded.
load(propertyArgument: string | string[] | Object): void;
sync

Mock replacement for the sync method in the application-specific Office.js APIs.

sync(): void;

Readme

Keywords

Package Sidebar

Install

npm i office-addin-mock

Weekly Downloads

6,472

Version

2.4.0

License

MIT

Unpacked Size

48.6 kB

Total Files

26

Last publish

Collaborators

  • ms-office