-
Notifications
You must be signed in to change notification settings - Fork 30
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 #826 from OneCommunityGlobal/christy_add_unit_test…
…_team_controller Christy Unit tests for the Team Controller's getAllTeams and getTeamById
- Loading branch information
Showing
3 changed files
with
118 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
Check mark: ✅ | ||
Cross Mark: ❌ | ||
|
||
# Team Controller Test Documentation | ||
|
||
## GetAllTeams | ||
|
||
> ### Negative Cases | ||
1. ✅ **Returns 404 - an error occurs during team retrieval.** | ||
|
||
> ### Positive Cases | ||
1. ✅ **Returns 200 - should return all teams sorted by name.** | ||
|
||
|
||
## GetTeamById | ||
|
||
> ### Negative Cases | ||
1. ✅ **Returns 404 - the specified team ID does not exist.** | ||
|
||
> ### Positive Cases | ||
1. ✅ **Returns 200 - all is successful, return a team by ID.** | ||
|
||
|
||
## PostTeam | ||
|
||
> ### Negative Cases | ||
1. ❌ **Returns 403 - the requestor lacks `postTeam` permission.** | ||
2. ❌ **Returns 403 - a team with the same name already exists.** | ||
|
||
> ### Positive Cases | ||
1. ❌ **Returns 200 - a new team is successfully created.** | ||
|
||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const Team = require('../models/team'); | ||
const teamController = require('./teamController'); | ||
const { mockReq, mockRes, assertResMock } = require('../test'); | ||
|
||
const makeSut = () => { | ||
const { getAllTeams, getTeamById } = teamController(Team); | ||
return { | ||
getAllTeams, | ||
getTeamById, | ||
}; | ||
}; | ||
|
||
const flushPromises = () => new Promise(setImmediate); | ||
|
||
describe('teamController', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const sortObject = { | ||
sort: () => {}, | ||
}; | ||
|
||
const error = new Error('any error'); | ||
|
||
describe('getAllTeams', () => { | ||
test('Returns 404 - an error occurs during team retrieval.', async () => { | ||
const { getAllTeams } = makeSut(); | ||
|
||
const mockSort = jest.spyOn(sortObject, 'sort').mockRejectedValueOnce(error); | ||
const findSpy = jest.spyOn(Team, 'find').mockReturnValue(sortObject); | ||
const response = getAllTeams(mockReq, mockRes); | ||
await flushPromises(); | ||
|
||
expect(findSpy).toHaveBeenCalledWith({}); | ||
expect(mockSort).toHaveBeenCalledWith({ teamName: 1 }); | ||
assertResMock(404, error, response, mockRes); | ||
}); | ||
|
||
test('Returns 200 - should return all teams sorted by name.', async () => { | ||
const team1 = { teamName: 'Team A' }; | ||
const team2 = { teamName: 'Team B' }; | ||
const sortedTeams = [team1, team2]; | ||
|
||
const mockSortResovledValue = [team1, team2]; | ||
const mockSort = jest.spyOn(sortObject, 'sort').mockResolvedValue(mockSortResovledValue); | ||
const findSpy = jest.spyOn(Team, 'find').mockReturnValue(sortObject); | ||
const { getAllTeams } = makeSut(); | ||
const response = getAllTeams(mockReq, mockRes); | ||
await flushPromises(); | ||
|
||
expect(findSpy).toHaveBeenCalledWith({}); | ||
expect(mockSort).toHaveBeenCalledWith({ teamName: 1 }); | ||
assertResMock(200, sortedTeams, response, mockRes); | ||
}); | ||
}); | ||
|
||
describe('getTeamById', () => { | ||
test('Returns 404 - the specified team ID does not exist.', async () => { | ||
const { getTeamById } = makeSut(); | ||
const req = { params: { teamId: 'nonExistentTeamId' } }; | ||
const findByIdSpy = jest.spyOn(Team, 'findById').mockRejectedValue(error); | ||
const response = getTeamById(req, mockRes); | ||
await flushPromises(); | ||
|
||
expect(findByIdSpy).toHaveBeenCalledWith(req.params.teamId); | ||
assertResMock(404, error, response, mockRes); | ||
}); | ||
|
||
test('Returns 200 - all is successful, return a team by ID.', async () => { | ||
const { getTeamById } = makeSut(); | ||
const teamId = '5a8e21f00317bc'; | ||
const findByIdSpy = jest.spyOn(Team, 'findById').mockResolvedValue({ teamId }); | ||
const response = getTeamById(mockReq, mockRes); | ||
await flushPromises(); | ||
|
||
expect(findByIdSpy).toHaveBeenCalledWith(teamId); | ||
assertResMock(200, { teamId }, response, mockRes); | ||
}); | ||
}); | ||
}); |
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