@@ -5,73 +5,51 @@ import { getCardValue } from "../implement/3-get-card-value.js";
55
66// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
77
8- test ( "Valid single-digit number cards return correct values" , ( ) => {
9- assert . equal ( getCardValue ( "2♠" ) , 2 ) ;
10- assert . equal ( getCardValue ( "5♥" ) , 5 ) ;
11- assert . equal ( getCardValue ( "9♦" ) , 9 ) ;
12- } ) ;
13-
14- test ( "Valid double-digit card (10) returns 10" , ( ) => {
15- assert . equal ( getCardValue ( "10♥" ) , 10 ) ;
16- assert . equal ( getCardValue ( "10♣" ) , 10 ) ;
17- } ) ;
18-
19- test ( "Ace cards return 11" , ( ) => {
20- assert . equal ( getCardValue ( "A♠" ) , 11 ) ;
21- assert . equal ( getCardValue ( "A♦" ) , 11 ) ;
22- } ) ;
23-
24- test ( "Face cards (J, Q, K) return 10" , ( ) => {
25- assert . equal ( getCardValue ( "J♣" ) , 10 ) ;
26- assert . equal ( getCardValue ( "Q♦" ) , 10 ) ;
27- assert . equal ( getCardValue ( "K♠" ) , 10 ) ;
28- } ) ;
29-
30- // Invalid Input Cases
31- test ( "Throws error for arbitrary non-card string" , ( ) => {
32- assert . throws (
33- ( ) => getCardValue ( "invalid" ) ,
34- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
35- ) ;
36- } ) ;
37-
38- test ( "Throws error for invalid ranks or suit-like characters" , ( ) => {
39- assert . throws (
40- ( ) => getCardValue ( "1♠" ) ,
41- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
42- ) ;
43- assert . throws (
44- ( ) => getCardValue ( "11♥" ) ,
45- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
46- ) ;
47- assert . throws (
48- ( ) => getCardValue ( "Z♠" ) ,
49- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
50- ) ;
51- } ) ;
52-
53- test ( "Throws error for invalid or missing suit" , ( ) => {
54- assert . throws (
55- ( ) => getCardValue ( "10" ) ,
56- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
57- ) ;
58- assert . throws (
59- ( ) => getCardValue ( "AX" ) ,
60- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
61- ) ;
62- } ) ;
63-
64- test ( "Throws error for reversed order (suit before rank)" , ( ) => {
65- assert . throws (
66- ( ) => getCardValue ( "♠A" ) ,
67- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
68- ) ;
69- } ) ;
70-
71- test ( "Throws error for empty string or non-string input" , ( ) => {
72- assert . throws (
73- ( ) => getCardValue ( "" ) ,
74- / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t /
75- ) ;
76- assert . throws ( ( ) => getCardValue ( 10 ) , / E x p e c t e d a s t r i n g / ) ;
77- } ) ;
8+ describe ( "getCardValue" , ( ) => {
9+ test ( "Valid single-digit number cards return correct values" , ( ) => {
10+ expect ( getCardValue ( "2♠" ) ) . toBe ( 2 ) ;
11+ expect ( getCardValue ( "5♥" ) ) . toBe ( 5 ) ;
12+ expect ( getCardValue ( "9♦" ) ) . toBe ( 9 ) ;
13+ } ) ;
14+
15+ test ( "Valid double-digit card (10) returns 10" , ( ) => {
16+ expect ( getCardValue ( "10♥" ) ) . toBe ( 10 ) ;
17+ expect ( getCardValue ( "10♣" ) ) . toBe ( 10 ) ;
18+ } ) ;
19+
20+ test ( "Ace cards return 11" , ( ) => {
21+ expect ( getCardValue ( "A♠" ) ) . toBe ( 11 ) ;
22+ expect ( getCardValue ( "A♦" ) ) . toBe ( 11 ) ;
23+ } ) ;
24+
25+ test ( "Face cards (J, Q, K) return 10" , ( ) => {
26+ expect ( getCardValue ( "J♣" ) ) . toBe ( 10 ) ;
27+ expect ( getCardValue ( "Q♦" ) ) . toBe ( 10 ) ;
28+ expect ( getCardValue ( "K♠" ) ) . toBe ( 10 ) ;
29+ } ) ;
30+
31+ // Robust Error-Handling Tests using Jest's toThrow with Regex
32+ test ( "Throws error for arbitrary non-card string" , ( ) => {
33+ expect ( ( ) => getCardValue ( "invalid" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
34+ } ) ;
35+
36+ test ( "Throws error for invalid ranks or suit-like characters" , ( ) => {
37+ expect ( ( ) => getCardValue ( "1♠" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
38+ expect ( ( ) => getCardValue ( "11♥" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
39+ expect ( ( ) => getCardValue ( "Z♠" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
40+ } ) ;
41+
42+ test ( "Throws error for invalid or missing suit" , ( ) => {
43+ expect ( ( ) => getCardValue ( "10" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
44+ expect ( ( ) => getCardValue ( "AX" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
45+ } ) ;
46+
47+ test ( "Throws error for reversed order (suit before rank)" , ( ) => {
48+ expect ( ( ) => getCardValue ( "♠A" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
49+ } ) ;
50+
51+ test ( "Throws error for empty string or non-string input" , ( ) => {
52+ expect ( ( ) => getCardValue ( "" ) ) . toThrow ( / E x p e c t e d a c a r d r a n k f o l l o w e d b y a s u i t / ) ;
53+ expect ( ( ) => getCardValue ( 10 ) ) . toThrow ( / E x p e c t e d a s t r i n g / ) ;
54+ } ) ;
55+ } ) ;
0 commit comments