-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnews.spec.js
More file actions
71 lines (61 loc) · 1.84 KB
/
news.spec.js
File metadata and controls
71 lines (61 loc) · 1.84 KB
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
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import newsReducer, {
getNewsAsync,
selectCurrentNewsItems,
selectCurrentPage,
selectCurrentTotal,
PAGE_SIZE,
} from './news';
const mockAxios = new MockAdapter(axios);
const sampleNewsItems = [1, 2, 3, 4, 5].map((idx) => ({
objectID: idx.toString(),
title: `News ${idx}`,
url: `http://news${idx}.test`,
points: idx * 10,
num_comments: idx * 6,
author: `Author ${idx}`,
created_at: `2020-04-0${idx}T03:09:30.000Z`,
}));
const rootReducer = combineReducers({
news: newsReducer,
});
let store;
describe('news ducks', () => {
beforeEach(() => {
store = createStore(rootReducer, applyMiddleware(thunk));
});
afterEach(() => {
mockAxios.reset();
});
it('should prepare correct default state', () => {
expect(selectCurrentNewsItems(store.getState())).toEqual([]);
expect(selectCurrentPage(store.getState())).toEqual(1);
expect(selectCurrentTotal(store.getState())).toEqual(0);
});
describe('getNewsAsync', () => {
it('fetches getNewsAsync correctly and update correct state', async () => {
mockAxios.onGet('https://hn.algolia.com/api/v1/search', {
params: {
page: 2,
query: 'Hello World',
hitsPerPage: PAGE_SIZE,
tags: 'story',
},
}).reply(200, {
hits: sampleNewsItems,
hitsPerPage: PAGE_SIZE,
nbPages: 11,
});
await store.dispatch(getNewsAsync({
page: 3,
searchQuery: 'Hello World',
}));
expect(selectCurrentNewsItems(store.getState())).toEqual(sampleNewsItems);
expect(selectCurrentPage(store.getState())).toEqual(3);
expect(selectCurrentTotal(store.getState())).toEqual(66);
});
});
});