Skip to content

Commit 065f1e6

Browse files
committed
Max function tests to cover additional cases including decimals and non-numeric values
1 parent 084addb commit 065f1e6

1 file changed

Lines changed: 85 additions & 19 deletions

File tree

Sprint-1/implement/max.test.js

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,94 @@ 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");
2019

21-
// Given an array with one number
22-
// When passed to the max function
23-
// Then it should return that number
20+
describe("max()", () => {
21+
it("returns -Infinity for empty array", () =>
22+
expect(findMax([])).toBe(-Infinity));
2423

25-
// Given an array with both positive and negative numbers
26-
// When passed to the max function
27-
// Then it should return the largest number overall
24+
// Given an array with one number
25+
// When passed to the max function
26+
// Then it should return that number
2827

29-
// Given an array with just negative numbers
30-
// When passed to the max function
31-
// Then it should return the closest one to zero
28+
[
29+
{ input: [43], expected: 43 },
30+
{ input: [342], expected: 342 },
31+
{ input: [65455453], expected: 65455453 },
32+
].forEach(({ input, expected }) =>
33+
it("returns the only number in array with one number", () =>
34+
expect(findMax(input)).toBe(expected))
35+
);
3236

33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
37+
// Given an array with both positive and negative numbers
38+
// When passed to the max function
39+
// Then it should return the largest number overall
3640

37-
// Given an array with non-number values
38-
// When passed to the max function
39-
// Then it should return the max and ignore non-numeric values
41+
[
42+
{ input: [43, -4], expected: 43 },
43+
{ input: [342, -45, -768, 23], expected: 342 },
44+
{ input: [65455453, -54666, -4566, 6565], expected: 65455453 },
45+
].forEach(({ input, expected }) =>
46+
it("returns the largest number in array containing negative numbers", () =>
47+
expect(findMax(input)).toBe(expected))
48+
);
4049

41-
// Given an array with only non-number values
42-
// When passed to the max function
43-
// Then it should return the least surprising value given how it behaves for all other inputs
50+
// Given an array with just negative numbers
51+
// When passed to the max function
52+
// Then it should return the closest one to zero
53+
54+
[
55+
{ input: [-43, -4], expected: -4 },
56+
{ input: [-342, -45, -768, -23], expected: -23 },
57+
{ input: [-65455453, -54666, -4566, -6565], expected: -4566 },
58+
].forEach(({ input, expected }) =>
59+
it("returns closes number to zero in an array with only negative numbers", () =>
60+
expect(findMax(input)).toBe(expected))
61+
);
62+
63+
// Given an array with decimal numbers
64+
// When passed to the max function
65+
// Then it should return the largest decimal number
66+
67+
[
68+
{ input: [43.32, -4.1], expected: 43.32 },
69+
{ input: [342.54, -45.12, -768.76, 23.99], expected: 342.54 },
70+
{
71+
input: [65455453.4533, -54666.222, -4566.322, 6565.43],
72+
expected: 65455453.4533,
73+
},
74+
].forEach(({ input, expected }) =>
75+
it("returns the largest decimal number in an array with numbers", () =>
76+
expect(findMax(input)).toBe(expected))
77+
);
78+
79+
// Given an array with non-number values
80+
// When passed to the max function
81+
// Then it should return the max and ignore non-numeric values
82+
83+
[
84+
{ input: [0, NaN, 1], expected: 1 },
85+
{ input: [1, 2, "3", null, undefined, NaN, 4], expected: 4 },
86+
{ input: ["apple", 1, 2, 34, "banana", 4], expected: 34 },
87+
{ input: [1, "2", 3, "4", 5], expected: 5 },
88+
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 4 },
89+
{ input: [3, "apple", 1, null, 2, undefined, 4, 54], expected: 54 },
90+
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 5 },
91+
].forEach(({ input, expected }) =>
92+
it("returns max of numbers in an array containing non-numeric values", () =>
93+
expect(findMax(input)).toEqual(expected))
94+
);
95+
96+
// Given an array with only non-number values
97+
// When passed to the max function
98+
// Then it should return the least surprising value given how it behaves for all other inputs
99+
100+
[
101+
["not an array", "3", null, undefined],
102+
["apple", "banana"],
103+
["apple", null, undefined],
104+
["banana", {}],
105+
].forEach((input) =>
106+
it("returns -Infinity in array with only non-numeric values", () =>
107+
expect(findMax(input)).toBe(-Infinity))
108+
);
109+
});

0 commit comments

Comments
 (0)