Lo hice de esta manera para poder simplificar el código un poco mas 😛
Reto completado 😄
Bienvenida
Bienvenido al Curso de Jest
¿Qué es un Test? ¿Qué tipos de Test Existen? Jest
Introducción a Jest
Preparación del entorno con Jest
Implementando pruebas para Boolean y Array
Implementando pruebas a promesas
Watch y Coverage
Usando Jest con React
Preparar proyecto
Crear mocks
Implementar provider mock
Snapshot
Probar Actions
Probar Reducers
Probar peticiones fetch
Deploy y CI con Travis
Jest + CI
Probando el proyecto antes de hacer deploy
Recapitulación y cierre
No tienes acceso a esta clase
¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera
No se trata de lo que quieres comprar, sino de quién quieres ser. Aprovecha el precio especial.
Antes: $249
Paga en 4 cuotas sin intereses
Termina en:
Oscar Barajas Tavares
Aportes 31
Preguntas 2
Lo hice de esta manera para poder simplificar el código un poco mas 😛
Reto completado 😄
Si son de pifiarle a los nombres como yo, una buena practica es poner los string referentes a las acciones en un archivo de "types"
src/types/actionsTypes.js
export const ADD_TO_CART = "'ADD_TO_CART'";
export const REMOVE_FROM_CART = 'REMOVE_FROM_CART';
luego en reducer, actions y test simplemente lo exportan
import { ADD_TO_CART, REMOVE_FROM_CART } from '../types/actionsTypes';
listo si algo sale mal o se modifica el nombre este string se encuentra definido en 1 solo lugar de esta manera evitamos errores y centralizamos las definiciones 😄
Reto:
import actions from '../../actions/';
import ProductMock from '../../__mocks__/ProductMock';
describe('Actions', () => {
const payload = ProductMock;
test('addToCart Action', () => {
const expected = {
type: 'ADD_TO_CART',
payload,
};
expect(actions.addToCart(payload)).toEqual(expected);
});
test('removeFromCart', () => {
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
});
import actions from '../../actions';
import ProductMock from '../../__mocks__/ProductMock';
describe('Actions', () => {
test('addToCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'ADD_TO_CART',
payload,
};
expect(actions.addToCart(payload)).toEqual(expected);
});
test('removeFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
});
test('removefromcart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});```
Aquà mi solución:
test('removeFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
Reto
import actions from '../../actions';
import productMock from '../../__mocks__/productMock';
describe('Actions', () => {
const payload = productMock;
test('AddToCart Action', () => {
const expected = {
type: 'ADD_TO_CART',
payload,
};
expect(actions.addToCart(payload)).toEqual(expected);
});
test('RemoveFromCart Action', () => {
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
});
para el reto
describe('Actions', ()=> {
....
test('removeToCart action', ()=> {
const payload = ProductMock
const expected = {
type: 'REMOVE_FROM_CART',
payload
}
expect(actions.removeFromCart(payload)).toEqual(expected)
})
})
Una forma de probar escenarios que son similares es con test.each
. Quedaria algo asi:
describe('Actions', () => {
const payload = ProductMock;
const actionsToTest = [
['ADD_TO_CART', actions.addToCart, payload],
['REMOVE_FROM_CART', actions.removeFromCart, payload],
];
test.each(actionsToTest)(
'should call %p correctly',
(actionType, actionToCall, expectedPayload) => {
const expected = {
type: actionType,
payload: expectedPayload,
};
expect(actionToCall(payload)).toEqual(expected);
}
);
});
Solucion al reto:
test('removeFromCart', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
Un curso de inglés urgente para el profesor Oscar!!
test("removeFromCart Action", ()=> {
const payload = ProductMock
const expected = {
type: "REMOVE_FROM_CART",
payload,
}
expect(actions.removeFromCart(payload).toEqual(expected))
})
// actions.test.js
import actions from ‘…/…/actions’;
import ProductMock from ‘…/…/mocks/ProductMock’;
describe(‘Actions’, () => {
test(‘addToCart Action’, () => {
const payload = ProductMock;
const expected = {
type: ‘ADD_TO_CART’,
payload,
};
expect(actions.addToCart(payload)).toEqual(expected);
});
// reto agregar la elimacion del producto
test(‘RemoveFromCart Action’, () => {
const payload = ProductMock;
const expected = {
type: ‘REMOVE_FROM_CART’,
payload
};
expect(actions.removeFromCart(payload)).toEqual(expected)
})
});
Reto:
RETO
import actions from '../../actions';
import ProductMock from '../../__mocks__/ProductMock';
describe('actions', () => {
test ('addToCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'ADD_TO_CART',
payload
}
expect(actions.addToCart(payload)).toEqual(expected);
});
test ('removeFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload
}
expect(actions.removeFromCart(payload)).toEqual(expected);
});
});
Reto: Remove from cart jest-React->
test(‘Remove from cart’, () => {
const expected = {
type: ‘REMOVE_FROM_CART’,
payload: ProductMock,
};
expect(actions.removeFromCart(ProductMock)).toEqual(expected);
});
reTO:
import actions from '../../actions';
import ProductMock from '../../__mocks__/ProductMock';
describe('Actions', () => {
test('PRobar addToCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'ADD_TO_CART',
payload,
};
expect(actions.addToCart(payload)).toEqual(expected);
});
test('PRobar removeFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
});
Mi código para probar removeFromCart
test('removeFromCart action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
Como solo necesita el id, solo le paso este: 😄
test('RemoveFromCart Action',()=>{
const payload = ProductMock.id;
const expected = {
type: 'REMOVE_FROM_CART',
payload
}
expect(actions.removeFromCart(payload)).toEqual(expected)
})
test('RemoveFromCart Action', () => {
const payload = ProductMock;
const expected = {
type : "REMOVE_FROM_CART",
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
test('removeFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
Challenge:
test('removeFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
Challenge:
import actions from '../../actions/'
import ProductMock from '../../__mocks__/ProductMock'
//Add To Cart
describe('Actions', () => {
test('addToCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'ADD_TO_CART',
payload,
};
expect(actions.addToCart(payload)).toEqual(expected);
});
});
//Remove From Cart
describe('Actions', () => {
test('removeFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
});```
test('Remove from card action ', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
it('Test de los actions eliminar', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
test('Remove from cart', () => {
const payload = ProviderMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload,
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
Mi solución fue similar a la de muchos pero con la diferencia que dejé la variable payload en mi scope global del describe y la usé en ambos test, asÃ:
describe('Testing actions', () => {
let payload = ProductMock;
test('Add to Cart Action', () => {
let expected = {
type: 'ADD_TO_CART',
payload
}
expect(actions.addToCart(payload)).toEqual(expected);
});
test('Remove from cart Action', () => {
let expected = {
type: 'REMOVE_FROM_CART',
payload
};
expect(actions.removeFromCart(payload)).toEqual(expected);
});
});
Mi solución
test('RemoveFromCart Action', () => {
const payload = ProductMock;
const expected = {
type: 'REMOVE_FROM_CART',
payload
};
expect(actions.removeFromCart(payload)).toEqual(expected)
})
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?