No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Probar Actions

11/16
Recursos

Aportes 28

Preguntas 2

Ordenar por:

Los aportes, preguntas y respuestas son vitales para aprender en comunidad. Regístrate o inicia sesión para participar.

con esta extencion para vscode puede hacer los test mas rapidos
https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest

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);
  });
});

  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)
    })