Closed Chewbee closed 1 year ago
Hi @Chewbee,
I'm sorry to hear you're having issues. @samantharamon, could you please investigate?
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.
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!
Thanks for this will test it hopefully today
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
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.
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.
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!
This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.
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
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 ...
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.