1+ /**
12// This statement loads the getCardValue function you wrote in the implement directory.
23// We will use the same function, but write tests for it using Jest in this file.
34const getCardValue = require("../implement/3-get-card-value");
@@ -17,4 +18,145 @@ test(`Should return 11 when given an ace card`, () => {
1718// To learn how to test whether a function throws an error as expected in Jest,
1819// please refer to the Jest documentation:
1920// https://jestjs.io/docs/expect#tothrowerror
21+ *
22+ */
23+
24+ // This statement loads the getCardValue function you wrote in the implement directory.
25+ // We will use the same function, but write tests for it using Jest in this file.
26+ const getCardValue = require ( "../implement/3-get-card-value" ) ;
27+
28+ // Test case 1: Ace cards
29+ describe ( 'Ace cards' , ( ) => {
30+ test ( 'should return 11 for Ace of Spades' , ( ) => {
31+ expect ( getCardValue ( "A♠" ) ) . toBe ( 11 ) ;
32+ } ) ;
33+
34+ test ( 'should return 11 for Ace of Hearts' , ( ) => {
35+ expect ( getCardValue ( "A♥" ) ) . toBe ( 11 ) ;
36+ } ) ;
37+
38+ test ( 'should return 11 for Ace of Diamonds' , ( ) => {
39+ expect ( getCardValue ( "A♦" ) ) . toBe ( 11 ) ;
40+ } ) ;
41+
42+ test ( 'should return 11 for Ace of Clubs' , ( ) => {
43+ expect ( getCardValue ( "A♣" ) ) . toBe ( 11 ) ;
44+ } ) ;
45+ } ) ;
46+
47+ // Test case 2: Number cards (2-10)
48+ describe ( 'Number cards' , ( ) => {
49+ // Test all number cards 2-9
50+ test . each ( [
51+ [ '2♠' , 2 ] , [ '3♥' , 3 ] , [ '4♦' , 4 ] , [ '5♣' , 5 ] ,
52+ [ '6♠' , 6 ] , [ '7♥' , 7 ] , [ '8♦' , 8 ] , [ '9♣' , 9 ]
53+ ] ) ( 'should return %i for card %s' , ( card , expectedValue ) => {
54+ expect ( getCardValue ( card ) ) . toBe ( expectedValue ) ;
55+ } ) ;
56+
57+ // Test 10 separately as it has different format
58+ test ( 'should return 10 for Ten of Spades' , ( ) => {
59+ expect ( getCardValue ( "10♠" ) ) . toBe ( 10 ) ;
60+ } ) ;
61+
62+ test ( 'should return 10 for Ten of Hearts' , ( ) => {
63+ expect ( getCardValue ( "10♥" ) ) . toBe ( 10 ) ;
64+ } ) ;
65+
66+ test ( 'should return 10 for Ten of Diamonds' , ( ) => {
67+ expect ( getCardValue ( "10♦" ) ) . toBe ( 10 ) ;
68+ } ) ;
69+
70+ test ( 'should return 10 for Ten of Clubs' , ( ) => {
71+ expect ( getCardValue ( "10♣" ) ) . toBe ( 10 ) ;
72+ } ) ;
73+ } ) ;
74+
75+ // Test case 3: Face cards (J, Q, K)
76+ describe ( 'Face cards' , ( ) => {
77+ test . each ( [
78+ [ 'J♠' , 10 ] , [ 'J♥' , 10 ] , [ 'J♦' , 10 ] , [ 'J♣' , 10 ] ,
79+ [ 'Q♠' , 10 ] , [ 'Q♥' , 10 ] , [ 'Q♦' , 10 ] , [ 'Q♣' , 10 ] ,
80+ [ 'K♠' , 10 ] , [ 'K♥' , 10 ] , [ 'K♦' , 10 ] , [ 'K♣' , 10 ]
81+ ] ) ( 'should return 10 for face card %s' , ( card , expectedValue ) => {
82+ expect ( getCardValue ( card ) ) . toBe ( expectedValue ) ;
83+ } ) ;
84+ } ) ;
85+
86+ // Test case 4: Invalid cards
87+ describe ( 'Invalid cards' , ( ) => {
88+ // Test invalid format/input types
89+ test ( 'should throw error for completely invalid card string' , ( ) => {
90+ expect ( ( ) => getCardValue ( "invalid" ) ) . toThrow ( ) ;
91+ } ) ;
92+
93+ test ( 'should throw error for empty string' , ( ) => {
94+ expect ( ( ) => getCardValue ( "" ) ) . toThrow ( ) ;
95+ } ) ;
96+
97+ test ( 'should throw error for non-string input (number)' , ( ) => {
98+ expect ( ( ) => getCardValue ( 123 ) ) . toThrow ( ) ;
99+ } ) ;
100+
101+ test ( 'should throw error for null input' , ( ) => {
102+ expect ( ( ) => getCardValue ( null ) ) . toThrow ( ) ;
103+ } ) ;
104+
105+ test ( 'should throw error for undefined input' , ( ) => {
106+ expect ( ( ) => getCardValue ( undefined ) ) . toThrow ( ) ;
107+ } ) ;
108+
109+ // Test invalid ranks
110+ test ( 'should throw error for invalid rank' , ( ) => {
111+ expect ( ( ) => getCardValue ( "Z♠" ) ) . toThrow ( ) ;
112+ } ) ;
113+
114+ test ( 'should throw error for rank that doesn\'t exist' , ( ) => {
115+ expect ( ( ) => getCardValue ( "1♥" ) ) . toThrow ( ) ;
116+ } ) ;
117+
118+ // Test invalid suits
119+ test ( 'should throw error for invalid suit symbol' , ( ) => {
120+ expect ( ( ) => getCardValue ( "A⭐" ) ) . toThrow ( ) ;
121+ } ) ;
122+
123+ test ( 'should throw error for suit without emoji' , ( ) => {
124+ expect ( ( ) => getCardValue ( "AS" ) ) . toThrow ( ) ; // Using 'S' instead of ♠
125+ } ) ;
126+
127+ // Test invalid card format
128+ test ( 'should throw error for card that\'s too short' , ( ) => {
129+ expect ( ( ) => getCardValue ( "A" ) ) . toThrow ( ) ;
130+ } ) ;
131+
132+ test ( 'should throw error for card that\'s too long' , ( ) => {
133+ expect ( ( ) => getCardValue ( "10♥♠" ) ) . toThrow ( ) ;
134+ } ) ;
135+
136+ test ( 'should throw error for invalid 3-character card' , ( ) => {
137+ expect ( ( ) => getCardValue ( "11♠" ) ) . toThrow ( ) ;
138+ } ) ;
139+
140+ test ( 'should throw error when rank and suit are reversed' , ( ) => {
141+ expect ( ( ) => getCardValue ( "♠A" ) ) . toThrow ( ) ;
142+ } ) ;
143+
144+ // Test edge cases with valid ranks but wrong format
145+ test ( 'should throw error for "10" without suit' , ( ) => {
146+ expect ( ( ) => getCardValue ( "10" ) ) . toThrow ( ) ;
147+ } ) ;
148+
149+ test ( 'should throw error for valid rank with multiple suits' , ( ) => {
150+ expect ( ( ) => getCardValue ( "A♠♥" ) ) . toThrow ( ) ;
151+ } ) ;
152+ } ) ;
153+
154+ // Test case 5: Verify all suits work correctly with a representative rank
155+ describe ( 'All suits functionality' , ( ) => {
156+ test . each ( [
157+ [ '5♠' , 5 ] , [ '5♥' , 5 ] , [ '5♦' , 5 ] , [ '5♣' , 5 ]
158+ ] ) ( 'should work correctly with suit %s' , ( card , expectedValue ) => {
159+ expect ( getCardValue ( card ) ) . toBe ( expectedValue ) ;
160+ } ) ;
161+ } ) ;
20162
0 commit comments