Skip to content

Commit

Permalink
Create MetadataAPI interface
Browse files Browse the repository at this point in the history
getMetadata cannot be exported as a standalone function. 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
  • Loading branch information
natalynyu committed Sep 14, 2020
1 parent 53e9024 commit 9fc18d1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/client/api/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
rawSemesterList,
error,
} from 'common/data';
import * as api from 'client/api';
import { MetadataAPI } from 'client/api/metadata';
import {
strictEqual,
deepStrictEqual,
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('Metadata API', function () {
.map(({ term, year }): string => `${term} ${year}`),
},
} as AxiosResponse<MetadataResponse>);
result = await api.getMetadata();
result = await MetadataAPI.getMetadata();
});
it('should call getMetadata', function () {
strictEqual(getStub.callCount, 1);
Expand All @@ -64,7 +64,7 @@ describe('Metadata API', function () {
});
it('should throw an error', async function () {
try {
await api.getMetadata();
await MetadataAPI.getMetadata();
fail('Did not throw an error');
} catch (err) {
deepStrictEqual(err, error);
Expand Down
6 changes: 5 additions & 1 deletion src/client/api/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import request from './request';
/**
* Get the current metadata
*/
export const getMetadata = async (): Promise<MetadataResponse> => {
const getMetadata = async (): Promise<MetadataResponse> => {
const response = await request.get('/api/metadata/');
return response.data as MetadataResponse;
};

export const MetadataAPI = {
getMetadata,
};
4 changes: 2 additions & 2 deletions src/client/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import { UserAPI } from 'client/api';
import { UserResponse } from 'common/dto/users/userResponse.dto';
import { MetadataContext } from 'client/context/MetadataContext';
import { getMetadata } from 'client/api/metadata';
import { MetadataAPI } from 'client/api/metadata';
import { Message } from './layout';
import NoMatch from './pages/NoMatch';
import logo from '../img/seas-logo.svg';
Expand Down Expand Up @@ -112,7 +112,7 @@ export const ColdApp: SFC = (): ReactElement => {
});

useEffect((): void => {
getMetadata()
MetadataAPI.getMetadata()
.then((metadata): void => {
setMetadata(metadata);
})
Expand Down
4 changes: 2 additions & 2 deletions src/client/components/__tests__/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { render as customRender } from 'test-utils';
import { metadata } from 'common/data/metadata';
import { stub, SinonStub } from 'sinon';
import * as metaApi from 'client/api/metadata';
import { MetadataAPI } from 'client/api/metadata';
import * as dummy from 'common/data';
import { UserAPI } from 'client/api';
import { MemoryRouter } from 'react-router-dom';
Expand All @@ -20,7 +20,7 @@ describe('App', function () {
let dispatchMessage: SinonStub;
beforeEach(function () {
userStub = stub(UserAPI, 'getCurrentUser');
metaStub = stub(metaApi, 'getMetadata');
metaStub = stub(MetadataAPI, 'getMetadata');
dispatchMessage = stub();
userStub.resolves(dummy.regularUser);
metaStub.resolves(dummy.metadata);
Expand Down

0 comments on commit 9fc18d1

Please sign in to comment.