-
Notifications
You must be signed in to change notification settings - Fork 4
/
quotes.ts
86 lines (70 loc) · 3.42 KB
/
quotes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { API_QUOTE_PATH, API_QUOTES_PATH } from '../../../helpers/quotes/quotesHttp';
import { IQuoteBlank, IQuote } from '../../../interfaces/IQuote';
import { HTTP_ERROR_500 } from '../../../test-utils/axiosMocks';
import { recordSaga } from '../../../test-utils/recordSaga';
import {
quoteCreate,
quoteCreateSuccess,
QuotesActionTypes,
quoteCreateError,
quoteFetchOne, quoteFetchOneSuccess, quoteFetchOneError, quoteFetchAll, quoteFetchAllSuccess, quoteFetchAllError
} from '../../actions/quotes';
import { quoteCreation, quoteFetching, quotesFetching } from '../quotes';
describe('Middlewares for quotes', () => {
const mock = new MockAdapter(axios);
const quoteBlank: IQuoteBlank = {text: 'aaa', author: 'bbb'};
const quote: IQuote = {id: 1, ...quoteBlank};
describe('Quote creation', () => {
const createAction = quoteCreate(quoteBlank);
it(`A quote should be created on server and be added to store by ${QuotesActionTypes.CREATED_SUCCESS}`, () => {
mock.onPost(API_QUOTE_PATH).reply(200, quote);
return recordSaga(quoteCreation, createAction)
.then(dispatched => {
expect(dispatched).toEqual([quoteCreateSuccess(quote)]);
});
});
it(`When server responds error, action ${QuotesActionTypes.CREATED_FAIL} should be created`, () => {
mock.onPost(API_QUOTE_PATH).reply(500);
return recordSaga(quoteCreation, createAction)
.then(dispatched => {
expect(dispatched).toEqual([quoteCreateError(HTTP_ERROR_500)]);
});
});
});
describe('Quote fetching', () => {
const fetchOneAction = quoteFetchOne(quote.id);
it(`Quote should be fetched from server and be added to store by ${QuotesActionTypes.FETCH_ONE_SUCCESS}`, () => {
mock.onGet(API_QUOTE_PATH).reply(200, quote);
return recordSaga(quoteFetching, fetchOneAction)
.then(dispatched => {
expect(dispatched).toEqual([quoteFetchOneSuccess(quote)]);
});
});
it(`When server responds error, action ${QuotesActionTypes.FETCH_ONE_FAIL} should be created`, () => {
mock.onGet(API_QUOTE_PATH).reply(500);
return recordSaga(quoteFetching, fetchOneAction)
.then(dispatched => {
expect(dispatched).toEqual([quoteFetchOneError(HTTP_ERROR_500)]);
});
});
});
describe('Quotes list fetching', () => {
const fetchAllAction = quoteFetchAll();
it(`Quotes list should be fetched from server and be added to store by ${QuotesActionTypes.FETCH_ALL_SUCCESS}`, () => {
mock.onGet(API_QUOTES_PATH).reply(200, [quote]);
return recordSaga(quotesFetching, fetchAllAction)
.then(dispatched => {
expect(dispatched).toEqual([quoteFetchAllSuccess([quote])]);
});
});
it(`When server responds error, action ${QuotesActionTypes.FETCH_ALL_FAIL} should be created`, () => {
mock.onGet(API_QUOTES_PATH).reply(500);
return recordSaga(quotesFetching, fetchAllAction)
.then(dispatched => {
expect(dispatched).toEqual([quoteFetchAllError(HTTP_ERROR_500)]);
});
});
});
});