Skip to content

Commit

Permalink
Free standing functions cannot be stubbed
Browse files Browse the repository at this point in the history
It turns out that you can no longer stub free-standing functions in Typescript 3.9.2+ according to this issue:

microsoft/TypeScript#38568
sinonjs/sinon#562

The solution is to make an object, FacultyAPI, that contains those functions and export the object instead of the functions themselves
  • Loading branch information
natalynyu committed Aug 3, 2020
1 parent 671dfcc commit edfb9e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
19 changes: 10 additions & 9 deletions src/client/api/__tests__/faculty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
newAppliedPhysicsFacultyMember,
bioengineeringFacultyMember,
} from 'testData';
import * as api from 'client/api';
import { FacultyAPI } from 'client/api';
import { ManageFacultyResponseDTO } from 'common/dto/faculty/ManageFacultyResponse.dto';
import {
strictEqual,
Expand Down Expand Up @@ -44,7 +44,8 @@ describe('Faculty API', function () {
],
},
});
const response = await api.getFacultySchedulesForYear(acadYear);
const response = await FacultyAPI
.getFacultySchedulesForYear(acadYear);
result = response[acadYear][0];
});
it('should call getAllFacultySchedules', function () {
Expand All @@ -64,7 +65,7 @@ describe('Faculty API', function () {
});
it('should throw an error', async function () {
try {
await api.getAllFacultyMembers();
await FacultyAPI.getAllFacultyMembers();
fail('Did not throw an error');
} catch (err) {
strictEqual(err, error);
Expand Down Expand Up @@ -97,7 +98,7 @@ describe('Faculty Admin API', function () {
bioengineeringFacultyMemberResponse,
],
} as AxiosResponse<ManageFacultyResponseDTO[]>);
getFacultyResult = await api.getAllFacultyMembers();
getFacultyResult = await FacultyAPI.getAllFacultyMembers();
});
it('should call getAllFacultyMembers', function () {
strictEqual(getStub.callCount, 1);
Expand All @@ -120,7 +121,7 @@ describe('Faculty Admin API', function () {
});
it('should throw an error', async function () {
try {
await api.getAllFacultyMembers();
await FacultyAPI.getAllFacultyMembers();
fail('Did not throw an error');
} catch (err) {
deepStrictEqual(err, error);
Expand All @@ -141,7 +142,7 @@ describe('Faculty Admin API', function () {
postStub.resolves({
data: physicsFacultyMemberResponse,
} as AxiosResponse<ManageFacultyResponseDTO>);
createFacultyResult = await api
createFacultyResult = await FacultyAPI
.createFaculty(newAppliedPhysicsFacultyMember);
});
it('should call createFaculty', function () {
Expand All @@ -161,7 +162,7 @@ describe('Faculty Admin API', function () {
});
it('should throw an error', async function () {
try {
await api.createFaculty(newAppliedPhysicsFacultyMember);
await FacultyAPI.createFaculty(newAppliedPhysicsFacultyMember);
fail('Did not throw an error');
} catch (err) {
deepStrictEqual(err, error);
Expand Down Expand Up @@ -190,7 +191,7 @@ describe('Faculty Admin API', function () {
putStub.resolves({
data: editedFacultyMemberResponse,
} as AxiosResponse<ManageFacultyResponseDTO>);
editFacultyResult = await api
editFacultyResult = await FacultyAPI
.editFaculty(editedFacultyMember);
});
it('should call editFaculty', function () {
Expand All @@ -213,7 +214,7 @@ describe('Faculty Admin API', function () {
});
it('should throw an error', async function () {
try {
await api.editFaculty(bioengineeringFacultyMember);
await FacultyAPI.editFaculty(bioengineeringFacultyMember);
fail('Did not throw an error');
} catch (err) {
deepStrictEqual(err, error);
Expand Down
8 changes: 4 additions & 4 deletions src/client/api/faculty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import request from './request';
/**
* Retrieves all faculty for the Faculty Admin tab
*/
export const getAllFacultyMembers = async ():
const getAllFacultyMembers = async ():
Promise<ManageFacultyResponseDTO[]> => {
const response = await request.get('/api/faculty/');
return response.data;
Expand All @@ -16,7 +16,7 @@ Promise<ManageFacultyResponseDTO[]> => {
/**
* Submits a POST request to create a new faculty for the Faculty Admin tab
*/
export const createFaculty = async (facultyInfo: CreateFacultyDTO):
const createFaculty = async (facultyInfo: CreateFacultyDTO):
Promise<ManageFacultyResponseDTO> => {
const response = await request.post('/api/faculty', facultyInfo);
return response.data;
Expand All @@ -25,7 +25,7 @@ Promise<ManageFacultyResponseDTO> => {
/**
* Edit an existing faculty member entry
*/
export const editFaculty = async (facultyInfo: UpdateFacultyDTO):
const editFaculty = async (facultyInfo: UpdateFacultyDTO):
Promise<ManageFacultyResponseDTO> => {
const response = await request.put(`/api/faculty/${facultyInfo.id}`, facultyInfo);
return response.data;
Expand All @@ -35,7 +35,7 @@ Promise<ManageFacultyResponseDTO> => {
* Retrieves faculty schedule information for the Faculty tab for specified
* academic year(s)
*/
export const getFacultySchedulesForYear = async (
const getFacultySchedulesForYear = async (
acadYears: number
):
Promise<{ [key: string]: FacultyResponseDTO[] }> => {
Expand Down
12 changes: 4 additions & 8 deletions src/client/components/pages/FacultyAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ import {
categoryEnumToTitleCase,
sortFaculty,
} from 'common/__tests__/utils/facultyHelperFunctions';
import {
getAllFacultyMembers,
createFaculty,
editFaculty,
} from '../../api/faculty';
import { FacultyAPI } from '../../api/faculty';

/**
* The component represents the Faculty Admin page, which will be rendered at
Expand All @@ -74,7 +70,7 @@ const FacultyAdmin: FunctionComponent = function (): ReactElement {
* If it fails, display a message for the user
*/
useEffect((): void => {
getAllFacultyMembers()
FacultyAPI.getAllFacultyMembers()
.then((facultyMembers): ManageFacultyResponseDTO[] => {
setFacultyMembers(facultyMembers);
return facultyMembers;
Expand Down Expand Up @@ -256,7 +252,7 @@ const FacultyAdmin: FunctionComponent = function (): ReactElement {
if (!createFacultyFirstName && !createFacultyLastName) {
throw new Error('At least a first or last name must be provided for a faculty member. Please try again.');
}
return createFaculty({
return FacultyAPI.createFaculty({
area: createFacultyArea,
HUID: createFacultyHUID,
firstName: createFacultyFirstName,
Expand Down Expand Up @@ -285,7 +281,7 @@ const FacultyAdmin: FunctionComponent = function (): ReactElement {
if (!editFacultyFirstName && !editFacultyLastName) {
throw new Error('At least a first or last name must be provided for a faculty member. Please try again.');
}
return editFaculty({
return FacultyAPI.editFaculty({
id: currentFaculty.id,
area: editFacultyArea,
HUID: editFacultyHUID,
Expand Down

0 comments on commit edfb9e9

Please sign in to comment.