Its been a while I am struggling with this. New to Unit testing.. I am trying to mock a unit test case for -
fetchFooter() {
client.items().type('footer_link')
.depthParameter(10)
.toObservable()
.subscribe((response) => {
this.setState({
resFooter: response.items,
resFooterLinked: response.linkedItems,
});
});
}
Where client is
import { DeliveryClient } from '@kentico/kontent-delivery';
const client = new DeliveryClient({
projectId: <projectKey>,
enableAdvancedLogging: false,
});
This is what I have written till now, but doesn't seem to work.
const sample = {
items: {},
linkedItems: {}
};//this is my response object
describe("testing client", () => {
const mockClient = {
items: jest.fn().mockReturnThis(),
type: jest.fn().mockReturnThis(),
depthParameter: jest.fn().mockReturnThis(),
toObservable: jest.fn().mockReturnThis(),
subscribe: jest.fn().mockReturnThis(),
};
const mockProps = {
client: mockClient,
};
const component = mount(
<Footer mockProps={mockProps} resFooter={true} resFooterLinked={true} />
);
describe("Component", () => {
describe("#componentDidMount", () => {
it("should mount the component and set state correctly", () => {
const mockedResponse = sample;
mockClient.subscribe.mockImplementationOnce((handler) => handler(sample));
// tslint:disable-next-line: no-string-literal
component["setState"] = jest.fn();
component.instance().fetchFooter();
expect(
mockClient.items().type().depthParameter().toObservable().subscribe
).toBeCalledWith("footer_link");
// tslint:disable-next-line: no-string-literal
expect(component["setState"]).toBeCalledWith(sample);
});
});
});
});
I am getting an error as -
expect(jest.fn()).toBeCalledWith(...expected)
Expected: "footer_link"
Number of calls: 0
Maybe I am missing some basics here. I searched for a solution for this, but unfortunately didn't find one which can work for me. Can anyone help with this? Thanks in advance