No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Probar Reducers

12/16
Recursos

Aportes 15

Preguntas 2

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

Bueno el curso, pero hace falta hacerlo con Hooks! No todo es Redux. 馃槂

un ejemplo simple de como probrar REMOVE_FROM_CART action

test('REMOVE_FROM_CART', () => {
    const initialState = { cart: [ProductMock] };
    const action = { type: 'REMOVE_FROM_CART', payload: ProductMock };
    const expected = { cart: [] };

    expect(reducer(initialState, action)).toEqual(expected);
  });

esta prueba es importante para una aplicacion que estoy desarrollando Gracias Oscar, Gracias Platzi

una recomendaci贸n es crear los mocks de datos en tipo de archivo json, con eso garantizamos que al momento de revisar la cobertura va ignorar esos archivos

Agradezco haber entendido Redux, antes de esta clase.

No estoy seguro de que sea necesario probar los reducers. Los que desarrollan Redux ya se encargaran de eso.

En todo caso ser铆a probar que las actions que tenemos sean las correctas.

驴Qu茅 opinan?

Mi c贸digo del REMOVE_FROM_CART

test('REMOVE_FROM_CART', () => {
    const initialState = {
      cart: [
        ProductMock,
      ],
    };
    const payload = ProductMock;
    const action = {
      type: 'REMOVE_FROM_CART',
      payload,
    };
    const expected = {
      cart: [],
    };
    expect(reducer(initialState, action)).toEqual(expected);
  });

Gracias por el curso sos d10s

As铆 qued贸 mi prueba de 鈥楻EMOVE_FROM_CART鈥:

  test('REMOVE_FROM_CART', () => {
    const payload = ProductMock;
    const initialState = {
      cart: [ProductMock],
    };
    const action = {
      type: 'REMOVE_FROM_CART',
      payload,
    };
    const expected = {
      cart: [],
    };
    expect(reducer(initialState, action)).toEqual(expected);
  });```

Ah estado algo pesado el curso, pero ah铆 vamos 馃槃

yo lo hice validando tambi茅n en el initialState el arreglo de productos

test('Retornar initialState', ()=> {
        const initialState = { cart: [], products: [] }
        expect(reducers(initialState, '')).toEqual(initialState)
    })
test('ADD_TO_CART', ()=> {
        const initialState = { cart: [], products: [] }
        const payload = ProductMock
        const action = { type: 'ADD_TO_CART', payload }

        const expected = {
            cart:[ProductMock],
            products: []
        }

        expect(reducers(initialState, action)).toEqual(expected)
    })

adicionalmente a帽ad铆 el REMOVE_FROM_CART

test('REMOVE_FROM_CART', ()=> {
        const initialState = { cart: [ProductMock], products: [] }
        const payload = ProductMock
        const action = { type: 'REMOVE_FROM_CART', payload }

        const expected = {
            cart: [],
            products: []
        }

        expect(reducers(initialState, action)).toEqual(expected)
    })

el c贸digo completo quedar铆a as铆
src/tests/reducers/reducers.test.js:

import reducers from '../../reducers'
import ProductMock from '../../__mocks__/ProductMock'

describe('Reducers', ()=> {
    
    test('Retornar initialState', ()=> {
        const initialState = { cart: [], products: [] }
        expect(reducers(initialState, '')).toEqual(initialState)
    })

    test('ADD_TO_CART', ()=> {
        const initialState = { cart: [], products: [] }
        const payload = ProductMock
        const action = { type: 'ADD_TO_CART', payload }

        const expected = {
            cart:[ProductMock],
            products: []
        }

        expect(reducers(initialState, action)).toEqual(expected)
    })

    test('REMOVE_FROM_CART', ()=> {
        const initialState = { cart: [ProductMock], products: [] }
        const payload = ProductMock
        const action = { type: 'REMOVE_FROM_CART', payload }

        const expected = {
            cart: [],
            products: []
        }

        expect(reducers(initialState, action)).toEqual(expected)
    })
})

El c贸digo de la clase con ambas acciones:

import reducer from '../../reducers';
import ProductMock from '../../__mocks__/ProductMock';

describe('Reducers', () => {
  test('Retornar Initial State', () => {
    expect(reducer({},'')).toEqual({});
  });

  test('ADD_TO_CART', () => {
    const initialState = {
      cart: [],
    };
    const action = {
      type: 'ADD_TO_CART',
      payload: ProviderMock,
    };
    const expected = {
      cart: [ ProductMock ],
    };
    expect(reducer(initialState, action)).toEqual(expected);
  });

  test('REMOVE_FROM_CART', () => {
    const initialState = {
      cart: []
    };
    const action = {
      type: 'REMOVE_FROM_CART',
      payload: ProductMock,
    };
    const expected = {
      cart: [],
    }
  });
});

it('Prueba de reducers remove', () => {
  const initialState = {
    cart: [],
  };
  const payload = ProductMock;
  const action = {
    type: 'REMOVE_FROM_CART',
    payload,
  };
  const expected = {
    cart: [],
  };
  expect(reducers(initialState, action)).toEqual(expected);
});```

Ac谩 mi implementaci贸n para REMOVE_FROM_CART como ejercicio complementario

  test('Remove from cart', () => {
    let payload = ProductMock;
    let initialState = {
      cart: []
    };
    let action = {
      type: 'REMOVE_FROM_CART',
      payload
    };
    let expected = {
      cart: []
    }
    expect(reducer(initialState, action)).toEqual(expected);
  });

Remover un Producto

test('REMOVE_TO_CART', () => {
        const initialState = {
            cart: [ProductMock]
        };
        const payload = ProductMock;
        const action = {
            type: 'REMOVE_FROM_CART',
            payload
        };
        const expected = {
            cart: [
            ]
        };
        expect(reducers(initialState, action)).toEqual(expected)
    })