-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1303 from Domfa/feat/add-pagination-to-getallwait…
…list-route feat : implement pagination to get-all-waitlist
- Loading branch information
Showing
2 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ describe('WaitlistService', () => { | |
|
||
const mockWaitlistRepository = { | ||
find: jest.fn(), | ||
findAndCount: jest.fn(), | ||
save: jest.fn(), | ||
create: jest.fn(), | ||
findOne: jest.fn(), | ||
|
@@ -48,10 +49,67 @@ describe('WaitlistService', () => { | |
}); | ||
|
||
describe('getAllWaitlist', () => { | ||
it('should return all waitlist', async () => { | ||
await waitlistService.getAllWaitlist(); | ||
it('should return paginated waitlist results with default values when no parameters are provided', async () => { | ||
// Arrange: setup default values (page = 1, limit = 10) | ||
const mockWaitlistEntries = [ | ||
{ id: '1', full_name: 'Alice', email: '[email protected]' }, | ||
{ id: '2', full_name: 'Bob', email: '[email protected]' }, | ||
]; | ||
const totalCount = 2; | ||
mockWaitlistRepository.findAndCount = jest.fn().mockResolvedValue([mockWaitlistEntries, totalCount]); | ||
|
||
expect(waitlistRepository.find).toHaveBeenCalled(); | ||
// Act: call service without pagination parameters | ||
const result = await waitlistService.getAllWaitlist(); | ||
|
||
// Assert: defaults (skip: 0, take: 10) should be used | ||
expect(mockWaitlistRepository.findAndCount).toHaveBeenCalledWith({ | ||
skip: 0, | ||
take: 10, | ||
}); | ||
expect(result).toEqual({ | ||
status: 'Success', | ||
status_code: 200, | ||
data: { | ||
current_page: 1, | ||
total_pages: 1, // since totalCount is 2 and default limit is 10 | ||
total_waitlist_count: totalCount, | ||
waitlist: mockWaitlistEntries, | ||
}, | ||
message: 'Waitlist found successfully', | ||
}); | ||
}); | ||
|
||
it('should return paginated waitlist results with provided page and limit', async () => { | ||
// Arrange: setup custom pagination values | ||
const mockWaitlistEntries = [ | ||
{ id: '3', full_name: 'Charlie', email: '[email protected]' }, | ||
{ id: '4', full_name: 'David', email: '[email protected]' }, | ||
]; | ||
const totalCount = 20; | ||
mockWaitlistRepository.findAndCount = jest.fn().mockResolvedValue([mockWaitlistEntries, totalCount]); | ||
|
||
const page = 2; | ||
const limit = 5; | ||
|
||
// Act: call service with page and limit parameters | ||
const result = await waitlistService.getAllWaitlist(page, limit); | ||
|
||
// Assert: ensure proper skip and take values | ||
expect(mockWaitlistRepository.findAndCount).toHaveBeenCalledWith({ | ||
skip: (page - 1) * limit, // (2-1)*5 = 5 | ||
take: limit, // 5 | ||
}); | ||
expect(result).toEqual({ | ||
status: 'Success', | ||
status_code: 200, | ||
data: { | ||
current_page: page, // e.g., 2 | ||
total_pages: Math.ceil(totalCount / limit), // e.g., 4 if totalCount is 20 and limit is 5 | ||
total_waitlist_count: totalCount, | ||
waitlist: mockWaitlistEntries, | ||
}, | ||
message: 'Waitlist found successfully', | ||
}); | ||
}); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters