@@ -16,28 +16,58 @@ const findMax = require("./max.js");
1616// When passed to the max function
1717// Then it should return -Infinity
1818// Delete this test.todo and replace it with a test.
19- test . todo ( "given an empty array, returns -Infinity" ) ;
20-
19+ test ( "given an empty array, returns -Infinity" , ( ) => {
20+ expect ( findMax ( [ ] ) ) . toEqual ( - Infinity ) ;
21+ } ) ;
2122// Given an array with one number
2223// When passed to the max function
2324// Then it should return that number
25+ test ( "given an array with one number, returns that number" , ( ) => {
26+ expect ( findMax ( [ 4 ] ) ) . toEqual ( 4 ) ;
27+ } ) ;
2428
2529// Given an array with both positive and negative numbers
2630// When passed to the max function
2731// Then it should return the largest number overall
32+ test ( "given an array with both positive and negative numbers, should returns the largest number overall" , ( ) => {
33+ expect ( findMax ( [ 4 , - 2 ] ) ) . toEqual ( 4 ) ;
34+ } ) ;
2835
2936// Given an array with just negative numbers
3037// When passed to the max function
3138// Then it should return the closest one to zero
39+ test ( "given an array with just negative numbers, should return the closest one to zero" , ( ) => {
40+ expect ( findMax ( [ - 2 , - 54 ] ) ) . toEqual ( - 2 ) ;
41+ } ) ;
3242
3343// Given an array with decimal numbers
3444// When passed to the max function
3545// Then it should return the largest decimal number
46+ test ( "given an array with decimal numbers, should return the largest decimal number" , ( ) => {
47+ expect ( findMax ( [ 3.5 , 10.8 ] ) ) . toEqual ( 10.8 ) ;
48+ } ) ;
3649
3750// Given an array containing a value that isn't a number
3851// When passed to the max function
3952// Then it should throw Error("findMax requires an array of numbers")
40-
53+ test ( "throws an error when an array contains a non-number" , ( ) => {
54+ expect ( ( ) => findMax ( [ 10 , NaN , 22 ] ) ) . toThrow (
55+ "findMax requires an array of numbers"
56+ ) ;
57+ } ) ;
4158// Given something that isn't an array at all, such as "hey", 42 or no argument
4259// When passed to the max function
4360// Then it should throw Error("findMax requires an array of numbers")
61+ test ( "throws an error when input is a string" , ( ) => {
62+ expect ( ( ) => findMax ( "hello" ) ) . toThrow (
63+ "findMax requires an array of numbers"
64+ ) ;
65+ } ) ;
66+
67+ test ( "throws an error when input is a number" , ( ) => {
68+ expect ( ( ) => findMax ( 42 ) ) . toThrow ( "findMax requires an array of numbers" ) ;
69+ } ) ;
70+
71+ test ( "throws an error when no argument is provided" , ( ) => {
72+ expect ( ( ) => findMax ( ) ) . toThrow ( "findMax requires an array of numbers" ) ;
73+ } ) ;
0 commit comments