Skip to content

Commit 63dac9a

Browse files
committed
Export api course functions in an object due to TypeScript update
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 Currently, my package-lock.json has 3.9.5, so that appears to be why I was having these problems in my test. My fix was to use an object where the functions become methods instead.
1 parent 819eb43 commit 63dac9a

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
physicsCourseResponse,
1111
error,
1212
} from 'testData';
13-
import * as api from 'client/api';
13+
import { CourseAPI } from 'client/api';
1414
import { ManageCourseResponseDTO } from 'common/dto/courses/ManageCourseResponse.dto';
1515
import {
1616
strictEqual,
@@ -37,7 +37,7 @@ describe('Course Admin API', function () {
3737
physicsCourseResponse,
3838
],
3939
} as AxiosResponse<ManageCourseResponseDTO[]>);
40-
result = await api.getAllCourses();
40+
result = await CourseAPI.getAllCourses();
4141
});
4242
it('should call getAllCourses', function () {
4343
strictEqual(getStub.callCount, 1);
@@ -60,7 +60,7 @@ describe('Course Admin API', function () {
6060
});
6161
it('should throw an error', async function () {
6262
try {
63-
await api.getAllCourses();
63+
await CourseAPI.getAllCourses();
6464
fail('Did not throw an error');
6565
} catch (err) {
6666
deepStrictEqual(err, error);

src/client/api/courses.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import { ManageCourseResponseDTO } from '../../common/dto/courses/ManageCourseRe
55
/**
66
* Retrieves all courses
77
*/
8-
export const getAllCourses = async (): Promise<ManageCourseResponseDTO[]> => {
8+
const getAllCourses = async (): Promise<ManageCourseResponseDTO[]> => {
99
const response = await request.get('/api/courses/');
1010
return response.data;
1111
};
1212

13-
export const getCourseInstancesForYear = async (
13+
const getCourseInstancesForYear = async (
1414
acadYear: number
1515
): Promise<CourseInstanceResponseDTO[][]> => {
1616
const response = await request
1717
.get(`/api/course-instances/?acadYear=${acadYear}`);
1818
return response.data;
1919
};
20+
21+
export const CourseAPI = {
22+
getAllCourses,
23+
getCourseInstancesForYear,
24+
};

src/client/components/pages/CourseAdmin.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { MessageContext } from 'client/context';
2323
import { TableRowProps } from 'mark-one/lib/Tables/TableRow';
2424
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
2525
import { faEdit } from '@fortawesome/free-solid-svg-icons';
26-
import { getAllCourses } from '../../api/courses';
26+
import { CourseAPI } from '../../api/courses';
2727

2828
/**
2929
* The component represents the Course Admin page, which will be rendered at
@@ -45,7 +45,7 @@ const CourseAdmin: FunctionComponent = function (): ReactElement {
4545
* If it fails, display a message for the user
4646
*/
4747
useEffect((): void => {
48-
getAllCourses()
48+
CourseAPI.getAllCourses()
4949
.then((courses): ManageCourseResponseDTO[] => {
5050
setCourses(courses);
5151
return courses;

src/client/components/pages/Courses/CoursesPage.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
import { LoadSpinner } from 'mark-one';
99
import { MessageContext } from 'client/context';
1010
import CourseInstanceResponseDTO from 'common/dto/courses/CourseInstanceResponse';
11-
import { getCourseInstancesForYear } from 'client/api';
11+
import { CourseAPI } from 'client/api';
1212
import { MESSAGE_TYPE, MESSAGE_ACTION, AppMessage } from 'client/classes';
1313
import { OFFERED, COURSE_TABLE_COLUMN } from 'common/constants';
1414
import CourseInstanceTable from './CourseInstanceTable';
@@ -64,7 +64,7 @@ const CoursesPage: FunctionComponent = (): ReactElement => {
6464

6565
useEffect((): void => {
6666
setFetching(true);
67-
getCourseInstancesForYear(acadYear)
67+
CourseAPI.getCourseInstancesForYear(acadYear)
6868
.then((courses: CourseInstanceResponseDTO[][]): void => {
6969
setCourses(courses[0]);
7070
})

src/client/components/pages/Courses/__tests__/CoursesPage.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { stub, SinonStub } from 'sinon';
44
import {
55
render, BoundFunction, QueryByText, FindByText, wait,
66
} from 'test-utils';
7-
import * as courseAPI from 'client/api';
7+
import { CourseAPI } from 'client/api';
88
import { AppMessage, MESSAGE_TYPE, MESSAGE_ACTION } from 'client/classes';
99
import { cs50CourseInstance } from 'testData';
1010
import CoursesPage from '../CoursesPage';
@@ -13,7 +13,7 @@ describe('Course Instances List', function () {
1313
let getStub: SinonStub;
1414
let dispatchStub: SinonStub;
1515
beforeEach(function () {
16-
getStub = stub(courseAPI, 'getCourseInstancesForYear');
16+
getStub = stub(CourseAPI, 'getCourseInstancesForYear');
1717
dispatchStub = stub();
1818
});
1919
describe('fetching data on render', function () {

0 commit comments

Comments
 (0)