twreporter-redux
Version:
redux actions and reducers for twreporter website
240 lines (209 loc) • 9.09 kB
JavaScript
var _chai = require('chai');
var _chai2 = _interopRequireDefault(_chai);
var _chaiAsPromised = require('chai-as-promised');
var _chaiAsPromised2 = _interopRequireDefault(_chaiAsPromised);
var _reduxMockStore = require('redux-mock-store');
var _reduxMockStore2 = _interopRequireDefault(_reduxMockStore);
var _reduxStateFields = require('../../constants/redux-state-fields');
var _reduxStateFields2 = _interopRequireDefault(_reduxStateFields);
var _nock = require('nock');
var _nock2 = _interopRequireDefault(_nock);
var _reduxThunk = require('redux-thunk');
var _reduxThunk2 = _interopRequireDefault(_reduxThunk);
var _actionTypes = require('../../constants/action-types');
var _actionTypes2 = _interopRequireDefault(_actionTypes);
var _topics3 = require('../topics');
var actions = _interopRequireWildcard(_topics3);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /* global describe, context, it, afterEach */
/*
Testing functions:
fetchAFullTopic
fetchTopics
fetchTopicsOnIndexPage
*/
_chai2.default.use(_chaiAsPromised2.default);
var expect = _chai2.default.expect;
var middlewares = [_reduxThunk2.default];
var mockStore = (0, _reduxMockStore2.default)(middlewares);
var topic1 = {
id: 'topic-id-1',
slug: 'topic-slug-1',
full: true
};
var topic2 = {
id: 'topic-id-2',
slug: 'topic-slug-2',
full: true
/* Fetch a full topic, whose assets like relateds, leading_video ...etc are all complete,
* @param {string} slug - slug of topic
*/
/*
========= Testing fetchAFullTopic ==========
*/
};describe('Testing fetchAFullTopic:', function () {
afterEach(function () {
_nock2.default.cleanAll();
});
context('Topic is already existed', function () {
it('Should dispatch no actions and return Promise.resolve()', function () {
var mockSlug = 'mock-slug';
var store = mockStore({
entities: {
topics: _defineProperty({}, mockSlug, {
id: 'mock-id',
slug: mockSlug,
full: true
})
}
});
store.dispatch(actions.fetchAFullTopic(mockSlug));
expect(store.getActions().length).to.equal(0); // no action is dispatched
return expect(store.dispatch(actions.fetchAFullTopic(mockSlug))).eventually.equal(undefined);
});
});
context('It loads a full topic successfully', function () {
it('Should dispatch types.START_TO_GET_A_FULL_TOPIC and types.GET_A_FULL_TOPIC', function () {
var mockSlug = 'mock-slug';
var mockTopic = {
id: 'mock-id',
slug: mockSlug,
full: false
};
var store = mockStore({
entities: {
topics: _defineProperty({}, mockSlug, mockTopic)
}
});
var mockApiResponse = {
record: mockTopic
};
(0, _nock2.default)('http://localhost:8080').get(encodeURI('/v1/topics/' + mockSlug + '?full=true')).reply(200, mockApiResponse);
return store.dispatch(actions.fetchAFullTopic(mockSlug)).then(function () {
expect(store.getActions().length).to.equal(2); // 2 actions: REQUEST && SUCCESS
expect(store.getActions()[0].type).to.deep.equal(_actionTypes2.default.START_TO_GET_A_FULL_TOPIC);
expect(store.getActions()[1].type).to.equal(_actionTypes2.default.GET_A_FULL_TOPIC);
expect(store.getActions()[1].payload).to.deep.equal(mockTopic);
});
});
});
context('If the api returns a failure', function () {
it('Should dispatch types.START_TO_GET_A_FULL_TOPIC and types.ERROR_TO_GET_A_FULL_TOPIC', function () {
var store = mockStore();
var mockSlug = 'mock-slug';
(0, _nock2.default)('http://localhost:8080').get(encodeURI('/v1/topics/' + mockSlug + '?full=true')).reply(404);
return store.dispatch(actions.fetchAFullTopic(mockSlug)).then(function () {
expect(store.getActions().length).to.equal(2); // 2 actions: REQUEST && FAILURE
expect(store.getActions()[0].type).to.deep.equal(_actionTypes2.default.START_TO_GET_A_FULL_TOPIC);
expect(store.getActions()[1].type).to.equal(_actionTypes2.default.ERROR_TO_GET_A_FULL_TOPIC);
expect(store.getActions()[1].error).to.be.an.instanceof(Error);
});
});
});
});
/* Fetch topics(only containing meta properties),
* and it will load more if (total > items you have currently).
* @param {number} limit - the number of posts you want to get in one request
*/
/*
========= Testing fetchTopics ==========
*/
describe('Testing fetchTopics:', function () {
afterEach(function () {
_nock2.default.cleanAll();
});
context('There is no more topics to load', function () {
it('Should dispatch no actions and return Promise.resolve()', function () {
var store = mockStore(_defineProperty({}, _reduxStateFields2.default.topics, {
total: 2,
items: [topic1, topic2]
}));
store.dispatch(actions.fetchTopics(10));
expect(store.getActions().length).to.equal(0); // no action is dispatched
return expect(store.dispatch(actions.fetchTopics(10))).eventually.equal(undefined);
});
});
context('It loads topics successfully', function () {
it('Should dispatch types.GET_TOPICS', function () {
var store = mockStore(_defineProperty({}, _reduxStateFields2.default.topics, {
items: [topic1],
total: 2
}));
var limit = 1;
var offset = 1;
var mockApiResponse = {
records: [topic2],
meta: {
limit: limit,
total: 2,
offset: offset
}
};
(0, _nock2.default)('http://localhost:8080').get(encodeURI('/v1/topics?limit=' + limit + '&offset=' + offset)).reply(200, mockApiResponse);
return store.dispatch(actions.fetchTopics(limit)).then(function () {
expect(store.getActions().length).to.equal(2); // 2 actions: REQUEST && SUCCESS
expect(store.getActions()[0].type).to.deep.equal(_actionTypes2.default.START_TO_GET_TOPICS);
expect(store.getActions()[1].type).to.equal(_actionTypes2.default.GET_TOPICS);
expect(store.getActions()[1].payload).to.deep.equal({
items: [topic2],
total: 2
});
});
});
});
context('If the api returns a failure', function () {
it('Should dispatch types.ERROR_TO_GET_TOPICS', function () {
var limit = 1;
var offset = 1;
var store = mockStore();
(0, _nock2.default)('http://localhost:8080').get(encodeURI('/v1/topics?limit=' + limit + '&offset=' + offset)).reply(404);
return store.dispatch(actions.fetchTopics(limit)).then(function () {
expect(store.getActions().length).to.equal(2); // 2 actions: REQUEST && FAILURE
expect(store.getActions()[0].type).to.deep.equal(_actionTypes2.default.START_TO_GET_TOPICS);
expect(store.getActions()[1].type).to.equal(_actionTypes2.default.ERROR_TO_GET_TOPICS);
expect(store.getActions()[1].error).to.be.an.instanceof(Error);
});
});
});
});
/**
* fetchTopicsOnIndexPage
* This function will fetch the 2 to 5 latest topics.
* It's specifically made for index page
*/
/*
========= Testing fetchTopicsOnIndexPage ==========
*/
describe('Testing fetchTopicsOnIndexPage:', function () {
after(function () {
_nock2.default.cleanAll();
});
context('index_page.topics are already existed', function () {
it('Should do nothing', function () {
var store = mockStore(_defineProperty({}, _reduxStateFields2.default.indexPage, _defineProperty({}, _reduxStateFields2.default.topics, [topic1, topic2])));
store.dispatch(actions.fetchTopicsOnIndexPage());
expect(store.getActions().length).to.equal(0); // no action is dispatched
return expect(store.dispatch(actions.fetchTopicsOnIndexPage())).eventually.equal(undefined);
});
});
context('Load topics if needed', function () {
it('Should dispatch types.GET_TOPICS_FOR_INDEX_PAGE)', function () {
var store = mockStore({});
(0, _nock2.default)('http://localhost:8080').get(encodeURI('/v1/topics?offset=1&limit=4')).reply(200, {
records: [topic1, topic2],
meta: {
limit: 10,
total: 2,
offset: 0
}
});
return store.dispatch(actions.fetchTopicsOnIndexPage()).then(function () {
expect(store.getActions().length).to.equal(2); // START and GET
expect(store.getActions()[1].type).to.equal(_actionTypes2.default.GET_TOPICS_FOR_INDEX_PAGE);
expect(store.getActions()[1].payload.items.length).to.equal(2);
});
});
});
});
;