Skip to content

Commit

Permalink
GetCredentialFromEmail saga test
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunsaker committed Jun 12, 2018
1 parent a8c85a1 commit 5831959
Showing 1 changed file with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { call, put } from 'redux-saga/effects';
import sagaHelper from 'redux-saga-testing';

import utils from '../../../../utils';
import getCredentialFromEmail from '../';

const auth = {
getCredentialFromEmail: jest.fn(),
};

const action = {
type: 'getCredentialFromEmail',
payload: {
email: 'shaun@aux.co.za',
password: '123123',
},
};

const nextAction = {
type: 'SUCCESS',
};

const actionWithNextAction = { ...action, meta: { nextAction } };

const response = { foo: 'bar' };

describe('When testing the saga without a nextAction and without a response from the api', () => {
const it = sagaHelper(getCredentialFromEmail(action));

it('should have called the mocked API first', (result) => {
expect(JSON.stringify(result)).toEqual(
JSON.stringify(
call(auth.getCredentialFromEmail, action.payload.email, action.payload.password),
),
);
});

// Insert test for default nextAction (if any)

it('and then nothing', (result) => {
expect(result).toBeUndefined();
});
});

describe('When testing the saga without a nextAction and with a response from the api', () => {
const it = sagaHelper(getCredentialFromEmail(action));

it('should have called the mocked API first', (result) => {
expect(JSON.stringify(result)).toEqual(
JSON.stringify(
call(auth.getCredentialFromEmail, action.payload.email, action.payload.password),
),
);

return response;
});

// Insert test for default nextAction (if any)
it('and then trigger the SIGN_IN_USER action', (result) => {
expect(result).toEqual(put({ type: 'SIGN_IN_USER', payload: response }));
});

it('and then nothing', (result) => {
expect(result).toBeUndefined();
});
});

describe('When testing the saga with a nextAction and without a response from the api', () => {
const it = sagaHelper(getCredentialFromEmail(actionWithNextAction));

it('should have called the mocked API first', (result) => {
expect(JSON.stringify(result)).toEqual(
JSON.stringify(
call(auth.getCredentialFromEmail, action.payload.email, action.payload.password),
),
);
});

it('and then trigger an action', (result) => {
expect(result).toEqual(put({ ...nextAction, payload: {} }));
});

it('and then nothing', (result) => {
expect(result).toBeUndefined();
});
});

describe('When testing the saga with a nextAction and with a response from the api', () => {
const it = sagaHelper(getCredentialFromEmail(actionWithNextAction));

it('should have called the mocked API first', (result) => {
expect(JSON.stringify(result)).toEqual(
JSON.stringify(
call(auth.getCredentialFromEmail, action.payload.email, action.payload.password),
),
);

return response;
});

it('and then trigger an action', (result) => {
expect(result).toEqual(put({ ...nextAction, payload: response }));
});

it('and then nothing', (result) => {
expect(result).toBeUndefined();
});
});

describe('When testing the saga when an error is thrown from the api', () => {
const it = sagaHelper(getCredentialFromEmail(action));
const errorMessage = 'Something went wrong';

it('should have called the mocked API first', (result) => {
expect(JSON.stringify(result)).toEqual(
JSON.stringify(
call(auth.getCredentialFromEmail, action.payload.email, action.payload.password),
),
);

return new Error(errorMessage);
});

it('and then trigger an error action with the error message', (result) => {
expect(result).toEqual(
put({
type: 'SET_SYSTEM_MESSAGE',
payload: utils.app.createError(new Error(errorMessage)),
error: true,
}),
);
});

it('and then nothing', (result) => {
expect(result).toBeUndefined();
});
});

0 comments on commit 5831959

Please sign in to comment.