Skip to content

Commit

Permalink
tests: add stories module action creators tests. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofmn authored Jan 17, 2018
1 parent 060a224 commit 32382e2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ducks/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const selectStory = (storyId) => {
}

export const requestTopStories = () => ({
type: SELECT_STORY
type: REQUEST_TOP_STORIES
})

export const receiveTopStories = (stories) => ({
Expand Down
6 changes: 3 additions & 3 deletions tests/middleware/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ describe('Middleware - API', () => {

await middleware(apiAction)

const preRequestMockCalls = apiAction.payload.types[0].calls[0][0]
const preRequestMockCalls = apiAction.payload.types[0].mock.calls

expect(dispatch.mock.calls.length).toBe(2)
expect(dispatch.mock.calls[0][0]).toEqual(preRequest())

expect(preRequestMockCalls.mock.calls.length).toBe(1)
expect(preRequestMockCalls.length).toBe(1)

expect(next.mock.calls.length).toBe(0)

Expand All @@ -83,7 +83,7 @@ describe('Middleware - API', () => {

await middleware(apiAction)

const successRequestMockCalls = apiAction.payload.types[1].calls
const successRequestMockCalls = apiAction.payload.types[1].mock.calls

expect(dispatch.mock.calls.length).toBe(2)
expect(dispatch.mock.calls[1][0]).toEqual(successRequest(response))
Expand Down
71 changes: 69 additions & 2 deletions tests/stories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import reducer, {
receiveTopStories,
errorReceiveTopStories,
selectStory,
fetchTopStories,
REQUEST_TOP_STORIES,
RECEIVE_TOP_STORIES,
ERROR_RECEIVE_TOP_STORIES,
SELECT_STORY
} from '../src/ducks/stories'

import { API_REQUEST } from '../src/constants/actionTypes'

describe('stories reducer', () => {
const initialState = reducer({}, {})

Expand Down Expand Up @@ -59,7 +62,7 @@ describe('stories reducer', () => {
expect(baseState).toEqual(expected)
})

it('should handle ERROR_RECEIVE_TOP_STORIES', () => {
it('should handle ERROR_RECEIVE_TOP_STORIES action', () => {
const action = {
type: ERROR_RECEIVE_TOP_STORIES,
error: true,
Expand All @@ -78,7 +81,7 @@ describe('stories reducer', () => {
expect(baseState).toEqual(expected)
})

it('should handle SELECT_STORY', () => {
it('should handle SELECT_STORY action', () => {
const action = {
type: SELECT_STORY,
payload: {
Expand All @@ -96,3 +99,67 @@ describe('stories reducer', () => {
expect(baseState).toEqual(expected)
})
})

describe('stories action creators', () => {
it('should create a action to select the story', () => {
const expected = {
type: SELECT_STORY,
payload: {
storyId: 1
}
}

expect(selectStory(1)).toEqual(expected)
})

it('should create a action to request the stories', () => {
const expected = {
type: REQUEST_TOP_STORIES
}

expect(requestTopStories()).toEqual(expected)
})

it('should create a action to receive the stories', () => {
const stories = [
{ by: 'author1', title: 'Fake News' },
{ by: 'author2', title: 'Fake News' }
]
const expected = {
type: RECEIVE_TOP_STORIES,
payload: {
stories
}
}

expect(receiveTopStories(stories)).toEqual(expected)
})

it('should create a action to inform error in fetching stories', () => {
const error = new Error('Error test.')
const expected = {
type: ERROR_RECEIVE_TOP_STORIES,
error: true,
payload: error
}

expect(errorReceiveTopStories(error)).toEqual(expected)
})

it('should create a action to perform a API request', () => {
const expected = {
type: API_REQUEST,
payload: {
endpoint: 'topstories',
method: 'GET',
types: [
requestTopStories,
receiveTopStories,
errorReceiveTopStories
]
}
}

expect(fetchTopStories()).toEqual(expected)
})
})

0 comments on commit 32382e2

Please sign in to comment.