Skip to content

Commit

Permalink
Helper for testing logics
Browse files Browse the repository at this point in the history
  • Loading branch information
outoftime committed Dec 23, 2019
1 parent b5f14ad commit c23f395
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 54 deletions.
17 changes: 17 additions & 0 deletions src/logic/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export async function processLogic({process, processOptions}, deps) {
const dispatchReturn =
'dispatchReturn' in processOptions
? processOptions.dispatchReturn
: process.length === 1;

const dispatch = jest.fn();
if (dispatchReturn) {
const actionToDispatch = await process(deps);
dispatch(actionToDispatch);
} else {
await new Promise(resolve => {
process(deps, dispatch, resolve);
});
}
return dispatch;
}
50 changes: 21 additions & 29 deletions src/logic/__tests__/linkGithubIdentity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import {linkGithub, saveCredentialForCurrentUser} from '../../clients/firebase';
import {getProfileForAuthenticatedUser} from '../../clients/github';
import {bugsnagClient} from '../../util/bugsnag';

import {
accountMigrationNeeded,
linkIdentityFailed,
identityLinked,
} from '../../actions/user';

import {processLogic} from './helpers';

import {
credentialFactory,
credentialInUseErrorFactory,
Expand All @@ -26,41 +34,31 @@ describe('linkGithubIdentity', () => {
credential: mockCredential,
});

const {
type,
payload: {
credential: {providerId},
user,
},
} = await linkGithubIdentity.process();
const dispatch = await processLogic(linkGithubIdentity);
expect(dispatch).toHaveBeenCalledWith(
identityLinked(mockCredential, mockUser),
);

expect(linkGithub).toHaveBeenCalledWith();
expect(saveCredentialForCurrentUser).toHaveBeenCalledWith(mockCredential);
expect(type).toBe('IDENTITY_LINKED');
expect(providerId).toBe(mockCredential.providerId);
expect(user).toEqual(mockUser);
});

test('credential already in use', async () => {
const error = credentialInUseErrorFactory.build();
const githubProfile = githubProfileFactory.build();

linkGithub.mockRejectedValue(error);
getProfileForAuthenticatedUser.mockResolvedValue(githubProfile);

const {
type,
payload: {
credential: {providerId, accessToken},
},
} = await linkGithubIdentity.process();
getProfileForAuthenticatedUser.mockResolvedValue({data: githubProfile});

const dispatch = await processLogic(linkGithubIdentity);

expect(linkGithub).toHaveBeenCalledWith();
expect(getProfileForAuthenticatedUser).toHaveBeenCalledWith(
error.credential.accessToken,
);
expect(type).toBe('ACCOUNT_MIGRATION_NEEDED');
expect(providerId).toBe(error.credential.providerId);
expect(accessToken).toBe(error.credential.accessToken);
expect(dispatch).toHaveBeenCalledWith(
accountMigrationNeeded(githubProfile, error.credential),
);
});

test('other error', async () => {
Expand All @@ -69,15 +67,9 @@ describe('linkGithubIdentity', () => {
linkGithub.mockRejectedValue(otherError);
bugsnagClient.notify.mockResolvedValue();

const {
type,
error,
payload: {message},
} = await linkGithubIdentity.process();
const dispatch = await processLogic(linkGithubIdentity);
expect(linkGithub).toHaveBeenCalledWith();
expect(bugsnagClient.notify).toHaveBeenCalledWith(otherError);
expect(type).toBe('LINK_IDENTITY_FAILED');
expect(error).toBe(true);
expect(message).toBe(otherError.code);
expect(dispatch).toHaveBeenCalledWith(linkIdentityFailed(otherError));
});
});
4 changes: 3 additions & 1 deletion src/logic/__tests__/logout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import logout from '../logout';

import {signOut} from '../../clients/firebase';

import {processLogic} from './helpers';

jest.mock('../../clients/firebase.js');

test('logOut', async () => {
await logout.process();
await processLogic(logout);
expect(signOut).toHaveBeenCalledWith();
});
32 changes: 14 additions & 18 deletions src/logic/__tests__/startAccountMigration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import {migrateAccount} from '../../clients/firebase';
import {bugsnagClient} from '../../util/bugsnag';

import {processLogic} from './helpers';

import {
credentialFactory,
firebaseErrorFactory,
Expand All @@ -28,9 +30,6 @@ jest.mock('../../util/bugsnag');
jest.useFakeTimers();

describe('startAccountMigration', () => {
const dispatch = jest.fn();
const done = jest.fn();

test('not dismissed during undo period, successful migration', async () => {
const mockUser = userFactory.build();
const mockCredential = credentialFactory.build();
Expand All @@ -51,11 +50,10 @@ describe('startAccountMigration', () => {
});

const emptyAction = new Observable();
const migrationDone = startAccountMigration.process(
{action$: emptyAction, getState: () => state},
dispatch,
done,
);
const migrationDone = processLogic(startAccountMigration, {
action$: emptyAction,
getState: () => state,
});
jest.advanceTimersByTime(5000);
await migrationDone;

Expand All @@ -80,11 +78,10 @@ describe('startAccountMigration', () => {
bugsnagClient.notify.mockResolvedValue();

const emptyAction = new Observable();
const migrationDone = startAccountMigration.process(
{action$: emptyAction, getState: () => state},
dispatch,
done,
);
const migrationDone = processLogic(startAccountMigration, {
action$: emptyAction,
getState: () => state,
});
jest.advanceTimersByTime(5000);
await migrationDone;

Expand All @@ -109,11 +106,10 @@ describe('startAccountMigration', () => {
dismissAccountMigration(),
);

await startAccountMigration.process(
{action$: cancelAction, getState: () => state},
dispatch,
done,
);
await processLogic(startAccountMigration, {
action$: cancelAction,
getState: () => state,
});

expect(migrateAccount).not.toHaveBeenCalledWith(mockCredential);
});
Expand Down
11 changes: 5 additions & 6 deletions src/logic/__tests__/unlinkGithubIdentity.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import unlinkGithubIdentity from '../unlinkGithubIdentity';

import {unlinkGithub} from '../../clients/firebase';
import {identityUnlinked} from '../../actions/user';

import {processLogic} from './helpers';

jest.mock('../../clients/firebase');

test('should unlink Github Identity', async () => {
const {
type,
payload: {providerId},
} = await unlinkGithubIdentity.process();
const dispatch = await processLogic(unlinkGithubIdentity);
expect(unlinkGithub).toHaveBeenCalledWith();
expect(type).toBe('IDENTITY_UNLINKED');
expect(providerId).toBe('github.com');
expect(dispatch).toHaveBeenCalledWith(identityUnlinked('github.com'));
});
1 change: 1 addition & 0 deletions src/logic/startAccountMigration.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default createLogic({
const shouldCancel = await Promise.race([continuePromise, cancelPromise]);

if (shouldCancel) {
done();
return;
}

Expand Down

0 comments on commit c23f395

Please sign in to comment.