No tienes acceso a esta clase

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

Probar Actions

11/16
Recursos

Aportes 32

Preguntas 2

Ordenar por:

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

o inicia sesi贸n.

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

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(鈥楢ctions鈥, () => {
test(鈥榓ddToCart Action鈥, () => {
const payload = ProductMock;
const expected = {
type: 鈥楢DD_TO_CART鈥,
payload,
};
expect(actions.addToCart(payload)).toEqual(expected);
});
// reto agregar la elimacion del producto
test(鈥楻emoveFromCart Action鈥, () => {
const payload = ProductMock;
const expected = {
type: 鈥楻EMOVE_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(鈥楻emove from cart鈥, () => {
const expected = {
type: 鈥楻EMOVE_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)
    })