I am fairly new to redux and even less sure how to test stuff like reducers
I have an online shop scenario where i can add stuff to the basket. I am aware I may not post enough code here but I'll see what I can do
so here is my test:
describe('ADD_TO_BASKET', () => {
it.only('adds a product to the basket', () => {
const fakeProduct = { name: 'Trainers', id: 1, price: 100 };
const action = { type: 'ADD_TO_BASKET', data: fakeProduct };
const currentState = { items: [], total: 0 };
const result = reducer(currentState, action);
expect(result.items.length).to.equal(1);
});
and I'm just testing the reducer to see what happens when I add something to the basket
problem is, I get this error: Cannot read property 'price' of undefined
and that's because in my reducer I have this:
case 'ADD_TO_BASKET': {
const items = addProductToBasket(state.items, action);
return {
items,
total: calculateTotal(items)
};
which calls calculateTotal which has price
in it, like so:
const calculateTotal = (items) => {
return items.reduce((totalPrice, basketItem) => {
const { product: { price }, quantity } = basketItem;
const total = price * quantity;
return totalPrice + total;
}, 0);
};
how do I mock/stub out this so that in my reducer I don't get these errors? another test is failing coz it can't read some
of undefined. im guessing again, I need to mock it somehow. any help??
Aucun commentaire:
Enregistrer un commentaire