-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92c492a
commit fb7b64d
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
sagas/images/showImagePicker/__tests__/showImagePicker.test.js
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,110 @@ | ||
import { call, put } from 'redux-saga/effects'; | ||
import sagaHelper from 'redux-saga-testing'; | ||
|
||
import utils from '../../../../utils'; | ||
import showImagePicker from '../'; | ||
|
||
const images = { | ||
showImagePicker: jest.fn(), | ||
}; | ||
|
||
const action = { | ||
type: 'showImagePicker', | ||
}; | ||
|
||
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(showImagePicker(action)); | ||
|
||
it('should have called the mocked API first', (result) => { | ||
expect(JSON.stringify(result)).toEqual(JSON.stringify(call(images.showImagePicker))); | ||
}); | ||
|
||
// 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(showImagePicker(action)); | ||
|
||
it('should have called the mocked API first', (result) => { | ||
expect(JSON.stringify(result)).toEqual(JSON.stringify(call(images.showImagePicker))); | ||
|
||
return response; | ||
}); | ||
|
||
// Insert test for default nextAction (if any) | ||
|
||
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(showImagePicker(actionWithNextAction)); | ||
|
||
it('should have called the mocked API first', (result) => { | ||
expect(JSON.stringify(result)).toEqual(JSON.stringify(call(images.showImagePicker))); | ||
}); | ||
|
||
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(showImagePicker(actionWithNextAction)); | ||
|
||
it('should have called the mocked API first', (result) => { | ||
expect(JSON.stringify(result)).toEqual(JSON.stringify(call(images.showImagePicker))); | ||
|
||
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(showImagePicker(action)); | ||
const errorMessage = 'Something went wrong'; | ||
|
||
it('should have called the mocked API first', (result) => { | ||
expect(JSON.stringify(result)).toEqual(JSON.stringify(call(images.showImagePicker))); | ||
|
||
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(); | ||
}); | ||
}); |