-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParkingLot.test.js
66 lines (52 loc) · 1.92 KB
/
ParkingLot.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
import { buildParkingLot } from "../../utils/generate";
import { HOURLY_RATE } from "../Ticket";
import Vehicle from "../Vehicle";
test("builds parkingLot with floor & spots", () => {
const floorCount = 2;
const spotsCount = 15;
const parkingLot = buildParkingLot(floorCount, spotsCount);
expect(parkingLot.floors).toHaveLength(floorCount);
expect(parkingLot.floors[0].spots).toHaveLength(spotsCount);
});
describe("ParkingLost test", () => {
it("should update availability properly", () => {
const parkingLot = buildParkingLot(2, 15);
const myBike = new Vehicle("RJ14 1234");
const ticket = parkingLot.issueTicketForVehicle(myBike);
expect(ticket.vehicle).toEqual(myBike);
const floorNumber = 0;
const spot = findFreeSpotOnFloor(parkingLot, floorNumber);
parkingLot.onSpotOccupied(spot, myBike);
let messages = parkingLot.displayAvailability();
expect(messages[floorNumber]).toEqual(
"Floor 0 has 14 spots available, out of 15"
);
parkingLot.onSpotFreed(spot);
messages = parkingLot.displayAvailability();
expect(messages[floorNumber]).toEqual(
"Floor 0 has 15 spots available, out of 15"
);
});
it("should calculate charges accurately", () => {
const parkingLot = buildParkingLot(2, 15);
const myDucati = new Vehicle("RJ14 1234");
const ticket = parkingLot.issueTicketForVehicle(myDucati);
// exiting 2 hours later
const hoursLater = 2;
const exitDateTime = new Date(
ticket.entryDateTime.getTime() + hoursLater * 60 * 60 * 1000
);
const charges = parkingLot.exitVehicle(ticket, exitDateTime);
expect(charges).toEqual(HOURLY_RATE * hoursLater);
expect(ticket.charges).toEqual(charges);
});
});
// helper fn
function findFreeSpotOnFloor(parkingLot, n) {
for (let spot of parkingLot.floors[n].spots) {
if (spot.isFree()) {
return spot;
}
}
throw new Error(`Could not find free spot on floor ${n}`);
}