Tengo la siguiente prueba:
it("should call runValidation when listen_to is set", async () => { const updatedProps = { item: { ...props.item, attributes: { ...props.item.attributes, }, }, validation: { ...props.validation, setValue: jest.fn(), runValidation: jest.fn(), listenFieldValues: jest.fn(), }, }; const spyOnValidation = jest.spyOn( updatedProps.validation, "runValidation" ); render(<Average item={updatedProps.item} validation={props.validation} />); expect(spyOnValidation).toHaveBeenCalled(); });y esta es la interfaz de usuario relacionada con él:
export const Average = ({ item, validation }: ComponentProps): JSX.Element => { const { attributes, name } = item; const hide_cents = attributes?.hide_cents; const [price, setPrice] = useState<number>(Number(item.value || 0)); useEffect(() => { if (attributes?.listen_to) { console.log("validation: ", validation); setupListenData({ item, validation, callback: setPrice }); validation.runValidation(); } }, [item, attributes, validation]); ... Estoy tratando de probar que se está llamando a validation.runValidation() dentro useEffect pero la prueba está fallando: número de llamadas 0
También intenté usar waitFor pero la prueba pasa independientemente de cuántas veces han sido llamados veces haveBeenCalledTimes(3) or haveBeenCalledTimes(100)
¿Ha intentado crear una función simulada dentro de esta prueba, luego asignar la función simulada a updatedProps.validation.runValidation , luego espera que se llame a la función simulada?
it("should call runValidation when listen_to is set", () => { const mockRunValidation = jest.fn(); const updatedProps = { item: { ...props.item, attributes: { ...props.item.attributes, }, }, validation: { ...props.validation, setValue: jest.fn(), runValidation: mockRunValidation, listenFieldValues: jest.fn(), }, }; render(<Average item={updatedProps.item} validation={props.validation} />); expect(mockRunValidation).toHaveBeenCalled(); });