Skip to content

Commit

Permalink
Export api course functions in an object due to TypeScript update
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

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.
  • Loading branch information
natalynyu committed Aug 3, 2020
1 parent 5fee569 commit 6587d8a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/client/api/__tests__/courses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
physicsCourseResponse,
error,
} from 'testData';
import * as api from 'client/api';
import { CourseAPI } from 'client/api';
import { ManageCourseResponseDTO } from 'common/dto/courses/ManageCourseResponse.dto';
import {
strictEqual,
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('Course Admin API', function () {
physicsCourseResponse,
],
} as AxiosResponse<ManageCourseResponseDTO[]>);
result = await api.getAllCourses();
result = await CourseAPI.getAllCourses();
});
it('should call getAllCourses', function () {
strictEqual(getStub.callCount, 1);
Expand All @@ -60,7 +60,7 @@ describe('Course Admin API', function () {
});
it('should throw an error', async function () {
try {
await api.getAllCourses();
await CourseAPI.getAllCourses();
fail('Did not throw an error');
} catch (err) {
deepStrictEqual(err, error);
Expand Down
9 changes: 7 additions & 2 deletions src/client/api/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { ManageCourseResponseDTO } from '../../common/dto/courses/ManageCourseRe
/**
* Retrieves all courses
*/
export const getAllCourses = async (): Promise<ManageCourseResponseDTO[]> => {
const getAllCourses = async (): Promise<ManageCourseResponseDTO[]> => {
const response = await request.get('/api/courses/');
return response.data;
};

export const getCourseInstancesForYear = async (
const getCourseInstancesForYear = async (
acadYear: number
): Promise<CourseInstanceResponseDTO[][]> => {
const response = await request
.get(`/api/course-instances/?acadYear=${acadYear}`);
return response.data;
};

export const CourseAPI = {
getAllCourses,
getCourseInstancesForYear,
};
4 changes: 2 additions & 2 deletions src/client/components/pages/CourseAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { MessageContext } from 'client/context';
import { TableRowProps } from 'mark-one/lib/Tables/TableRow';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faEdit } from '@fortawesome/free-solid-svg-icons';
import { getAllCourses } from '../../api/courses';
import { CourseAPI } from '../../api/courses';

/**
* The component represents the Course Admin page, which will be rendered at
Expand All @@ -44,7 +44,7 @@ const CourseAdmin: FunctionComponent = function (): ReactElement {
* If it fails, display a message for the user
*/
useEffect((): void => {
getAllCourses()
CourseAPI.getAllCourses()
.then((courses): ManageCourseResponseDTO[] => {
setCourses(courses);
return courses;
Expand Down
4 changes: 2 additions & 2 deletions src/client/components/pages/Courses/CoursesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, {
import { LoadSpinner } from 'mark-one';
import { MessageContext } from 'client/context';
import CourseInstanceResponseDTO from 'common/dto/courses/CourseInstanceResponse';
import { getCourseInstancesForYear } from 'client/api';
import { CourseAPI } from 'client/api';
import { MESSAGE_TYPE, MESSAGE_ACTION, AppMessage } from 'client/classes';
import { OFFERED, COURSE_TABLE_COLUMN } from 'common/constants';
import CourseInstanceTable from './CourseInstanceTable';
Expand Down Expand Up @@ -63,7 +63,7 @@ const CoursesPage: FunctionComponent = (): ReactElement => {

useEffect((): void => {
setFetching(true);
getCourseInstancesForYear(acadYear)
CourseAPI.getCourseInstancesForYear(acadYear)
.then((courses: CourseInstanceResponseDTO[][]): void => {
setCourses(courses[0]);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { stub, SinonStub } from 'sinon';
import {
render, BoundFunction, QueryByText, FindByText, wait,
} from 'test-utils';
import * as courseAPI from 'client/api';
import { CourseAPI } from 'client/api';
import { AppMessage, MESSAGE_TYPE, MESSAGE_ACTION } from 'client/classes';
import { cs50CourseInstance } from 'testData';
import CoursesPage from '../CoursesPage';
Expand All @@ -13,7 +13,7 @@ describe('Course Instances List', function () {
let getStub: SinonStub;
let dispatchStub: SinonStub;
beforeEach(function () {
getStub = stub(courseAPI, 'getCourseInstancesForYear');
getStub = stub(CourseAPI, 'getCourseInstancesForYear');
dispatchStub = stub();
});
describe('fetching data on render', function () {
Expand Down

0 comments on commit 6587d8a

Please sign in to comment.