I am attempting to create a mock object (ColumnApi from ag-grid) using jest and then pass it as a parameter to a function that calls the "getAllColumns" method from ColumnApi. I am not concerned with how the "getAllColumns" method functions, but I want it to return a specific array of Columns (Column[]). How can I design this object/mock to achieve both:
- passing it as a parameter to a function that requires the ColumnApi type
- mocking the return value of a function within this object
Test:
describe("test", () => {
// my unsuccessful attempts to create the mock/object:
// let columnApi: ColumnApi {};
// const spyInstance = jest.spyOn(columnApi, "getAllColumns");
// const mock = jest.fn().mockImplementation(() => "abc");
// jest.mock("@ag-grid-community/core/dist/cjs/columnController/columnApi", () => columnApi);
// const columnApiSpy = jest.spyOn(ColumnApi, "getAllColumns");
//
// jest.mock("./main", () => ({
// columnApi: ,
// }));
// let columnApiMock: jest.Mock<ColumnApi>;
const params = {
columnApi: columnApi,
}
beforeEach(async () => {
await repository.getRows(params);
});
});
"getRows" function from repository:
getRows(columnApi: ColumnsApi) {
columnApi.getAllColumns());
}