twreporter-redux
Version:
redux actions and reducers for twreporter website
178 lines (160 loc) • 5.24 kB
JavaScript
/* global describe, it */
import fieldNames from '../../constants/redux-state-field-names'
import reducer from '../index-page'
import types from '../../constants/action-types'
// lodash
import cloneDeep from 'lodash/cloneDeep'
import merge from 'lodash/merge'
import { expect } from 'chai'
const _ = {
cloneDeep,
merge,
}
const post1 = {
id: 'post-id-1',
slug: 'post-slug-1',
}
const post2 = {
id: 'post-id-2',
slug: 'post-slug-2',
}
const post3 = {
id: 'post-id-3',
slug: 'post-slug-3',
}
const post4 = {
id: 'post-id-4',
slug: 'post-slug-4',
}
const fullTopic = {
id: 'topic-id-1',
slug: 'topic-slug-1',
relateds: [post3, post4],
full: true,
}
const nonFullTopic = {
id: 'topic-id-1',
slug: 'topic-slug-1',
full: false,
}
describe('index-page reducer', () => {
it('should return the initial state', () => {
expect(
reducer({}, {}),
).to.deep.equal({})
})
it('should handle GET_CONTENT_FOR_INDEX_PAGE', () => {
expect(
reducer({
[]: 'topic-slug-3',
}, {
type: types.GET_CONTENT_FOR_INDEX_PAGE,
payload: {
[]: _.cloneDeep([post1, post2]),
[]: _.cloneDeep([post4]),
[]: _.cloneDeep([post3]),
[]: _.cloneDeep([fullTopic]),
[]: _.cloneDeep([fullTopic, nonFullTopic]),
[]: _.cloneDeep([post1, post4]),
[]: _.cloneDeep([post2, post3]),
},
}),
).to.deep.equal({
[]: [post1.slug, post2.slug],
[]: [post4.slug],
[]: [post3.slug],
[]: [fullTopic.slug],
[]: [fullTopic.slug, nonFullTopic.slug],
[]: [post1.slug, post4.slug],
[]: [post2.slug, post3.slug],
[]: [],
[]: [],
[]: [],
[]: [],
[]: [],
[]: [],
error: null,
isFetching: false,
})
})
it('should handle ERROR_TO_GET_CONTENT_FOR_INDEX_PAGE', () => {
const err = new Error('error occurs')
expect(
reducer({
[]: [post1.slug, post2.slug],
[]: [post4.slug],
[]: [post3.slug],
[]: [fullTopic.slug],
[]: [fullTopic.slug, nonFullTopic.slug],
[]: [post1.slug, post4.slug],
[]: [post2.slug, post3.slug],
error: null,
}, {
type: types.ERROR_TO_GET_INDEX_PAGE_CONTENT,
error: err,
}),
).to.deep.equal({
[]: [post1.slug, post2.slug],
[]: [post4.slug],
[]: [post3.slug],
[]: [fullTopic.slug],
[]: [fullTopic.slug, nonFullTopic.slug],
[]: [post1.slug, post4.slug],
[]: [post2.slug, post3.slug],
error: err,
isFetching: false,
})
})
it('should handle GET_TOPICS_FOR_INDEX_PAGE', () => {
expect(
reducer({
}, {
type: types.GET_TOPICS_FOR_INDEX_PAGE,
payload: {
items: _.cloneDeep([nonFullTopic, fullTopic]),
},
}),
).to.deep.equal({
[]: [nonFullTopic.slug, fullTopic.slug],
})
})
it('should handle GET_PHOTOGRAPHY_POSTS_FOR_INDEX_PAGE', () => {
expect(
reducer({
}, {
type: types.GET_PHOTOGRAPHY_POSTS_FOR_INDEX_PAGE,
payload: {
items: _.cloneDeep([post1]),
},
}),
).to.deep.equal({
[]: [post1.slug],
})
})
it('should handle GET_INFOGRAPHIC_POSTS_FOR_INDEX_PAGE', () => {
expect(
reducer({
}, {
type: types.GET_INFOGRAPHIC_POSTS_FOR_INDEX_PAGE,
payload: {
items: _.cloneDeep([post1]),
},
}),
).to.deep.equal({
[]: [post1.slug],
})
})
it('should handle GET_EDITOR_PICKED_POSTS', () => {
expect(
reducer({
}, {
type: types.GET_EDITOR_PICKED_POSTS,
payload: {
items: _.cloneDeep([post1]),
},
}),
).to.deep.equal({
[]: [post1.slug],
})
})
})