-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Added tests for contacts sagas.
- Loading branch information
Showing
3 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
])); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters