-
Notifications
You must be signed in to change notification settings - Fork 10
/
objectIteration.test.js
95 lines (86 loc) · 3.08 KB
/
objectIteration.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const {
getCarMake,
isCarColorMatching,
addCar,
countCarsMadeInYear,
removeCarById,
updateCarColor,
} = require("./objectIteration");
describe("Car Data Functions", () => {
let cars;
beforeEach(() => {
cars = [
{ id: 401, make: "Toyota", model: "Camry", year: 2021, color: "Blue" },
{ id: 402, make: "Honda", model: "Civic", year: 2020, color: "Silver" },
{ id: 403, make: "Ford", model: "Mustang", year: 2019, color: "Red" },
{
id: 405,
make: "Chevrolet",
model: "Tahoe",
year: 2022,
color: "Black",
},
{ id: 406, make: "Nissan", model: "Altima", year: 2020, color: "White" },
];
});
describe("getCarMake", () => {
it("should return the make of the car", () => {
expect(getCarMake(cars[0])).toBe("Toyota");
});
it("should return the make of the car", () => {
expect(getCarMake(cars[1])).toBe("Honda");
});
});
describe("isCarColorMatching", () => {
it("should return true if car color matches the provided color", () => {
expect(isCarColorMatching(cars[0], "Blue")).toBe(true);
});
it("should return false if car color does not match the provided color", () => {
expect(isCarColorMatching(cars[0], "Red")).toBe(false);
});
it("should return true if car color matches the provided color", () => {
expect(isCarColorMatching(cars[2], "Blue")).toBe(false);
});
it("should return false if car color does not match the provided color", () => {
expect(isCarColorMatching(cars[2], "Red")).toBe(true);
});
});
describe("addCar", () => {
it("should add a new car to the array and return the updated array, with the new car as the last element", () => {
const newCar = {
id: 407,
make: "Kia",
model: "Sorento",
year: 2021,
color: "Green",
};
const { id, make, model, year, color } = newCar;
const updatedCars = addCar([...cars], id, make, model, year, color);
expect(updatedCars).toContainEqual(newCar);
expect(updatedCars.length).toBe(cars.length + 1);
1;
expect(updatedCars[updatedCars.length - 1]).toEqual(newCar);
});
});
describe("countCarsMadeInYear", () => {
it("should return the number of cars made in a specified year", () => {
expect(countCarsMadeInYear(cars, 2020)).toBe(2);
});
});
describe("removeCarById", () => {
it("should remove a car by id and return the updated array without the car", () => {
const updatedCars = removeCarById([...cars], 405);
expect(updatedCars.some((car) => car.id === 405)).toBe(false);
expect(updatedCars.length).toBe(cars.length - 1);
});
});
describe("updateCarColor", () => {
it("should update the color of the car with the given id and return the updated car", () => {
const updatedCar = updateCarColor([...cars], 401, "Yellow");
expect(updatedCar.color).toBe("Yellow");
});
it("should return 'No Car Found' if no car with the given id exists", () => {
expect(updateCarColor(cars, 404, "Yellow")).toBe("No Car Found");
});
});
});