Skip to content

Instantly share code, notes, and snippets.

@mattraykowski
Created July 14, 2017 19:15
Show Gist options
  • Save mattraykowski/36c846a0d634723f2568508d9ea8c37c to your computer and use it in GitHub Desktop.
Save mattraykowski/36c846a0d634723f2568508d9ea8c37c to your computer and use it in GitHub Desktop.
Editing Card Amounts
describe('#handleCardAddition', () => {
let cards;
let editingDecklistCards;
const handleCardAddition = value => {
const cardToAdd = cards.allCards.find(c => c.name === value);
// Check to see if the card exists, if it does then add it.
if (editingDecklistCards.hasOwnProperty(cardToAdd.cardId)) {
cardToAdd.amount = 2;
editingDecklistCards[cardToAdd.cardId].amount = 2;
} else {
cardToAdd.amount = 1;
editingDecklistCards[cardToAdd.cardId] = cardId;
}
};
beforeEach(() => {
cards = {
allCards: [
{ cardId: 'card1', name: 'test card 1', amount: 1 },
{ cardId: 'card2', name: 'test card 2', amount: 1 },
{ cardId: 'card3', name: 'test card 3', amount: 0 },
],
};
editingDecklistCards = {
card1: { cardId: 'card1', name: 'test card 1', amount: 1 },
card2: { cardId: 'card2', name: 'test card 2', amount: 2 },
};
});
test('adding an existing card increments amounts to 2', () => {
handleCardAddition('test card 1');
expect(cards.allCards[0]).toHaveProperty('amount', 2);
expect(editingDecklistCards.card1.amount).toBe(2);
});
test('adding an existing card does not increment other cards', () => {
handleCardAddition('test card 1');
expect(cards.allCards[1]).toHaveProperty('amount', 0);
expect(cards.allCards[2]).toHaveProperty('amount', 0);
expect(editingDecklistCards.card2.amount).toBe(0);
expect(editingDecklistCards.card3).toBeUndefined();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment