Skip to content

Commit aae3835

Browse files
committed
Free standing functions cannot be stubbed
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
1 parent 24e8859 commit aae3835

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

src/client/api/__tests__/faculty.test.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
newAppliedPhysicsFacultyMember,
1111
bioengineeringFacultyMember,
1212
} from 'testData';
13-
import * as api from 'client/api';
13+
import { FacultyAPI } from 'client/api';
1414
import { ManageFacultyResponseDTO } from 'common/dto/faculty/ManageFacultyResponse.dto';
1515
import {
1616
strictEqual,
@@ -44,7 +44,8 @@ describe('Faculty API', function () {
4444
],
4545
},
4646
});
47-
const response = await api.getFacultySchedulesForYear(acadYear);
47+
const response = await FacultyAPI
48+
.getFacultySchedulesForYear(acadYear);
4849
result = response[acadYear][0];
4950
});
5051
it('should call getAllFacultySchedules', function () {
@@ -64,7 +65,7 @@ describe('Faculty API', function () {
6465
});
6566
it('should throw an error', async function () {
6667
try {
67-
await api.getAllFacultyMembers();
68+
await FacultyAPI.getAllFacultyMembers();
6869
fail('Did not throw an error');
6970
} catch (err) {
7071
strictEqual(err, error);
@@ -97,7 +98,7 @@ describe('Faculty Admin API', function () {
9798
bioengineeringFacultyMemberResponse,
9899
],
99100
} as AxiosResponse<ManageFacultyResponseDTO[]>);
100-
getFacultyResult = await api.getAllFacultyMembers();
101+
getFacultyResult = await FacultyAPI.getAllFacultyMembers();
101102
});
102103
it('should call getAllFacultyMembers', function () {
103104
strictEqual(getStub.callCount, 1);
@@ -120,7 +121,7 @@ describe('Faculty Admin API', function () {
120121
});
121122
it('should throw an error', async function () {
122123
try {
123-
await api.getAllFacultyMembers();
124+
await FacultyAPI.getAllFacultyMembers();
124125
fail('Did not throw an error');
125126
} catch (err) {
126127
deepStrictEqual(err, error);
@@ -141,7 +142,7 @@ describe('Faculty Admin API', function () {
141142
postStub.resolves({
142143
data: physicsFacultyMemberResponse,
143144
} as AxiosResponse<ManageFacultyResponseDTO>);
144-
createFacultyResult = await api
145+
createFacultyResult = await FacultyAPI
145146
.createFaculty(newAppliedPhysicsFacultyMember);
146147
});
147148
it('should call createFaculty', function () {
@@ -161,7 +162,7 @@ describe('Faculty Admin API', function () {
161162
});
162163
it('should throw an error', async function () {
163164
try {
164-
await api.createFaculty(newAppliedPhysicsFacultyMember);
165+
await FacultyAPI.createFaculty(newAppliedPhysicsFacultyMember);
165166
fail('Did not throw an error');
166167
} catch (err) {
167168
deepStrictEqual(err, error);
@@ -190,7 +191,7 @@ describe('Faculty Admin API', function () {
190191
putStub.resolves({
191192
data: editedFacultyMemberResponse,
192193
} as AxiosResponse<ManageFacultyResponseDTO>);
193-
editFacultyResult = await api
194+
editFacultyResult = await FacultyAPI
194195
.editFaculty(editedFacultyMember);
195196
});
196197
it('should call editFaculty', function () {
@@ -213,7 +214,7 @@ describe('Faculty Admin API', function () {
213214
});
214215
it('should throw an error', async function () {
215216
try {
216-
await api.editFaculty(bioengineeringFacultyMember);
217+
await FacultyAPI.editFaculty(bioengineeringFacultyMember);
217218
fail('Did not throw an error');
218219
} catch (err) {
219220
deepStrictEqual(err, error);

src/client/api/faculty.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import request from './request';
77
/**
88
* Retrieves all faculty for the Faculty Admin tab
99
*/
10-
export const getAllFacultyMembers = async ():
10+
const getAllFacultyMembers = async ():
1111
Promise<ManageFacultyResponseDTO[]> => {
1212
const response = await request.get('/api/faculty/');
1313
return response.data as ManageFacultyResponseDTO[];
@@ -16,7 +16,7 @@ Promise<ManageFacultyResponseDTO[]> => {
1616
/**
1717
* Submits a POST request to create a new faculty for the Faculty Admin tab
1818
*/
19-
export const createFaculty = async (facultyInfo: CreateFacultyDTO):
19+
const createFaculty = async (facultyInfo: CreateFacultyDTO):
2020
Promise<ManageFacultyResponseDTO> => {
2121
const response = await request.post('/api/faculty', facultyInfo);
2222
return response.data;
@@ -25,7 +25,7 @@ Promise<ManageFacultyResponseDTO> => {
2525
/**
2626
* Edit an existing faculty member entry
2727
*/
28-
export const editFaculty = async (facultyInfo: UpdateFacultyDTO):
28+
const editFaculty = async (facultyInfo: UpdateFacultyDTO):
2929
Promise<ManageFacultyResponseDTO> => {
3030
const response = await request.put(`/api/faculty/${facultyInfo.id}`, facultyInfo);
3131
return response.data;
@@ -35,7 +35,7 @@ Promise<ManageFacultyResponseDTO> => {
3535
* Retrieves faculty schedule information for the Faculty tab for specified
3636
* academic year(s)
3737
*/
38-
export const getFacultySchedulesForYear = async (
38+
const getFacultySchedulesForYear = async (
3939
acadYears: number
4040
):
4141
Promise<Record<string, FacultyResponseDTO[]>> => {

src/client/components/pages/FacultyAdmin.tsx

+6-9
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ import {
4242
categoryEnumToTitleCase,
4343
sortFaculty,
4444
} from 'common/__tests__/utils/facultyHelperFunctions';
45-
import {
46-
getAllFacultyMembers,
47-
createFaculty,
48-
editFaculty,
49-
} from '../../api/faculty';
5045
import { getAreaColor } from '../../../common/constants';
46+
import { FacultyAPI } from '../../api/faculty';
5147

5248
/**
5349
* The component represents the Faculty Admin page, which will be rendered at
@@ -74,9 +70,10 @@ const FacultyAdmin: FunctionComponent = function (): ReactElement {
7470
* If it fails, display a message for the user
7571
*/
7672
useEffect((): void => {
77-
getAllFacultyMembers()
78-
.then((facultyMembers) => {
73+
FacultyAPI.getAllFacultyMembers()
74+
.then((facultyMembers): ManageFacultyResponseDTO[] => {
7975
setFacultyMembers(facultyMembers);
76+
return facultyMembers;
8077
})
8178
.catch((): void => {
8279
dispatchMessage({
@@ -255,7 +252,7 @@ const FacultyAdmin: FunctionComponent = function (): ReactElement {
255252
if (!createFacultyFirstName && !createFacultyLastName) {
256253
throw new Error('At least a first or last name must be provided for a faculty member. Please try again.');
257254
}
258-
return createFaculty({
255+
return FacultyAPI.createFaculty({
259256
area: createFacultyArea,
260257
HUID: createFacultyHUID,
261258
firstName: createFacultyFirstName,
@@ -284,7 +281,7 @@ const FacultyAdmin: FunctionComponent = function (): ReactElement {
284281
if (!editFacultyFirstName && !editFacultyLastName) {
285282
throw new Error('At least a first or last name must be provided for a faculty member. Please try again.');
286283
}
287-
return editFaculty({
284+
return FacultyAPI.editFaculty({
288285
id: currentFaculty.id,
289286
area: editFacultyArea,
290287
HUID: editFacultyHUID,

0 commit comments

Comments
 (0)