22// We will use the same function, but write tests for it using Jest in this file.
33const getCardValue = require ( "../implement/3-get-card-value" ) ;
44
5- test ( "should return 11 for Ace of Spades" , ( ) => {
6- const aceofSpades = getCardValue ( "A♠" ) ;
7- expect ( aceofSpades ) . toEqual ( 11 ) ;
8- } ) ;
5+ test ( "should return 11 for all Aces" , ( ) => {
6+ const suits = [ "♠" , "♥" , "♦" , "♣" ] ;
7+ for ( const suit of suits ) {
8+ expect ( getCardValue ( `A${ suit } ` ) ) . toEqual ( 11 ) ;
9+ }
10+ } ) ; // One array handles all suits for Aces
911
1012// Case 2: Handle Number Cards (2-10):
11- test ( "should return 7 for 7 of Hearts" , ( ) => {
12- const sevenOfHearts = getCardValue ( "7♥" ) ;
13- expect ( sevenOfHearts ) . toEqual ( 7 ) ;
14- } ) ;
13+ test ( "should return correct value for number cards (2-10)" , ( ) => {
14+ const suits = [ "♠" , "♥" , "♦" , "♣" ] ;
15+ for ( let i = 2 ; i <= 10 ; i ++ ) {
16+ for ( const suit of suits ) {
17+ expect ( getCardValue ( `${ i } ${ suit } ` ) ) . toEqual ( i ) ;
18+ }
19+ }
20+ } ) ; // Loop through numbers 2-10 for all suits; 36 test cases
1521
1622// Case 3: Handle Face Cards (J, Q, K):
17- test ( "should return 10 for King of Hearts" , ( ) => {
18- const kingOfHearts = getCardValue ( "K♥" ) ;
19- expect ( kingOfHearts ) . toEqual ( 10 ) ;
20- } ) ;
23+ test ( "should return 10 for all face cards (J, Q, K)" , ( ) => {
24+ const faceCards = [ "J" , "Q" , "K" ] ;
25+ const suits = [ "♠" , "♥" , "♦" , "♣" ] ;
26+ for ( const card of faceCards ) {
27+ for ( const suit of suits ) {
28+ expect ( getCardValue ( `${ card } ${ suit } ` ) ) . toEqual ( 10 ) ;
29+ }
30+ }
31+ } ) ; // Loop through face cards for all suits; 12 test cases
2132
22- // Case 4: Handle Ace (A):
23- test ( "should return 11 for Ace of Diamonds" , ( ) => {
24- const aceOfDiamonds = getCardValue ( "A♦" ) ;
25- expect ( aceOfDiamonds ) . toEqual ( 11 ) ;
26- } ) ;
2733
28- // Case 5: Handle Invalid Cards:
29- test ( "should return 0 for invalid card" , ( ) => {
30- const invalidCard = getCardValue ( "1♣" ) ;
31- expect ( invalidCard ) . toEqual ( 0 ) ;
32- } ) ;
34+ // Case 4: Handle Invalid Cards:
35+ test ( "should throw error for all invalid cards" , ( ) => {
36+ const invalidCards = [ "1♣" , "11♦" , "B♠" , "Z♥" , "15♣" , "" , " " ] ;
37+ for ( const card of invalidCards ) {
38+ expect ( ( ) => getCardValue ( card ) ) . toThrow ( "Invalid card rank" ) ;
39+ }
40+ } ) ; // Loop through all invalid cards; 7 test cases
0 commit comments