-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unit-tests): test examples for Redux and Saga
Added redux and saga tests examples Installed dependencies: yarn add redux-saga-test-plan --dev
- Loading branch information
Showing
9 changed files
with
515 additions
and
414 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
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
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
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
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,36 @@ | ||
import { nanoid } from '@reduxjs/toolkit' | ||
import { expectSaga, testSaga } from 'redux-saga-test-plan' | ||
import { call } from 'redux-saga-test-plan/matchers' | ||
|
||
import { getPosts } from '../../api' | ||
import { Post } from '../../types' | ||
import { onGetPosts } from '../posts.sagas' | ||
import postsReducer, { postsActions } from '../posts.slice' | ||
|
||
const expectedSagaPosts: Post[] = [{ id: '1', title: 'saga-test-example', body: nanoid() }] | ||
|
||
describe('Saga - test examples', () => { | ||
it('should execute commands in exact order with redux-saga-test-plan', async () => | ||
testSaga(onGetPosts) | ||
.next() | ||
.call(getPosts) | ||
.next(expectedSagaPosts) | ||
.put(postsActions.fetchAllSucceeded(expectedSagaPosts)) | ||
.next() | ||
.isDone()) | ||
|
||
it('should mock external api with .provide()', () => | ||
expectSaga(onGetPosts) | ||
.provide([[call(getPosts), expectedSagaPosts]]) | ||
.put(postsActions.fetchAllSucceeded(expectedSagaPosts)) | ||
.run()) | ||
|
||
test('integration test with withReducer', () => | ||
expectSaga(onGetPosts) | ||
.withReducer(postsReducer) | ||
.provide([[call(getPosts), expectedSagaPosts]]) | ||
.hasFinalState({ | ||
posts: expectedSagaPosts, | ||
}) | ||
.run()) | ||
}) |
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,81 @@ | ||
import { RootState, store } from '../../../../store/store' | ||
import { Post } from '../../types' | ||
import reducer, { postsActions, selectPosts } from '../posts.slice' | ||
|
||
const expectedPosts = [ | ||
{ id: '1', body: 'post1', title: 'post1' }, | ||
{ id: '2', body: 'post2', title: 'post2' }, | ||
] | ||
|
||
describe('State tests', () => { | ||
it('should initially set post to an empty array', () => { | ||
const state = store.getState().posts | ||
expect(state.posts.length).toEqual(0) | ||
}) | ||
}) | ||
|
||
describe('Reducer tests', () => { | ||
it('should return the initial state when passed an empty action', () => { | ||
// Given | ||
const initialState = undefined | ||
|
||
const action = { type: '' } | ||
|
||
// When | ||
const result = reducer(initialState, action) | ||
|
||
// Then | ||
expect(result).toEqual({ posts: [] }) | ||
}) | ||
|
||
it('should add received posts', () => { | ||
// Given | ||
const initialState = undefined | ||
|
||
const action = postsActions.fetchAllSucceeded(expectedPosts) | ||
|
||
// When | ||
const result = reducer(initialState, action) | ||
|
||
// Then | ||
expect(Object.keys(result.posts).length).toEqual(expectedPosts.length) | ||
expect(result.posts).toEqual(expectedPosts) | ||
}) | ||
}) | ||
|
||
describe('Selectors tests', () => { | ||
it('should return empty posts', () => { | ||
// Given | ||
const state: RootState = { | ||
posts: { | ||
posts: [], | ||
}, | ||
router: {}, | ||
} | ||
|
||
// When | ||
const result = selectPosts(state) | ||
|
||
// Then | ||
expect(result).toEqual([]) | ||
}) | ||
}) | ||
|
||
|
||
const expectedSagaPosts: Post[] = [{ id: '1', title: 'saga', body: 'saga' }] | ||
|
||
jest.mock('../../api/index', () => ({ | ||
async getPosts() { | ||
return expectedSagaPosts | ||
}, | ||
})) | ||
|
||
describe('Saga tests', () => { | ||
it('should return all posts when fetchAll dispatched (using mocked Rest API)', async () => { | ||
// When | ||
await store.dispatch(postsActions.fetchAll()) | ||
|
||
// Then | ||
expect(store.getState().posts.posts).toEqual(expectedSagaPosts) | ||
}) | ||
}) |
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
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
Oops, something went wrong.