what the style for attribute javascript reactjs jestjs

javascript - the - html title attribute style



Jest espiar la funcionalidad (3)

Estoy cambiando para bromear con moca, y me pregunto si hay una manera de espiar un método de reacción. Por ejemplo, digamos que tengo el siguiente método en mi componente (Ignorar la biblioteca sdk, simplemente construye una llamada ajax jquery):

getData() { sdk.getJSON(''/someURL'').done(data => { this.setState({data}); }); }

Usando sinon probaría esto espiando el prototipo así:

it(''should call getData'', () => { sinon.spy(Component.prototype, ''getData''); mount(<Component />); expect(Component.prototype.getData.calledOnce).to.be.true; });

Esto aseguraría la cobertura del código sin burlarse del método. ¿Hay funcionalidad similar en broma?

EDITAR: Además, si esta funcionalidad no existe, ¿cuál es la mejor estrategia para probar las llamadas a la API?


En realidad puedes usar jest.spyOn jest.spyOn

Si se llama a un método cuando se crea un componente:

import { mount } from ''enzyme''; describe(''My component'', () => { it(''should call getData'', () => { const spy = jest.spyOn(Component.prototype, ''getData''); mount(<Component />); expect(Component.prototype.getData).toHaveBeenCalledTimes(1) }); })

o si lo tiene en su DOM y método de uso de enlace puede utilizar:

import { shallow } from ''enzyme''; describe(''My component'', () => { it(''should call getData'', () => { const wrapper = shallow(<Component />); const instance = wrapper.instance() const spy = jest.spyOn(instance, ''getData''); wrapper.find(''button'').simulate(''click'') expect(spy).toHaveBeenCalledTimes(1) }); })


Existe el método spyOn , que se introdujo con v19 hace algunos días, que hace exactamente lo que está buscando


Podría spyOn por el nuevo método spyOn o lo siguiente también debería funcionar bien.

it(''should call getData'', () => { Component.prototype.getData = jest.fn(Component.prototype.getData); expect(Component.prototype.getData).toBeCalled(); });