OfficeDev / office-js-docs-pr

Microsoft Office Add-ins Documentation
https://learn.microsoft.com/office/dev/add-ins
Creative Commons Attribution 4.0 International
404 stars 248 forks source link

example do not work in Typescript #3779

Closed Chewbee closed 1 year ago

Chewbee commented 1 year ago

Hello I am sorry to say but in TS it does not works

I do not mean the tests fails ;) that is my problem

I mean the setup, here is the code

import { setSelectedTextRange,GetFirstofSelectedShapes, GetTextFrameVerified, GetTextRangeText } from "../commands/setSelectedText";
import { OfficeMockObject } from "office-addin-mock";
import OfficeAddinMock from "office-addin-mock";

const PowerPointMockData = {
  host: "powerpoint",
  context: {
    document: {},
  },
  CoercionType: {
    Text: {},
  },
};

describe(`Tools function `, function () {

  it("GetFirstofSelectedShapes  OK", async function () {
    const officeMock = new OfficeAddinMock.OfficeMockObject(PowerPointMockData);
    global.Office = officeMock;

    expect(await GetFirstofSelectedShapes(context: PowerPoint.RequestContext)).resolves.toBeInstanceOf(PowerPoint.Shape)
  });

});

for example the import of the mock does not work it would work if i imported : OfficeMockObject but it seems OfficeAddinMock is not exported

Long story short, the doc is not very useful , please advise I know my tests are not yet well written but the code above does not compile therefore ...

FAIL  src/tests/setSelectedText.tests.ts
  ● Test suite failed to run

    src/tests/setSelectedText.tests.ts:2:10 - error TS2305: Module '"office-addin-mock"' has no exported member 'OfficeAddinMock'.

    2 import { OfficeAddinMock } from "office-addin-mock";

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

AlexJerabek commented 1 year ago

Hi @Chewbee,

I'm sorry to hear you're having issues. @samantharamon, could you please investigate?

samantharamon commented 1 year ago

Hi @Chewbee,

Thanks for reaching out. Because the module you're trying to import from doesn't have a default export (doesn't contain export default) attempting to import from it using import OfficeAddinMock from "office-addin-mock"; will not work. The equivalent of const OfficeAddinMock = require("office-addin-mock"); using import in this case would be import * as OfficeAddinMock from "office-addin-mock";.

For a shorter syntax, you can continue using import { OfficeMockObject } from "office-addin-mock"; instead.

We welcome your feedback for improvement on the doc. I'll add this task to our backlog to provide examples on the use of the import statement for cases like this.

Let us know if there's anything else we can help you with.

ghost commented 1 year ago

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

Chewbee commented 1 year ago

Thanks for this will test it hopefully today

Chewbee commented 1 year ago

Hello ,

Thanks a lot

I still have issues and cannot find answer in the docs, in the same file

import {
  setSelectedTextRange,
  GetFirstofSelectedShapes,
  GetTextFrameVerified,
  GetTextRangeText,
} from "../commands/setSelectedText";
// import { OfficeMockObject } from "office-addin-mock";
import * as OfficeAddinMock from "office-addin-mock";

// Create the seed mock object.
const PowerPointMockData = {
  context: {
    document: {},
  },
  // Mock the Office.CoercionType enum.
  CoercionType: {
    Text: {},
  },
};
//
describe("the powerpoint functions", () => {

  it("GetTextFrameVerified  OK", async () => {
    const officeMock = new OfficeAddinMock.OfficeMockObject(PowerPointMockData) as any;
    global.PowerPoint = officeMock;

    await PowerPoint.run(async (context) => {
      const shapes: PowerPoint.ShapeCollection = context.presentation.slides.getItemAt(0).shapes;
      expect(await GetTextFrameVerified(officeMock.context.document.textFrame)).resolves.toBeInstanceOf(
        PowerPoint.TextFrame
      );
      shapes.addTextBox("Hello!");
      await context.sync();
    });
  });
  //
});

this await PowerPoint.run(async (context) => {will fail with the message:

the powerpoint functions > GetTextFrameVerified  OK
-----
TypeError: PowerPoint.run is not a function

In the documentation I cannot find how to make this work ! There are only snippets

BTW I am in TypeScripts not in JS

thanks in advance

Best regards

AlexJerabek commented 1 year ago

Hi @Chewbee,

Sorry for the delayed response. The holidays mean a lot of people are out. I'll try to look into this issue soon and get back to you.

AlexJerabek commented 1 year ago

Hi @Chewbee,

The error you're getting is because PowerPoint.run is not mocked up in the mock object. The PowerPointMockData object in your code needs to replicate as much of the PowerPoint.RequestContext object as you're going to use, as well as the run function and enums.

The relevant section to copy code from in the document would be under Mocking the Office application-specific APIs. You can see in the final block of code there is a testing version of the run function:

// Create the seed mock object.
const mockData = {
...
  // Mock the Word.run function.
  run: async function(callback) {
    await callback(this.context);
  },
};

Please let me know if that helps unblock you. Additionally, if you find that there are problems you're encountering due to TypeScript or jest, I'd recommend posting on Stack Overflow with the appropriate tags. You'll reach a wider audience there and find people with lots of knowledge in areas beyond Office.js.

ghost commented 1 year ago

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

ghost commented 1 year ago

This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.