@@ -4,52 +4,97 @@ const getCardValue = require("../implement/3-get-card-value");
44
55// TODO: Write tests in Jest syntax to cover all possible outcomes.
66
7- // Case 1: Ace (A)
8- test ( `Should return 11 when given an ace card` , ( ) => {
9- expect ( getCardValue ( "A♠" ) ) . toEqual ( 11 ) } ) ;
7+ // Case 1: Ace (A) - works
8+ // test(`Should return 11 when given an ace card`, () => {
9+ // expect(getCardValue("A♠")).toEqual(11);
10+ // });
11+
1012// Suggestion: Group the remaining test data into these categories:
11- // Number Cards (2-10)
12- test ( `Should return a number given 9 card` , ( ) => {
13- expect ( getCardValue ( "9♠" ) ) . toEqual ( 9 ) } ) ;
14- // Face Cards (J, Q, K)
15- test ( `Should return 10 when given a Queen card` , ( ) => {
16- expect ( getCardValue ( "Q♥" ) ) . toEqual ( 10 ) ;
13+ // Number Cards (2-10) - works
14+ // test(`Should return a number given (2-10) card`, () => {
15+ // expect(getCardValue("2♠")).toEqual(2);
16+ // expect(getCardValue("3♥")).toEqual(3);
17+ // expect(getCardValue("4♦")).toEqual(4);
18+ // expect(getCardValue("5♣")).toEqual(5);
19+ // expect(getCardValue("6♠")).toEqual(6);
20+ // expect(getCardValue("7♥")).toEqual(7);
21+ // expect(getCardValue("8♦")).toEqual(8);
22+ // expect(getCardValue("9♣")).toEqual(9);
23+ // expect(getCardValue("10♠")).toEqual(10);
24+ // });
25+
26+ // Face Cards (J, Q, K) - works
27+ // test(`Should return 10 when given a Queen, King and Jack cards`, () => {
28+ // expect(getCardValue("Q♥")).toEqual(10);
29+ // expect(getCardValue("K♦")).toEqual(10);
30+ // expect(getCardValue("J♣")).toEqual(10);
31+ // });
32+
33+ // Invalid Cards - works
34+ // test('Should throw "Invalid card" for "invalid"', () => {
35+ // expect(() => getCardValue("invalid")).toThrow("Invalid card");
36+ // expect(() => getCardValue("789")).toThrow("Invalid card");
37+ // expect(() => getCardValue("")).toThrow("Invalid card");
38+ // expect(() => getCardValue("♠♥♦♣")).toThrow("Invalid card");
39+ // expect(() => getCardValue("5$")).toThrow("Invalid card");
40+ // expect(() => getCardValue("£50")).toThrow("Invalid card");
41+ // });
42+
43+ // **Invalid Cards - this only checks the array of invalid input. I have to think about how to
44+ // use the loop here in the reverse order, what is valid or else return invalid!**
45+ // test('should throw "Invalid card" for invalid inputs', () => {
46+ // const invalidCards = ["invalid", "789", "", "♠♥♦♣", "5$", "£50"];
47+
48+ // for (const card of invalidCards) {
49+ // expect(() => getCardValue(card)).toThrow("Invalid card");
50+ // }
51+ // });
52+
53+ // Case 1: Ace (A)
54+ test ( "should return a value of 11 when given Ace cards" , ( ) => {
55+ const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
56+
57+ for ( let i = 0 ; i < validSuits . length ; i ++ ) {
58+ const card = "A" + validSuits [ i ] ;
59+ expect ( getCardValue ( card ) ) . toEqual ( 11 ) ;
60+ }
1761} ) ;
18- test ( `Should return 10 when given a King card` , ( ) => {
19- expect ( getCardValue ( "K♦" ) ) . toEqual ( 10 ) ;
62+
63+ // Suggestion: Group the remaining test data into these categories:
64+ // Case 2: Number Cards (2-10)
65+ test ( "should return the value of number cards (2-10)" , ( ) => {
66+ const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
67+ for ( let rank = 2 ; rank <= 10 ; rank ++ ) {
68+ for ( let suit of validSuits ) {
69+ const card = `${ rank } ${ suit } ` ;
70+ expect ( getCardValue ( card ) ) . toEqual ( rank ) ;
71+ }
72+ }
2073} ) ;
2174
22- test ( `Should return 10 when given a Jack card` , ( ) => {
23- expect ( getCardValue ( "J♣" ) ) . toEqual ( 10 ) ;
75+ // Case 3: Face Cards (J, Q, K)
76+ test ( "should return a value of 10 for cards (J, Q, K)" , ( ) => {
77+ const validRanks = [ "J" , "Q" , "K" ] ;
78+ const validSuits = [ "♠" , "♥" , "♦" , "♣" ] ;
79+
80+ for ( let rank of validRanks ) {
81+ for ( let suit of validSuits ) {
82+ const card = `${ rank } ${ suit } ` ;
83+ expect ( getCardValue ( card ) ) . toEqual ( 10 ) ;
84+ }
85+ }
2486} ) ;
2587
26- // Invalid Cards
88+ // Case 4: Invalid Cards
2789test ( 'Should throw "Invalid card" for "invalid"' , ( ) => {
2890 expect ( ( ) => getCardValue ( "invalid" ) ) . toThrow ( "Invalid card" ) ;
29- } ) ;
30-
31- test ( 'Should throw "Invalid card" for input "789"' , ( ) => {
3291 expect ( ( ) => getCardValue ( "789" ) ) . toThrow ( "Invalid card" ) ;
33- } ) ;
34-
35- test ( 'Should throw "Invalid card" for input ""' , ( ) => {
3692 expect ( ( ) => getCardValue ( "" ) ) . toThrow ( "Invalid card" ) ;
37- } ) ;
38-
39- test ( 'Should throw "Invalid card" for input "♠♥♦♣"' , ( ) => {
4093 expect ( ( ) => getCardValue ( "♠♥♦♣" ) ) . toThrow ( "Invalid card" ) ;
41- } ) ;
42-
43- test ( 'Should throw "Invalid card" for input "5$"' , ( ) => {
4494 expect ( ( ) => getCardValue ( "5$" ) ) . toThrow ( "Invalid card" ) ;
45- } ) ;
46-
47- test ( 'Should throw "Invalid card" for input "£50"' , ( ) => {
4895 expect ( ( ) => getCardValue ( "£50" ) ) . toThrow ( "Invalid card" ) ;
4996} ) ;
5097
51-
5298// To learn how to test whether a function throws an error as expected in Jest,
5399// please refer to the Jest documentation:
54100// https://jestjs.io/docs/expect#tothrowerror
55-
0 commit comments