I'm trying to write a test for my LED hook, but it's not working. I need to test if the code is calling the Set Behavior. I'm using toHaveBeenCalled, but I'm missing something.
When I run the test shows this message:
useLED › should call setBehavior()
expect(jest.fn()).toHaveBeenCalled()
What I need to do? Please someone help me!!! Thank you.
My hook:
const useLED = (page: ICMSDataErrorPage | ICMSSuccessPage | undefined) => {
const ledService = useService<ILEDService>(LEDServiceConstants.ServiceName);
const startLEDAnimation = async () => {
await ledService.startAnimation();
};
const stopLEDAnimation = async () => {
await ledService.stopAnimation();
};
useEffect(() => {
if (page) {
const { red, green, blue, white, behavior } = page;
ledService.setColor({
red,
green,
blue,
white,
});
ledService.setBehavior(behavior);
startLEDAnimation();
}
return () => {
stopLEDAnimation();
};
}, []);
};
export default useLED;
MY TEST:
describe("useLED", () => {
let ledService : ILEDService;
let page: ICMSDataErrorPage | ICMSSuccessPage | undefined;
beforeEach(() => {
ledService = mock<ILEDService>();
page = mock<ICMSDataErrorPage | ICMSSuccessPage | undefined>();
when(useService as jest.Mock)
.calledWith(LEDServiceConstants.ServiceName)
.mockReturnValue(ledService)
});
it("should call setBehavior()", () => {
renderHook(() => useLED(page));
expect(ledService.setBehavior).toHaveBeenCalled();
})});