Skip to content

Commit

Permalink
[tests] Added tests for contacts sagas.
Browse files Browse the repository at this point in the history
  • Loading branch information
seland committed Sep 8, 2018
1 parent 3a1b04c commit c5763c6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
16 changes: 16 additions & 0 deletions __tests__/src/sagas/contacts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @flow

import { takeEvery, all } from 'redux-saga/effects';

import rootSaga from '../../../../src/sagas/contacts';
import { ADD_CONTACT, START_CONTACTS_FETCH } from '../../../../src/actions/contacts';
import { SERVICES_CREATED } from '../../../../src/actions/serviceContainer';
import { addNewContact, fetchContacts } from '../../../../src/sagas/contacts/sagas';

test('rootSaga', () => {
const iterator = rootSaga();
expect(iterator.next().value).toEqual(all([
takeEvery([SERVICES_CREATED, START_CONTACTS_FETCH], fetchContacts),
takeEvery(ADD_CONTACT, addNewContact),
]));
});
74 changes: 74 additions & 0 deletions __tests__/src/sagas/contacts/sagas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// @flow

import { call, put } from 'redux-saga/effects';
import { cloneableGenerator } from 'redux-saga/utils';

import { addContact, contactsFetchFailed, contactsUpdated } from '../../../../src/actions/contacts';
import { addNewContact, fetchContacts } from '../../../../src/sagas/contacts/sagas';
import { getProfile } from '../../../../src/sagas/chat/sagas';
import ContactsService from '../../../../src/services/contacts';
import type { ProfileType } from '../../../../src/types/Chat';

const mockIdentityKey = 'MOCK_IDENTITY_KEY';
const mockProfile: ProfileType = {
name: 'name',
location: 'location',
identityKey: mockIdentityKey,
ethereumAddress: 'ethereumAddress',
ethereumPublicKey: 'ethereumPublicKey',
chatIdKey: 'chatIdKey',
timestamp: new Date(),
version: 1,
image: 'image',
ethereumKeySignature: 'ethereumKeySignature',
identityKeySignature: 'identityKeySignature',
};

test('addNewContact', () => {
const mockCallback = jest.fn();
const mockAction = addContact(mockIdentityKey, mockCallback);

const gen = cloneableGenerator(addNewContact)(mockAction);
expect(gen.next().value).toEqual(call(getProfile, mockIdentityKey));

const exceptionGen = gen.clone();
const mockError = new Error('MOCK: Failed to get profile');
expect(exceptionGen.throw(mockError).value).toEqual(call(mockCallback, mockError));
expect(exceptionGen.next().done).toBeTruthy();

const noProfileGen = gen.clone();
expect(noProfileGen.next(undefined).value).toEqual(call(mockCallback, new Error('Trying to add contact, but profile is not on database')));
expect(noProfileGen.next().done).toBeTruthy();

expect(gen.next(mockProfile).value).toEqual(call(ContactsService.addContact, mockIdentityKey));
expect(gen.next().value).toEqual(call(mockCallback, null));
expect(gen.next().value).toEqual(put(fetchContacts));
});

test('fetchContacts', () => {
const mockIdentityKey2 = 'MOCK_IDENTITY_KEY_2';

const mockIdentityKeys = [
{ identity_key: mockIdentityKey },
{ identity_key: mockIdentityKey2 },
];

const gen = cloneableGenerator(fetchContacts)();
expect(gen.next().value).toEqual(call(ContactsService.getContacts));

const exceptionGen = gen.clone();
const mockError = new Error('MOCK: Failed to get contacts');
expect(exceptionGen.throw(mockError).value).toEqual(put(contactsFetchFailed(mockError)));
expect(exceptionGen.next().done).toBeTruthy();

const emptyListGen = gen.clone();
expect(emptyListGen.next([]).value).toEqual(put(contactsUpdated([])));
expect(emptyListGen.next().done).toBeTruthy();

expect(gen.next(mockIdentityKeys).value).toEqual(call(getProfile, mockIdentityKey));
// We return profile for first request
expect(gen.next(mockProfile).value).toEqual(call(getProfile, mockIdentityKey2));
// We return null for second request
expect(gen.next(null).value).toEqual(put(contactsUpdated([{ profile: mockProfile }])));
expect(gen.next().done).toBeTruthy();
});
6 changes: 3 additions & 3 deletions src/sagas/contacts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
} from '../../actions/contacts';

/**
* @desc Root Contacts saga.
* @desc Root contacts saga.
* @return {void}
*/
export default function* rootSaga(): Generator<*, *, *> {
yield all([
yield takeEvery([SERVICES_CREATED, START_CONTACTS_FETCH], fetchContacts),
yield takeEvery(ADD_CONTACT, addNewContact),
takeEvery([SERVICES_CREATED, START_CONTACTS_FETCH], fetchContacts),
takeEvery(ADD_CONTACT, addNewContact),
]);
}

0 comments on commit c5763c6

Please sign in to comment.