Skip to content

Instantly share code, notes, and snippets.

@skayred
Created September 25, 2017 12:40
Show Gist options
  • Save skayred/2a31396a4e461d500cdbecf419bb7824 to your computer and use it in GitHub Desktop.
Save skayred/2a31396a4e461d500cdbecf419bb7824 to your computer and use it in GitHub Desktop.
import { expect } from 'chai';
import * as sinon from 'sinon';
import * as News from '../../redux/modules/news/news';
import * as Seo from '../../redux/modules/seo/seo';
import { staticMetaProps } from '../../redux/modules/news/config';
import { IPostServerResponse } from '../../models/posts';
import { mockStore } from '../../helpers/test_helper';
import { IMochaDone } from '../../models/mocha_done';
import {
IActionRequest,
IActionSuccess,
IActionFailure,
} from '../../models/tests_actions';
import { ISeoDataAction } from '../../models/seo_data';
import { errResponse } from '../common/error';
const response: IPostServerResponse = {
data: [
{
attributes: {
content: '',
datePublish: '',
image: '',
numViews: 0,
sectionUrl: '',
subtitle: '',
author: '',
title: '',
slug: '',
comments: [],
regions: [],
},
id: '',
type: '',
},
{
attributes: {
content: '',
datePublish: '',
numViews: 0,
image: '',
sectionUrl: '',
subtitle: '',
author: '',
title: '',
slug: '',
comments: [],
regions: [],
},
id: '',
type: '',
},
],
meta: {
pagination: {
page: 1,
pages: 3805,
count: 38050,
},
},
};
describe('NewsReducer', () => {
describe('getNews', () => {
const FIRST_PAGE = 1;
const ORDERING = '-date_publish';
it('dispatches Request and Success Actions on OK requests', (done: IMochaDone) => {
const server = sinon.fakeServer.create();
server.respondWith('GET', 'http://mock.backend-content.dev2.cian.ru/v1/get-news-list?page=1&ordering=-date_publish&tag=&category=', [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(response),
]);
const expectedActions: (IActionRequest | IActionSuccess | ISeoDataAction)[] = [
{
type: News.NEWS_REQUEST,
payload: {
pathname: '/v1/get-news-list',
params: [
{
name: 'page',
value: FIRST_PAGE,
},
{
name: 'ordering',
value: ORDERING,
},
{
name: 'tag',
value: '',
},
{
name: 'category',
value: '',
},
],
},
},
{
type: News.NEWS_SUCCESS,
payload: response,
},
{
type: Seo.SEO_DATA_SUCCESS,
payload: { seo: staticMetaProps },
},
];
const store = mockStore();
const params = {
page: FIRST_PAGE,
ordering: ORDERING,
tag: '',
category: '',
};
store.dispatch(News.getNews(params));
server.respond();
server.restore();
setImmediate(() => {
expect(store.getActions()).to.eql(expectedActions);
done();
});
});
it('dispatches Failure on failed requests', (done: IMochaDone) => {
const server = sinon.fakeServer.create();
server.respondWith('GET', 'http://mock.backend-content.dev2.cian.ru/v1/get-news-list?page=1&ordering=-date_publish&tag=&category=', [
400,
{ 'Content-Type': 'application/json' },
JSON.stringify({ errors: [{ ...errResponse }] }),
]);
const expectedActions: (IActionRequest | IActionFailure)[] = [
{
type: News.NEWS_REQUEST,
payload: {
pathname: '/v1/get-news-list',
params: [
{
name: 'page',
value: FIRST_PAGE,
},
{
name: 'ordering',
value: ORDERING,
},
{
name: 'tag',
value: '',
},
{
name: 'category',
value: '',
},
],
},
},
{
type: News.NEWS_FAILURE,
error: errResponse,
},
{
type: Seo.SEO_DATA_FAILURE,
error: errResponse,
},
];
const store = mockStore();
const params = {
page: FIRST_PAGE,
ordering: ORDERING,
tag: '',
category: '',
};
store.dispatch(News.getNews(params));
server.respond();
server.restore();
setImmediate(() => {
expect(store.getActions()).to.eql(expectedActions);
done();
});
});
});
describe('Reducer', () => {
const state = News.initialState;
it('handles action of type request', () => {
const action: any = {
type: News.NEWS_REQUEST,
payload: {
pathname: '/v1/get-news-list',
page: 1,
},
};
expect(News.newsReducer(state, action)).to.be.eql(state);
});
it('handles action of type success', () => {
const action: any = {
type: News.NEWS_SUCCESS,
payload: response,
};
expect(News.newsReducer(state, action)).to.be.eql({
items: response.data,
pages_count: response.meta.pagination.pages,
count: action.payload.meta.pagination.count,
});
});
it('handles action of type failure', () => {
const action: any = {
type: News.NEWS_FAILURE,
err: errResponse,
};
expect(News.newsReducer(state, action)).to.be.eql(state);
});
it('handles action with unknown type', () => {
expect(News.newsReducer(state, undefined)).to.be.eql(state);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment