Advertisement
Guest User

Untitled

a guest
Jan 28th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Hand from '../Hand';
  2. const TEST_IMG_URL = 'https://deckofcardsapi.com/static/img/back.png';
  3.  
  4. const aceCard: Card = {
  5.   value: 'ACE',
  6.   suit: 'HEARTS',
  7.   imageURL: TEST_IMG_URL
  8. };
  9.  
  10. const queenCard: Card = {
  11.   value: 'QUEEN',
  12.   suit: 'HEARTS',
  13.   imageURL: TEST_IMG_URL
  14. };
  15.  
  16. const kingCard: Card = {
  17.   value: 'KING',
  18.   suit: 'HEARTS',
  19.   imageURL: TEST_IMG_URL
  20. };
  21.  
  22. const jackCard: Card = {
  23.   value: 'JACK',
  24.   suit: 'SPADES',
  25.   imageURL: TEST_IMG_URL
  26. };
  27.  
  28. const threeCard: Card = {
  29.   value: '3',
  30.   suit: 'CLUBS',
  31.   imageURL: TEST_IMG_URL
  32. };
  33.  
  34. const twoCard: Card = {
  35.   value: '2',
  36.   suit: 'DIAMONDS',
  37.   imageURL: TEST_IMG_URL
  38. };
  39. const fiveCard: Card = {
  40.   value: '5',
  41.   suit: 'HEARTS',
  42.   imageURL: TEST_IMG_URL
  43. };
  44.  
  45. const tenCard: Card = {
  46.   value: '10',
  47.   suit: 'CLUBS',
  48.   imageURL: TEST_IMG_URL
  49. }
  50.  
  51. describe('isBusted', () => {
  52.   it('busts after drawing 1 king, 1 queen, and 1 jack as total > 21', () => {
  53.     const hand = new Hand();
  54.     hand.addCard(kingCard);
  55.     hand.addCard(queenCard);
  56.     hand.addCard(jackCard);
  57.     expect(hand.totalPoints).toEqual(30);
  58.     expect(hand.isBusted()).toEqual(true);
  59.   });
  60.  
  61.   it('does not bust after drawing 1 king and 1 queen as total <= 21', () => {
  62.     const hand = new Hand();
  63.     hand.addCard(kingCard);
  64.     hand.addCard(queenCard);
  65.     expect(hand.totalPoints).toEqual(20);
  66.     expect(hand.isBusted()).toEqual(false);
  67.   });
  68. });
  69.  
  70. describe('addCard', () => {
  71.   it('results in a count of cards and total points matching the cards added', () => {
  72.     const hand = new Hand();
  73.     hand.addCard(threeCard);
  74.     hand.addCard(jackCard);
  75.     expect(hand.cards.length).toEqual(2);
  76.   });
  77. });
  78.  
  79. describe('addCards', () => {
  80.   it('results in a count of cards and total points matching the cards added', () => {
  81.     const hand = new Hand();
  82.     hand.addCards([threeCard, jackCard]);
  83.     expect(hand.cards.length).toEqual(2);
  84.   });
  85. });
  86.  
  87. /* These are the same examples given in the README.md */
  88. describe('ace value', () => {
  89.   it('is worth 1 point  to add up to 21 if the player has a Jack and a Queen, and then draws an Ace', () => {
  90.     const hand = new Hand();
  91.     hand.addCard(queenCard);
  92.     hand.addCard(jackCard);
  93.     expect(hand.totalPoints).toEqual(20);
  94.     hand.addCard(aceCard);
  95.     expect(hand.totalPoints).toEqual(21);
  96.   });
  97.  
  98.   it('If the player has a Queen and an Ace, the Ace will be worth 11 points to add up to 21', () => {
  99.     const hand = new Hand();
  100.     hand.addCard(queenCard);
  101.     hand.addCard(aceCard);
  102.     expect(hand.totalPoints).toEqual(21);
  103.   });
  104.  
  105.   it('If the player has a 2 and an Ace, the Ace will be worth 11 points to get closer to 21', () => {
  106.     const hand = new Hand();
  107.     hand.addCard(twoCard);
  108.     hand.addCard(aceCard);
  109.     expect(hand.totalPoints).toEqual(13); // 2 + 11 = 13
  110.   });
  111.  
  112.   it('If the player has a 2 and a 5, and then draws an Ace, the Ace will be worth 11 points to get closer to 21. If the player then draws a 10, the Ace will now be worth 1 point', () => {
  113.     const hand = new Hand();
  114.     hand.addCard(twoCard);
  115.     hand.addCard(fiveCard);
  116.     expect(hand.totalPoints).toEqual(7); // 2 + 5 = 7
  117.     hand.addCard(aceCard);
  118.     expect(hand.totalPoints).toEqual(18); // 2 + 5 + 11 = 18
  119.     hand.addCard(tenCard);
  120.     expect(hand.totalPoints).toEqual(18); // 2 + 5 + 1 + 10 = 18 (Ace adjusted to 1)
  121.   });
  122. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement