Skip to content

Commit

Permalink
Fix App callCount test by no longer stubbing free-standing function g…
Browse files Browse the repository at this point in the history
…etCurrentUser due to version change

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 test in App.test.tsx was failing when it tried to get the callCount for a stub for a free-standing function getCurrentUser, and currently, my package-lock.json has 3.9.5, so that appears to be why. My fix was to use an object where the functions become methods instead.
  • Loading branch information
natalynyu committed Aug 3, 2020
1 parent ddbe7fb commit 2b1132e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/client/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
PageTitle,
Link,
} from 'mark-one';
import { getCurrentUser } from 'client/api';
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';
Expand Down Expand Up @@ -77,7 +77,7 @@ const ColdApp: SFC = (): ReactElement => {
*/

useEffect((): void => {
getCurrentUser()
UserAPI.getCurrentUser()
.then(({ data: user }): UserResponse => {
setUser(user);
return user;
Expand Down
9 changes: 5 additions & 4 deletions src/client/components/__tests__/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { stub, SinonStub } from 'sinon';
import { AxiosResponse } from 'axios';
import { UserResponse } from 'common/dto/users/userResponse.dto';
import * as dummy from 'testData';
import * as api from 'client/api';
import { UserAPI } from 'client/api';
import { App } from '../App';

describe('App', function () {
let apiStub: SinonStub;
beforeEach(function () {
apiStub = stub(api, 'getCurrentUser');
apiStub = stub(UserAPI, 'getCurrentUser');
apiStub.resolves({
data: dummy.regularUser,
} as AxiosResponse<UserResponse>);
Expand Down Expand Up @@ -157,9 +157,10 @@ describe('App', function () {
<App />
</MemoryRouter>
);
strictEqual(apiStub.callCount, 1);
const { fullName } = dummy.regularUser;
return waitForElement(() => getByText(fullName, { exact: false }));
await waitForElement(() => getByText(fullName, { exact: false }));
strictEqual(apiStub.callCount, 1);
return getByText(fullName, { exact: false });
});
});
context('When userFetch fails', function () {
Expand Down

0 comments on commit 2b1132e

Please sign in to comment.