Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Office Addin Mock does not support basic API usage of .items to get array of PowerPoint.Slide #904

Open
2 tasks done
eaallen opened this issue Nov 7, 2024 · 2 comments
Open
2 tasks done

Comments

@eaallen
Copy link

eaallen commented Nov 7, 2024

Prerequisites

Please answer the following questions before submitting an issue.
YOU MAY DELETE THE PREREQUISITES SECTION.

  • I checked to make sure that this issue has not already been filed

  • I checked the documentation and found no answer

Expected behavior

I should be able to access an array PowerPoint.Slide by accessing PowerPoint.SlideCollection.items field. This works just fine outside of the tests.

example:

context.presentation.load("slides/id") 
await context.sync()
context.presentation.slides.items // an array of PowerPoint.Slide

Current behavior

The current behavior does not allow me to use the items field which is available in PowerPoint.SlideCollection, and all other collections. I see two issues:

  1. items is not included in the props argument for the context.load method in production. But it seems that the OfficeMock is executing the navigational properties to be explicitly available
  2. It seems like OfficeMock does not handle the items array well (see bellow code) from what I could tell, any items in the array do not get turned into an officeMock object.

Steps to Reproduce

run this test

const mockData = {
    context: {
        presentation: {
            slides: {
                items: [{
                    id: "a1",
                    tags: { key: "mock", value: "mock" },
                }]
            }
        },
    },
    run: async function (callback) {
        await callback(this.context);
    },
}


describe("unable to mock powerpoint object correctly", () => {
    beforeAll(() => {
        window.PowerPoint = new OfficeMockObject(mockData, "powerpoint");
    })
    test("unable to complete", async () => {
        await window.PowerPoint.run(async (context) => {
            context.presentation.load("slides/id") // ERROR: Property id needs to be present in object model before load is called
            await context.sync()
            expect(context.presentation.slides.items[0].key).toEqual("a1") 
        })
    })
})

Please provide detailed steps for reproducing the issue.

  1. copy this code in .test.js file, I use vitest but jest should work just fine.
  2. run the test
  3. you will get the following error Error: Property id needs to be present in object model before load is called

Context

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

  • Operating System: macOS 14.0 (23A344)
  • Node version: v20.16.0
  • Office version: N/A
  • Tool version: N/A
  • Office-Addin-Mock version: 2.4.5

Failure Logs

Error: Property id needs to be present in object model before load is called.
  ❯ OfficeMockObject.loadScalar node_modules/office-addin-mock/src/officeMockObject.ts:144:13
  ❯ OfficeMockObject.loadNavigational node_modules/office-addin-mock/src/officeMockObject.ts:130:34
  ❯ node_modules/office-addin-mock/src/officeMockObject.ts:106:16
  ❯ OfficeMockObject.loadMultipleProperties node_modules/office-addin-mock/src/officeMockObject.ts:105:10
  ❯ node_modules/office-addin-mock/src/officeMockObject.ts:48:12
  ❯ OfficeMockObject.load node_modules/office-addin-mock/src/officeMockObject.ts:47:16
  ❯ callback src/power-point/tools/tests/slide-locator.unit.test.js:32:34
      30| 
      31|         await window.PowerPoint.run(async (context) => {
     32|             context.presentation.load("slides/id") // Property id need…
          |                                  ^
      33|             await context.sync()
      34|             expect(context.presentation.slides.items[0].key).toEqual("…
  ❯ OfficeMockObject.run src/power-point/tools/tests/slide-locator.unit.test.js:19:15
@eaallen
Copy link
Author

eaallen commented Nov 7, 2024

I just want to add, this bug has me abandoning this tool almost entirely, which would be a real shame because I see its potential. I hope this can get resolved quickly, especially if this is an error on my side!

@eutiquiorg
Copy link

eutiquiorg commented Dec 10, 2024

A similar behavior happens for Excel. When I try to mock the items of a Worksheet collection, I get an Error, property was not loaded.

let excelMock: ExcelMock;
let startupTest: Startup;
let mockWorksheet = {
name: Sheet1
};
let excelMockData =
{
context: {
workbook: {
worksheets:
{
items: [ mockWorksheet ],
load: function (e: string) { }
}
}
},
run: async function (callback: any) {
await callback(this.context);
}
};
beforeAll(() => {
startupTest = new Startup();
});
beforeEach(() => { jest.clearAllMocks(); });
afterAll(() => {
excelMock.restore();
});
it(Startup_FindSheet, () => {
excelMock = new ExcelMock(excelMockData);
excelMock.mock();
startupTest.findSheet();
});

export default class startupHelper {
constructor() { }
public findSheet = () => {
Excel.run(async (context) => {
let sheets = context.workbook.worksheets;
sheets.load();
await context.sync();
for (const sheet of sheets.items) {
//...
}
//...
});
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants