1+ /**
2+ * Original file:
3+ *
14// 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.
36const getCardValue = require("../implement/3-get-card-value");
@@ -17,4 +20,123 @@ test(`Should return 11 when given an ace card`, () => {
1720// To learn how to test whether a function throws an error as expected in Jest,
1821// please refer to the Jest documentation:
1922// https://jestjs.io/docs/expect#tothrowerror
23+ *
24+ */
25+
26+ // Implementation
27+
28+ const getCardValue = require ( "../implement/3-get-card-value" ) ;
29+
30+ describe ( 'getCardValue' , ( ) => {
31+ // Test all valid cards
32+ describe ( 'valid cards' , ( ) => {
33+ // Test all aces
34+ test ( 'should return 11 for aces' , ( ) => {
35+ expect ( getCardValue ( 'A♠' ) ) . toBe ( 11 ) ;
36+ expect ( getCardValue ( 'A♥' ) ) . toBe ( 11 ) ;
37+ expect ( getCardValue ( 'A♦' ) ) . toBe ( 11 ) ;
38+ expect ( getCardValue ( 'A♣' ) ) . toBe ( 11 ) ;
39+ } ) ;
40+
41+ // Test all number cards
42+ test ( 'should return correct numeric value for number cards' , ( ) => {
43+ expect ( getCardValue ( '2♠' ) ) . toBe ( 2 ) ;
44+ expect ( getCardValue ( '3♥' ) ) . toBe ( 3 ) ;
45+ expect ( getCardValue ( '4♦' ) ) . toBe ( 4 ) ;
46+ expect ( getCardValue ( '5♣' ) ) . toBe ( 5 ) ;
47+ expect ( getCardValue ( '6♠' ) ) . toBe ( 6 ) ;
48+ expect ( getCardValue ( '7♥' ) ) . toBe ( 7 ) ;
49+ expect ( getCardValue ( '8♦' ) ) . toBe ( 8 ) ;
50+ expect ( getCardValue ( '9♣' ) ) . toBe ( 9 ) ;
51+ } ) ;
52+
53+ // Test 10 specifically (since it's two digits)
54+ test ( 'should return 10 for ten cards' , ( ) => {
55+ expect ( getCardValue ( '10♠' ) ) . toBe ( 10 ) ;
56+ expect ( getCardValue ( '10♥' ) ) . toBe ( 10 ) ;
57+ expect ( getCardValue ( '10♦' ) ) . toBe ( 10 ) ;
58+ expect ( getCardValue ( '10♣' ) ) . toBe ( 10 ) ;
59+ } ) ;
60+
61+ // Test all face cards
62+ test ( 'should return 10 for face cards' , ( ) => {
63+ expect ( getCardValue ( 'J♠' ) ) . toBe ( 10 ) ;
64+ expect ( getCardValue ( 'Q♥' ) ) . toBe ( 10 ) ;
65+ expect ( getCardValue ( 'K♦' ) ) . toBe ( 10 ) ;
66+ expect ( getCardValue ( 'J♣' ) ) . toBe ( 10 ) ;
67+ } ) ;
68+ } ) ;
69+
70+ // Test invalid inputs
71+ describe ( 'invalid inputs' , ( ) => {
72+ // Test invalid formats
73+ test ( 'should throw error for empty string' , ( ) => {
74+ expect ( ( ) => getCardValue ( '' ) ) . toThrow ( 'Invalid card format' ) ;
75+ } ) ;
76+
77+ test ( 'should throw error for card without suit' , ( ) => {
78+ expect ( ( ) => getCardValue ( 'A' ) ) . toThrow ( 'Invalid card format' ) ;
79+ expect ( ( ) => getCardValue ( '10' ) ) . toThrow ( 'Invalid card format' ) ;
80+ } ) ;
81+
82+ test ( 'should throw error for card with extra characters' , ( ) => {
83+ expect ( ( ) => getCardValue ( 'A♠♠' ) ) . toThrow ( 'Invalid card format' ) ;
84+ expect ( ( ) => getCardValue ( '10♠♠' ) ) . toThrow ( 'Invalid card format' ) ;
85+ } ) ;
86+
87+ // Test invalid ranks
88+ test ( 'should throw error for invalid rank' , ( ) => {
89+ expect ( ( ) => getCardValue ( '1♠' ) ) . toThrow ( 'Invalid card format' ) ;
90+ expect ( ( ) => getCardValue ( '11♠' ) ) . toThrow ( 'Invalid card format' ) ;
91+ expect ( ( ) => getCardValue ( 'B♠' ) ) . toThrow ( 'Invalid card format' ) ;
92+ expect ( ( ) => getCardValue ( 'X♠' ) ) . toThrow ( 'Invalid card format' ) ;
93+ } ) ;
94+
95+ // Test invalid suits
96+ test ( 'should throw error for invalid suit' , ( ) => {
97+ // Using similar-looking but incorrect suit symbols
98+ expect ( ( ) => getCardValue ( 'A♤' ) ) . toThrow ( 'Invalid card format' ) ; // ♤ instead of ♠
99+ expect ( ( ) => getCardValue ( 'A♡' ) ) . toThrow ( 'Invalid card format' ) ; // ♡ instead of ♥
100+ expect ( ( ) => getCardValue ( 'A♢' ) ) . toThrow ( 'Invalid card format' ) ; // ♢ instead of ♦
101+ expect ( ( ) => getCardValue ( 'A♧' ) ) . toThrow ( 'Invalid card format' ) ; // ♧ instead of ♣
102+ } ) ;
103+
104+ // Test wrong order
105+ test ( 'should throw error when suit comes before rank' , ( ) => {
106+ expect ( ( ) => getCardValue ( '♠A' ) ) . toThrow ( 'Invalid card format' ) ;
107+ expect ( ( ) => getCardValue ( '♥10' ) ) . toThrow ( 'Invalid card format' ) ;
108+ } ) ;
109+
110+ // Test invalid types
111+ test ( 'should throw error for non-string inputs' , ( ) => {
112+ expect ( ( ) => getCardValue ( 123 ) ) . toThrow ( 'Invalid card format' ) ;
113+ expect ( ( ) => getCardValue ( null ) ) . toThrow ( 'Invalid card format' ) ;
114+ expect ( ( ) => getCardValue ( undefined ) ) . toThrow ( 'Invalid card format' ) ;
115+ expect ( ( ) => getCardValue ( { } ) ) . toThrow ( 'Invalid card format' ) ;
116+ expect ( ( ) => getCardValue ( [ ] ) ) . toThrow ( 'Invalid card format' ) ;
117+ } ) ;
118+ } ) ;
119+
120+ // Test edge cases
121+ describe ( 'edge cases' , ( ) => {
122+ test ( 'should handle lowercase ranks? (assuming they should be invalid)' , ( ) => {
123+ // This test checks if the function is case-sensitive
124+ // According to spec, ranks should be uppercase
125+ expect ( ( ) => getCardValue ( 'a♠' ) ) . toThrow ( 'Invalid card format' ) ;
126+ expect ( ( ) => getCardValue ( 'j♥' ) ) . toThrow ( 'Invalid card format' ) ;
127+ } ) ;
128+
129+ test ( 'should handle whitespace' , ( ) => {
130+ // Cards shouldn't have whitespace according to spec
131+ expect ( ( ) => getCardValue ( 'A ♠' ) ) . toThrow ( 'Invalid card format' ) ;
132+ expect ( ( ) => getCardValue ( ' 10♥' ) ) . toThrow ( 'Invalid card format' ) ;
133+ } ) ;
134+
135+ test ( 'should throw error for completely invalid strings' , ( ) => {
136+ expect ( ( ) => getCardValue ( 'invalid' ) ) . toThrow ( 'Invalid card format' ) ;
137+ expect ( ( ) => getCardValue ( 'xyz' ) ) . toThrow ( 'Invalid card format' ) ;
138+ expect ( ( ) => getCardValue ( 'hello world' ) ) . toThrow ( 'Invalid card format' ) ;
139+ } ) ;
140+ } ) ;
141+ } ) ;
20142
0 commit comments