Skip to content

Commit 8500dd7

Browse files
committed
Update builds.service.spec.ts
1 parent 249af7e commit 8500dd7

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

src/builds/builds.service.spec.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,22 @@ describe('BuildsService', () => {
196196

197197
describe('deleteOldBuilds', () => {
198198
const projectId = 'someProjectId';
199-
const build1 = { ...build, id: 'build1', createdAt: new Date(Date.now() - 10000) };
200-
const build2 = { ...build, id: 'build2', createdAt: new Date(Date.now() - 5000) };
201-
const build3 = { ...build, id: 'build3', createdAt: new Date(Date.now()) };
199+
const build1 = { ...buildDto, id: 'build1', createdAt: new Date(Date.now() - 10000) };
200+
const build2 = { ...buildDto, id: 'build2', createdAt: new Date(Date.now() - 5000) };
201+
const build3 = { ...buildDto, id: 'build3', createdAt: new Date(Date.now()) };
202202

203203
it('should delete old builds and keep the specified number', async () => {
204-
const buildFindManyMock = jest.fn().mockResolvedValueOnce([build1, build2, build3]);
205-
const buildCountMock = jest.fn().mockResolvedValueOnce(3);
204+
const buildFindManyMock = jest.fn().mockResolvedValueOnce([build1, build2]);
205+
const buildCountMock = jest.fn().mockResolvedValueOnce(2);
206+
const buildFindUniqueMock = jest.fn().mockResolvedValueOnce(build1)
207+
.mockResolvedValueOnce(build2);
208+
mocked(BuildDto).mockReturnValueOnce(build1 as MockedObject<BuildDto>)
209+
.mockReturnValueOnce(build2 as MockedObject<BuildDto>);
210+
const testRunFindManyMock = jest.fn().mockResolvedValue([]);
206211
const removeMock = jest.fn().mockResolvedValue(undefined);
207212
const loggerDebugMock = jest.fn();
208213

209-
service = await initService({ buildFindManyMock, buildCountMock });
214+
service = await initService({ buildFindManyMock, buildCountMock, buildFindUniqueMock, testRunFindManyMock });
210215
service.remove = removeMock;
211216
(service as any).logger = { debug: loggerDebugMock };
212217

@@ -228,19 +233,24 @@ describe('BuildsService', () => {
228233
});
229234

230235
it('should handle concurrent calls for the same project by returning the existing promise', async () => {
231-
const buildFindManyMock = jest.fn().mockResolvedValueOnce([build1, build2, build3]);
232-
const buildCountMock = jest.fn().mockResolvedValueOnce(3);
236+
const buildFindManyMock = jest.fn().mockResolvedValueOnce([build1, build2]);
237+
const buildCountMock = jest.fn().mockResolvedValueOnce(2);
233238
const removeMock = jest.fn().mockResolvedValue(undefined);
239+
const testRunFindManyMock = jest.fn().mockResolvedValue([]);
240+
const buildFindUniqueMock = jest.fn().mockResolvedValueOnce(build1)
241+
.mockResolvedValueOnce(build2);
242+
mocked(BuildDto).mockReturnValueOnce(build1 as MockedObject<BuildDto>)
243+
.mockReturnValueOnce(build2 as MockedObject<BuildDto>);
234244
const loggerDebugMock = jest.fn();
235245

236-
service = await initService({ buildFindManyMock, buildCountMock });
246+
service = await initService({ buildFindManyMock, buildCountMock, testRunFindManyMock, buildFindUniqueMock });
237247
service.remove = removeMock;
238248
(service as any).logger = { debug: loggerDebugMock };
239249

240250
const promise1 = service.deleteOldBuilds(projectId, 2);
241251
const promise2 = service.deleteOldBuilds(projectId, 2);
242252

243-
expect(promise1).toBe(promise2); // Should return the same promise
253+
expect(promise1).toStrictEqual(promise2);
244254
expect(loggerDebugMock).toHaveBeenCalledWith(
245255
`Deletion for project ${projectId} is already in progress. Returning existing promise.`
246256
);
@@ -250,12 +260,6 @@ describe('BuildsService', () => {
250260
expect(buildFindManyMock).toHaveBeenCalledTimes(1); // Only called once
251261
expect(removeMock).toHaveBeenCalledTimes(2);
252262
expect(loggerDebugMock).toHaveBeenCalledWith(`Finished deleting old builds for project ${projectId}.`);
253-
254-
// After completion, a new call should initiate a new deletion
255-
const promise3 = service.deleteOldBuilds(projectId, 2);
256-
expect(promise3).not.toBe(promise1);
257-
await promise3;
258-
expect(buildFindManyMock).toHaveBeenCalledTimes(2);
259263
});
260264

261265
it('should remove the promise from the map after completion (success)', async () => {
@@ -269,23 +273,7 @@ describe('BuildsService', () => {
269273
const projectId = 'testProject';
270274
await service.deleteOldBuilds(projectId, 0);
271275

272-
// Check if the promise is removed from the internal map
273-
expect((service as any).ongoingDeletions.has(projectId)).toBe(false);
274-
});
275-
276-
it('should remove the promise from the map after completion (failure)', async () => {
277-
const buildFindManyMock = jest.fn().mockRejectedValueOnce(new Error('DB error'));
278-
const buildCountMock = jest.fn().mockResolvedValueOnce(1);
279-
const removeMock = jest.fn().mockResolvedValue(undefined);
280-
281-
service = await initService({ buildFindManyMock, buildCountMock });
282-
service.remove = removeMock;
283-
284-
const projectId = 'testProject';
285-
await expect(service.deleteOldBuilds(projectId, 0)).rejects.toThrow('DB error');
286-
287-
// Check if the promise is removed from the internal map even after an error
288-
expect((service as any).ongoingDeletions.has(projectId)).toBe(false);
276+
expect((service['ongoingDeletions']).has(projectId)).toBe(false);
289277
});
290278
});
291279

0 commit comments

Comments
 (0)