Emanuel Suriano
React developer (WIP)
Aclaración: Jest tiene manejo de funciones mockeadas, pero estas seran manejadas con Sinon
Realiza un render en sólo un nivel de profundidad del componente.
Pros:
Contras:
Realiza un Full DOM rendering del componente.
Pros:
Contras:
const Label = ({ text }) => <label>{text}</label>;
const SubmitButton = ({ text }) => <button><Label text={text} /></button>;
<button>
<Label text="Hello!" />
</button>
<button>
<label>
Hello!
</label>
</button>
Ofrece informacion sobre los llamados de las funciones: si fue llamado, cuántas veces, con qué parámetros, etc.
function myFunction(callback, number){
callback(number + 1);
}
describe('myFunction', function() {
it('should call the callback function', function() {
var callback = sinon.spy(); //create instance of spy
myFunction(callback, 10); //send as parameter
assert(callback.calledOnce); //check if it was called
assert(callback.calledWith(11)); //check with what parameters was called
});
});
Proveen una manera de reemplazar el comportamiento de una función, pudiendo retornar valores o excepciones.
const userService = {
function makeFetch(id) {
return fetch('users/' + id);
}
}
describe('myFunction', function() {
it('should call the callback function', function() {
var callback = sinon.stub(userService, 'makeFetch'); // create instance of stub
callback.callsFake((id) => ({ id, name: 'userName' })); // redefine behaviour
const result = userService.makeFetch(10);
assert(result).toEqual({ id: 10, name: 'userName' })
callback.restore(); //IMPORTANT! --> restore behaviour of userService.makeFetch
});
});
Proveen una manera de reemplazar todo un objeto facilmente, combinando spies y stubs.
function incrementStoredData(value, amount){
var total = store.get(value) || 0;
var newtotal = total + amount;
store.set(value, newtotal);
}
describe('incrementStoredData', function() {
it('should increment stored value by one - using Mock', function() {
var storeMock = sinon.mock(store); // create instance of Mock
storeMock.expects('get')
.withArgs('data')
.returns(0); //redefine what it has to returns and set validation
storeMock.expects('set')
.once()
.withArgs('data', 1); //set validation for the set function
incrementStoredData('data', 1);
// IMPORTANT!
storeMock.restore(); // restore to the default behaviour
storeMock.verify(); // this line runs all the validation from above
});
it('should increment stored value by one - using Stub and Spy', () => {
const getStub = sinon.stub(store, 'get').callsFake(() => 0);
const setSpy = sinon.spy(store, 'set');
incrementStoredData('data', 1);
assert.equal(getStub.calledWith('data'), true);
assert.equal(setSpy.calledOnce, true);
assert.equal(setSpy.calledWith('data', 1), true);
getStub.restore();
setSpy.restore();
});
});
Nos dejan "viajar en el tiempo", para probar eventos que dependan del tiempo en los componentes.
it('should check if timer ends when it has to', () => {
var clock = sinon.useFakeTimers(); // create instance of fakeTimer
var result = "timer running";
setTimeout(() => {
result = "time's out!";
}, 1000); // When 1 second has passed, result will take the value of "time's out!"
clock.tick(1000); //make 1 second passed with the fakeTimer
assert.equal(result, "time's out!");
});
By Emanuel Suriano
Introduccion de testing de componentes de React.