Skip to content

Commit

Permalink
Merge pull request #826 from OneCommunityGlobal/christy_add_unit_test…
Browse files Browse the repository at this point in the history
…_team_controller

Christy Unit tests for the Team Controller's getAllTeams and getTeamById
  • Loading branch information
one-community authored Jan 5, 2025
2 parents 9fbb125 + 297ecc9 commit c65533b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
36 changes: 36 additions & 0 deletions requirements/teamController/teamController.md
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.**




81 changes: 81 additions & 0 deletions src/controllers/teamController.spec.js
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);
});
});
});
2 changes: 1 addition & 1 deletion src/test/mock-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const mockReq = {
requestorId: '65cf6c3706d8ac105827bb2e', // this one matches the id of the db/createUser for testing purposes
},
},
params: { userid: '5a7e21f00317bc1538def4b7', userId: '5a7e21f00317bc1538def4b7' },
params: { userid: '5a7e21f00317bc1538def4b7', userId: '5a7e21f00317bc1538def4b7', teamId: '5a8e21f00317bc' },
};

module.exports = mockReq;

0 comments on commit c65533b

Please sign in to comment.