Skip to content

Commit c454067

Browse files
committed
Simplified and moved mocks to the mock files
1 parent f706bd6 commit c454067

13 files changed

+130
-145
lines changed

prisma/seed/schedule.seed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type Prisma } from "@prisma/client";
33

44
import { prisma } from "../seed.utils";
55

6-
function getServiceTimeSlot() {
6+
function getScheduleTimeSlot() {
77
const startHour = faker.number.int({ min: 6, max: 10 }); // e.g. 6 AM to 10 AM
88
const startMinutes = faker.helpers.arrayElement([0, 15, 30, 45]);
99
const availabilitySlot = faker.number.int({ min: 6, max: 8 });
@@ -39,7 +39,7 @@ function createSchedule(day: number, serviceId: number): Prisma.ServiceScheduleC
3939
};
4040
}
4141

42-
const { startTime, endTime } = getServiceTimeSlot();
42+
const { startTime, endTime } = getScheduleTimeSlot();
4343

4444
return {
4545
day,

src/booking/booking.controller.spec.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { Test } from "@nestjs/testing";
2-
import { BookingStatus } from "@prisma/client";
32

43
import { mockUser } from "../user/user.mocks";
54
import { BookingController } from "./booking.controller";
6-
import { CreateBookingDto, UpdateBookingDto } from "./booking.dto";
7-
import { mockBooking } from "./booking.mocks";
5+
import { mockBooking, mockCreateBooking, mockUpdateBooking } from "./booking.mocks";
86
import { BookingService } from "./booking.service";
97

108
describe("BookingController", () => {
@@ -14,8 +12,8 @@ describe("BookingController", () => {
1412
const mockBookingService = {
1513
findAllBookings: jest.fn().mockResolvedValue([mockBooking, mockBooking]),
1614
findOneBooking: jest.fn().mockResolvedValue(mockBooking),
17-
createBooking: jest.fn((data: CreateBookingDto) => Promise.resolve({ ...mockBooking, ...data })),
18-
updateBooking: jest.fn((_, data: UpdateBookingDto) => Promise.resolve({ ...mockBooking, ...data })),
15+
createBooking: jest.fn((data) => Promise.resolve({ ...mockBooking, ...data })),
16+
updateBooking: jest.fn((_, data) => Promise.resolve({ ...mockBooking, ...data })),
1917
deleteBooking: jest.fn().mockResolvedValue(mockBooking)
2018
};
2119

@@ -62,29 +60,19 @@ describe("BookingController", () => {
6260

6361
describe("create", () => {
6462
it("should create a booking", async () => {
65-
const createBookingDto: CreateBookingDto = {
66-
from: new Date(),
67-
to: new Date(),
68-
status: BookingStatus.PENDING,
69-
userId: 2,
70-
serviceId: 1
71-
};
72-
const result = await controller.create(createBookingDto);
63+
const result = await controller.create(mockCreateBooking);
7364

7465
expect(bookingService.createBooking).toHaveBeenCalled();
75-
expect(result).toEqual({ ...mockBooking, ...createBookingDto });
66+
expect(result).toEqual({ ...mockBooking, ...mockCreateBooking });
7667
});
7768
});
7869

7970
describe("update", () => {
8071
it("should update a booking", async () => {
81-
const updateBookingDto: UpdateBookingDto = {
82-
status: BookingStatus.CANCELLED
83-
};
84-
const result = await controller.update(mockBooking.id, updateBookingDto);
72+
const result = await controller.update(mockBooking.id, mockUpdateBooking);
8573

8674
expect(bookingService.updateBooking).toHaveBeenCalled();
87-
expect(result).toEqual({ ...mockBooking, ...updateBookingDto });
75+
expect(result).toEqual({ ...mockBooking, ...mockUpdateBooking });
8876
});
8977
});
9078

src/booking/booking.mocks.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BookingStatus } from "@prisma/client";
22

3-
import { BookingEntity } from "./booking.dto";
3+
import { BookingEntity, CreateBookingDto, UpdateBookingDto } from "./booking.dto";
44

55
export const mockBooking: BookingEntity = {
66
id: 1,
@@ -12,3 +12,15 @@ export const mockBooking: BookingEntity = {
1212
userId: 1,
1313
serviceId: 1
1414
};
15+
16+
export const mockCreateBooking: CreateBookingDto = {
17+
from: new Date(),
18+
to: new Date(),
19+
status: BookingStatus.PENDING,
20+
userId: 2,
21+
serviceId: 1
22+
};
23+
24+
export const mockUpdateBooking: UpdateBookingDto = {
25+
status: BookingStatus.CANCELLED
26+
};

src/booking/booking.service.spec.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Test } from "@nestjs/testing";
2-
import { BookingStatus } from "@prisma/client";
32

43
import { PrismaService } from "../prisma/prisma.service";
5-
import { CreateBookingDto, UpdateBookingDto } from "./booking.dto";
6-
import { mockBooking } from "./booking.mocks";
4+
import { mockBooking, mockCreateBooking, mockUpdateBooking } from "./booking.mocks";
75
import { BookingService } from "./booking.service";
86

97
describe("BookingService", () => {
@@ -66,32 +64,22 @@ describe("BookingService", () => {
6664

6765
describe("createBooking", () => {
6866
it("should create a booking", async () => {
69-
const createBookingDto: CreateBookingDto = {
70-
from: new Date(),
71-
to: new Date(),
72-
status: BookingStatus.PENDING,
73-
userId: 2,
74-
serviceId: 1
75-
};
76-
const mockCreatedBooking = { ...mockBooking, ...createBookingDto };
67+
const mockCreatedBooking = { ...mockBooking, ...mockCreateBooking };
7768
const createBookingSpy = jest.spyOn(service, "createBooking").mockResolvedValue(mockCreatedBooking);
78-
const result = await service.createBooking(createBookingDto);
69+
const result = await service.createBooking(mockCreateBooking);
7970

80-
expect(createBookingSpy).toHaveBeenCalledWith(createBookingDto);
71+
expect(createBookingSpy).toHaveBeenCalledWith(mockCreateBooking);
8172
expect(result).toEqual(mockCreatedBooking);
8273
});
8374
});
8475

8576
describe("updateBooking", () => {
8677
it("should update a booking", async () => {
87-
const updateBookingDto: UpdateBookingDto = {
88-
status: BookingStatus.CANCELLED
89-
};
90-
const mockUpdatedBooking = { ...mockBooking, ...updateBookingDto };
78+
const mockUpdatedBooking = { ...mockBooking, ...mockUpdateBooking };
9179
const updateBookingSpy = jest.spyOn(service, "updateBooking").mockResolvedValue(mockUpdatedBooking);
92-
const result = await service.updateBooking(mockBooking.id, updateBookingDto);
80+
const result = await service.updateBooking(mockBooking.id, mockUpdateBooking);
9381

94-
expect(updateBookingSpy).toHaveBeenCalledWith(mockBooking.id, updateBookingDto);
82+
expect(updateBookingSpy).toHaveBeenCalledWith(mockBooking.id, mockUpdateBooking);
9583
expect(result).toEqual(mockUpdatedBooking);
9684
});
9785
});

src/location/location.controller.spec.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Test } from "@nestjs/testing";
22

33
import { LocationController } from "./location.controller";
4-
import { CreateLocationDto, UpdateLocationDto } from "./location.dto";
5-
import { mockLocation } from "./location.mocks";
4+
import { mockCreateLocation, mockLocation, mockUpdateLocation } from "./location.mocks";
65
import { LocationService } from "./location.service";
76

87
describe("LocationController", () => {
@@ -12,8 +11,8 @@ describe("LocationController", () => {
1211
const mockLocationService = {
1312
findAllLocations: jest.fn().mockResolvedValue([mockLocation, mockLocation]),
1413
findOneLocation: jest.fn().mockResolvedValue(mockLocation),
15-
createLocation: jest.fn((data: CreateLocationDto) => Promise.resolve({ ...mockLocation, ...data })),
16-
updateLocation: jest.fn((_, data: UpdateLocationDto) => Promise.resolve({ ...mockLocation, ...data })),
14+
createLocation: jest.fn((data) => Promise.resolve({ ...mockLocation, ...data })),
15+
updateLocation: jest.fn((_, data) => Promise.resolve({ ...mockLocation, ...data })),
1716
deleteLocation: jest.fn().mockResolvedValue(mockLocation)
1817
};
1918

@@ -60,29 +59,19 @@ describe("LocationController", () => {
6059

6160
describe("create", () => {
6261
it("should create a location", async () => {
63-
const createLocationDto: CreateLocationDto = {
64-
address: "934 Koelpin Oval",
65-
city: "Silver Spring",
66-
country: "Norway",
67-
lat: 0,
68-
lng: 0
69-
};
70-
const result = await controller.create(createLocationDto);
62+
const result = await controller.create(mockCreateLocation);
7163

7264
expect(locationService.createLocation).toHaveBeenCalled();
73-
expect(result).toEqual({ ...mockLocation, ...createLocationDto });
65+
expect(result).toEqual({ ...mockLocation, ...mockCreateLocation });
7466
});
7567
});
7668

7769
describe("update", () => {
7870
it("should update a location", async () => {
79-
const updateLocationDto: UpdateLocationDto = {
80-
city: "London"
81-
};
82-
const result = await controller.update(mockLocation.id, updateLocationDto);
71+
const result = await controller.update(mockLocation.id, mockUpdateLocation);
8372

8473
expect(locationService.updateLocation).toHaveBeenCalled();
85-
expect(result).toEqual({ ...mockLocation, ...updateLocationDto });
74+
expect(result).toEqual({ ...mockLocation, ...mockUpdateLocation });
8675
});
8776
});
8877

src/location/location.mocks.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LocationEntity } from "./location.dto";
1+
import { CreateLocationDto, LocationEntity, UpdateLocationDto } from "./location.dto";
22

33
export const mockLocation: LocationEntity = {
44
id: 1,
@@ -8,3 +8,15 @@ export const mockLocation: LocationEntity = {
88
lat: -77.4925,
99
lng: -72.1182
1010
};
11+
12+
export const mockCreateLocation: CreateLocationDto = {
13+
address: "934 Koelpin Oval",
14+
city: "Silver Spring",
15+
country: "Poland",
16+
lat: -77.4925,
17+
lng: -72.1182
18+
};
19+
20+
export const mockUpdateLocation: UpdateLocationDto = {
21+
city: "London"
22+
};

src/location/location.service.spec.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Test } from "@nestjs/testing";
22

33
import { PrismaService } from "../prisma/prisma.service";
4-
import { CreateLocationDto, UpdateLocationDto } from "./location.dto";
5-
import { mockLocation } from "./location.mocks";
4+
import { mockCreateLocation, mockLocation, mockUpdateLocation } from "./location.mocks";
65
import { LocationService } from "./location.service";
76

87
describe("LocationService", () => {
@@ -65,32 +64,22 @@ describe("LocationService", () => {
6564

6665
describe("createLocation", () => {
6766
it("should create a location", async () => {
68-
const createLocationDto: CreateLocationDto = {
69-
address: "934 Koelpin Oval",
70-
city: "Silver Spring",
71-
country: "Poland",
72-
lat: -77.4925,
73-
lng: -72.1182
74-
};
75-
const mockCreatedLocation = { ...mockLocation, ...createLocationDto };
67+
const mockCreatedLocation = { ...mockLocation, ...mockCreateLocation };
7668
const createLocationSpy = jest.spyOn(service, "createLocation").mockResolvedValue(mockCreatedLocation);
77-
const result = await service.createLocation(createLocationDto);
69+
const result = await service.createLocation(mockCreateLocation);
7870

79-
expect(createLocationSpy).toHaveBeenCalledWith(createLocationDto);
71+
expect(createLocationSpy).toHaveBeenCalledWith(mockCreateLocation);
8072
expect(result).toEqual(mockCreatedLocation);
8173
});
8274
});
8375

8476
describe("updateLocation", () => {
8577
it("should update a location", async () => {
86-
const updateLocationDto: UpdateLocationDto = {
87-
city: "London"
88-
};
89-
const mockUpdatedLocation = { ...mockLocation, ...updateLocationDto };
78+
const mockUpdatedLocation = { ...mockLocation, ...mockUpdateLocation };
9079
const updateLocationSpy = jest.spyOn(service, "updateLocation").mockResolvedValue(mockUpdatedLocation);
91-
const result = await service.updateLocation(mockLocation.id, updateLocationDto);
80+
const result = await service.updateLocation(mockLocation.id, mockUpdateLocation);
9281

93-
expect(updateLocationSpy).toHaveBeenCalledWith(mockLocation.id, updateLocationDto);
82+
expect(updateLocationSpy).toHaveBeenCalledWith(mockLocation.id, mockUpdateLocation);
9483
expect(result).toEqual(mockUpdatedLocation);
9584
});
9685
});

src/schedule/schedule.mocks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ServiceSchedule } from "@prisma/client";
2+
3+
import { UpdateScheduleDto } from "./schedule.dto";
4+
5+
export const mockSchedule: ServiceSchedule = {
6+
id: 1,
7+
day: 0,
8+
startTime: "08:00",
9+
endTime: "16:00",
10+
serviceId: 1
11+
};
12+
13+
export const mockUpdateSchedule: UpdateScheduleDto = {
14+
startTime: "09:00"
15+
};

src/service/service.controller.spec.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Test } from "@nestjs/testing";
2-
import { ServiceStatus } from "@prisma/client";
32

43
import { ServiceController } from "./service.controller";
5-
import { CreateServiceDto, UpdateServiceDto } from "./service.dto";
6-
import { mockService } from "./service.mocks";
4+
import { mockCreateService, mockService, mockUpdateService } from "./service.mocks";
75
import { ServiceService } from "./service.service";
86

97
describe("ServiceController", () => {
@@ -13,8 +11,8 @@ describe("ServiceController", () => {
1311
const mockServiceService = {
1412
findAllServices: jest.fn().mockResolvedValue([mockService, mockService]),
1513
findOneService: jest.fn().mockResolvedValue(mockService),
16-
createService: jest.fn((data: CreateServiceDto) => Promise.resolve({ ...mockService, ...data })),
17-
updateService: jest.fn((_, data: UpdateServiceDto) => Promise.resolve({ ...mockService, ...data })),
14+
createService: jest.fn((data) => Promise.resolve({ ...mockService, ...data })),
15+
updateService: jest.fn((_, data) => Promise.resolve({ ...mockService, ...data })),
1816
deleteService: jest.fn().mockResolvedValue(mockService)
1917
};
2018

@@ -43,10 +41,10 @@ describe("ServiceController", () => {
4341

4442
describe("findAll", () => {
4543
it("should get all services", async () => {
46-
const result = await controller.findOne(mockService.id);
44+
const result = await controller.findAll();
4745

48-
expect(serviceService.findOneService).toHaveBeenCalled();
49-
expect(result).toEqual(mockService);
46+
expect(serviceService.findAllServices).toHaveBeenCalled();
47+
expect(result).toEqual([mockService, mockService]);
5048
});
5149
});
5250

@@ -61,27 +59,17 @@ describe("ServiceController", () => {
6159

6260
describe("create", () => {
6361
it("should create a service", async () => {
64-
const createServiceDto: CreateServiceDto = {
65-
name: "Service name",
66-
description: "Service description",
67-
price: 19,
68-
status: ServiceStatus.ACTIVE,
69-
locationId: 1
70-
};
71-
const result = await controller.create(createServiceDto);
62+
const result = await controller.create(mockCreateService);
7263

7364
expect(serviceService.createService).toHaveBeenCalled();
74-
expect(result).toEqual({ ...mockService, ...createServiceDto });
65+
expect(result).toEqual({ ...mockService, ...mockCreateService });
7566
});
7667
});
7768

7869
describe("update", () => {
7970
it("should update a service", async () => {
80-
const updateServiceDto: UpdateServiceDto = {
81-
description: "Updated description"
82-
};
83-
const mockUpdatedService = { ...mockService, ...updateServiceDto };
84-
const result = await controller.update(mockService.id, updateServiceDto);
71+
const mockUpdatedService = { ...mockService, ...mockUpdateService };
72+
const result = await controller.update(mockService.id, mockUpdateService);
8573

8674
expect(result).toEqual(mockUpdatedService);
8775
});

src/service/service.mocks.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Service, ServiceStatus } from "@prisma/client";
1+
import { Service, ServiceSchedule, ServiceStatus } from "@prisma/client";
2+
import { mockSchedule } from "src/schedule/schedule.mocks";
3+
4+
import { CreateServiceDto, UpdateServiceDto } from "./service.dto";
25

36
export const mockService: Service = {
47
id: 1,
@@ -10,3 +13,16 @@ export const mockService: Service = {
1013
status: ServiceStatus.ACTIVE,
1114
locationId: 1
1215
};
16+
17+
export const mockCreateService: CreateServiceDto & { ServiceSchedule: Array<ServiceSchedule> } = {
18+
name: "Service name",
19+
description: "Service description",
20+
price: 19,
21+
status: ServiceStatus.ACTIVE,
22+
locationId: 1,
23+
ServiceSchedule: [mockSchedule, mockSchedule]
24+
};
25+
26+
export const mockUpdateService: UpdateServiceDto = {
27+
description: "Updated description"
28+
};

0 commit comments

Comments
 (0)