Skip to content

Commit 2c04019

Browse files
committed
test: unit test for new validations in time trackers service
1 parent d68a781 commit 2c04019

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

backend/src/modules/time-trackers/__tests__/time-trackers.service.spec.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,63 @@ describe('TimeTrackersService', () => {
3737
expect(service).toBeDefined();
3838
});
3939

40-
it('should create timetrackers correctly', async () => {
40+
it('should create timetrackers correctly with expected timezone id', async () => {
4141
const timeTracker = new TimeTrackersStub();
4242
const timeZoneId = Intl.DateTimeFormat().resolvedOptions().timeZone;
43-
timeTrackersRepository.create.mockResolvedValue(timeTracker);
43+
const timeTrackerWithCorrectTimeZoneId = {
44+
...timeTracker,
45+
timezone_id: timeZoneId,
46+
};
47+
timeTrackersRepository.create.mockResolvedValue(
48+
timeTrackerWithCorrectTimeZoneId,
49+
);
4450
timeTrackersRepository.verifyTimeConflict.mockResolvedValue([]);
4551
const result = await service.create(timeTracker as CreateTimeTrackerDto);
4652

47-
expect(result).toEqual(timeTracker);
48-
expect(timeTrackersRepository.create).toHaveBeenCalledWith({
49-
...(timeTracker as CreateTimeTrackerDto),
50-
timezone_id: timeZoneId,
51-
});
53+
expect(result).toEqual(timeTrackerWithCorrectTimeZoneId);
54+
expect(timeTrackersRepository.create).toHaveBeenCalledWith(
55+
timeTrackerWithCorrectTimeZoneId,
56+
);
5257
expect(timeTrackersRepository.verifyTimeConflict).toHaveBeenCalledWith(
5358
timeTracker.end_date,
5459
timeTracker.start_date,
5560
);
61+
expect(timeTracker.timezone_id).not.toEqual(result.timezone_id);
62+
expect(result.timezone_id).toEqual(timeZoneId);
63+
});
64+
65+
it('should throw error when try create timetrackers with invalid dates', async () => {
66+
const timeTracker = new TimeTrackersStub({
67+
start_date: new Date('01-01-2025'),
68+
end_date: new Date('01-01-2024'),
69+
});
70+
71+
await expect(
72+
service.create(timeTracker as CreateTimeTrackerDto),
73+
).rejects.toThrow('A data de início é maior que a data de fim');
74+
expect(timeTrackersRepository.create).toHaveBeenCalledTimes(0);
75+
expect(timeTrackersRepository.verifyTimeConflict).toHaveBeenCalledTimes(0);
76+
});
77+
78+
it('should throw error when try create timetrackers with conflict dates', async () => {
79+
const timeTracker = new TimeTrackersStub({
80+
start_date: new Date('01-01-2025'),
81+
end_date: new Date('01-01-2026'),
82+
});
83+
const otherTimeTracker = new TimeTrackersStub({
84+
start_date: new Date('02-02-2025'),
85+
end_date: new Date('04-04-2025'),
86+
});
87+
timeTrackersRepository.verifyTimeConflict.mockResolvedValue([timeTracker]);
88+
89+
await expect(
90+
service.create(otherTimeTracker as CreateTimeTrackerDto),
91+
).rejects.toThrow('Já existe uma tarefa nesse intervalo de tempo');
92+
expect(timeTrackersRepository.create).toHaveBeenCalledTimes(0);
93+
expect(timeTrackersRepository.verifyTimeConflict).toHaveBeenCalledWith(
94+
otherTimeTracker.end_date,
95+
otherTimeTracker.start_date,
96+
);
5697
});
5798

5899
it('should find all timetrackers correctly', async () => {

0 commit comments

Comments
 (0)