@@ -15,29 +15,71 @@ const findMax = require("./max.js");
1515// Given an empty array
1616// When passed to the max function
1717// Then it should return -Infinity
18- // Delete this test.todo and replace it with a test.
19- test . todo ( "given an empty array, returns -Infinity" ) ;
18+ // test.todo("given an empty array, returns -Infinity");
19+ it ( "returns -Infinity for an empty array" , ( ) => {
20+ expect ( findMax ( [ ] ) ) . toBe ( - Infinity ) ;
21+ } ) ;
2022
2123// Given an array with one number
2224// When passed to the max function
2325// Then it should return that number
26+ it ( "returns 10 for a single positive number" , ( ) => {
27+ expect ( findMax ( [ 10 ] ) ) . toBe ( 10 ) ;
28+ } ) ;
29+
30+ it ( "returns the largest value in a positive-number array" , ( ) => {
31+ expect ( findMax ( [ 10 , 20 , 5 ] ) ) . toBe ( 20 ) ;
32+ } ) ;
2433
2534// Given an array with both positive and negative numbers
2635// When passed to the max function
2736// Then it should return the largest number overall
37+ it ( "returns 200 for an array of positive and negative numbers" , ( ) => {
38+ expect ( findMax ( [ 10 , - 10 , 200 , - 15 ] ) ) . toEqual ( 200 ) ;
39+ } ) ;
40+
41+ it ( "returns Infinity when Infinity is the largest value" , ( ) => {
42+ expect ( findMax ( [ 10 , Infinity , - 15 ] ) ) . toBe ( Infinity ) ;
43+ } ) ;
2844
2945// Given an array with just negative numbers
3046// When passed to the max function
3147// Then it should return the closest one to zero
48+ it ( "returns -1 for an array of negative numbers" , ( ) => {
49+ expect ( findMax ( [ - 1 , - 10 , - 200 , - 15 , - 30 ] ) ) . toEqual ( - 1 ) ;
50+ } ) ;
51+
52+ it ( "returns the number for a single negative value" , ( ) => {
53+ expect ( findMax ( [ - 10 ] ) ) . toBe ( - 10 ) ;
54+ } ) ;
3255
3356// Given an array with decimal numbers
3457// When passed to the max function
3558// Then it should return the largest decimal number
59+ it ( "returns 200.99 for an array of decimal numbers" , ( ) => {
60+ expect ( findMax ( [ 1.5 , 2.5 , 200.99 , 15.01 , 30.1 ] ) ) . toBe ( 200.99 ) ;
61+ } ) ;
3662
3763// Given an array containing a value that isn't a number
3864// When passed to the max function
3965// Then it should throw Error("findMax requires an array of numbers")
66+ it ( "throws for an array of strings" , ( ) => {
67+ expect ( ( ) => findMax ( [ "Hello" , "There" , "Weekend" ] ) ) . toThrow (
68+ "findMax requires an array of numbers"
69+ ) ;
70+ } ) ;
71+
72+ it ( "throws for an array mixing strings and numbers" , ( ) => {
73+ expect ( ( ) => findMax ( [ "Hello" , 10 , 20 ] ) ) . toThrow (
74+ "findMax requires an array of numbers"
75+ ) ;
76+ } ) ;
4077
4178// Given something that isn't an array at all, such as "hey", 42 or no argument
4279// When passed to the max function
4380// Then it should throw Error("findMax requires an array of numbers")
81+ it ( "throws when the input is not an array" , ( ) => {
82+ expect ( ( ) => findMax ( "Hello" ) ) . toThrow (
83+ "findMax requires an array of numbers"
84+ ) ;
85+ } ) ;
0 commit comments