Skip to content

Commit 9b3ceab

Browse files
committed
Cover actions with tests
As far actions are the core part of UI implementations it's essential to be sure that they work correctly
1 parent e9c2d36 commit 9b3ceab

File tree

4 files changed

+193
-25
lines changed

4 files changed

+193
-25
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"eslint-plugin-jsx-a11y": "^6.0.2",
5050
"eslint-plugin-react": "^7.4.0",
5151
"extract-text-webpack-plugin": "^3.0.2",
52+
"fetch-mock": "^6.0.0-beta.2",
5253
"file-loader": "^1.1.5",
5354
"glob": "^7.1.2",
5455
"html-webpack-plugin": "^2.30.1",

tests/actions.test.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/store.test.js

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import fetchMock from 'fetch-mock';
2+
import createStore from '../src/store';
3+
4+
import * as actions from '../src/actions';
5+
6+
describe('actions', () => {
7+
it('should create an action to set recent snippets', () => {
8+
const snippets = [
9+
{
10+
id: 1,
11+
content: 'test',
12+
syntax: 'JavaScript',
13+
},
14+
{
15+
id: 2,
16+
content: 'batman',
17+
syntax: 'Python',
18+
},
19+
];
20+
const store = createStore();
21+
store.dispatch(actions.setRecentSnippets(snippets));
22+
23+
expect(store.getState().toJS()).toEqual({
24+
recent: [1, 2],
25+
snippets: {
26+
1: {
27+
id: 1,
28+
content: 'test',
29+
syntax: 'JavaScript',
30+
},
31+
2: {
32+
id: 2,
33+
content: 'batman',
34+
syntax: 'Python',
35+
},
36+
},
37+
syntaxes: [],
38+
});
39+
});
40+
41+
it('should create an action to fetch recent snippets', async () => {
42+
const snippets = [
43+
{
44+
id: 1,
45+
content: 'test',
46+
syntax: 'JavaScript',
47+
},
48+
{
49+
id: 2,
50+
content: 'batman',
51+
syntax: 'Python',
52+
},
53+
];
54+
55+
fetchMock.getOnce('http://api.xsnippet.org/snippets', JSON.stringify(snippets));
56+
57+
const store = createStore();
58+
await store.dispatch(actions.fetchRecentSnippets);
59+
60+
expect(store.getState().toJS()).toEqual({
61+
recent: [1, 2],
62+
snippets: {
63+
1: {
64+
id: 1,
65+
content: 'test',
66+
syntax: 'JavaScript',
67+
},
68+
2: {
69+
id: 2,
70+
content: 'batman',
71+
syntax: 'Python',
72+
},
73+
},
74+
syntaxes: [],
75+
});
76+
});
77+
78+
it('should create an action to set snippet', () => {
79+
const snippet = {
80+
id: 3,
81+
content: 'not batman',
82+
syntax: 'Go',
83+
};
84+
const store = createStore();
85+
store.dispatch(actions.setSnippet(snippet));
86+
87+
expect(store.getState().toJS()).toEqual({
88+
recent: [],
89+
snippets: {
90+
3: {
91+
id: 3,
92+
content: 'not batman',
93+
syntax: 'Go',
94+
},
95+
},
96+
syntaxes: [],
97+
});
98+
});
99+
100+
it('should create an action to fetch snippet', async () => {
101+
const snippet = {
102+
id: 3,
103+
content: 'not batman',
104+
syntax: 'Go',
105+
};
106+
107+
fetchMock.getOnce(`http://api.xsnippet.org/snippets/${snippet.id}`, JSON.stringify(snippet));
108+
109+
const store = createStore();
110+
await store.dispatch(actions.fetchSnippet(snippet.id));
111+
112+
expect(store.getState().toJS()).toEqual({
113+
recent: [],
114+
snippets: {
115+
3: {
116+
id: 3,
117+
content: 'not batman',
118+
syntax: 'Go',
119+
},
120+
},
121+
syntaxes: [],
122+
});
123+
});
124+
125+
it('should create an action to set syntaxes', () => {
126+
const syntaxes = ['JavaScript', 'Python', 'Java', 'Go', 'Plain Text'];
127+
const store = createStore();
128+
store.dispatch(actions.setSyntaxes(syntaxes));
129+
130+
expect(store.getState().toJS()).toEqual({
131+
recent: [],
132+
snippets: {},
133+
syntaxes,
134+
});
135+
});
136+
137+
it('should create an action to fetch syntaxes', async () => {
138+
const syntaxes = ['JavaScript', 'Python', 'Java', 'Go', 'Plain Text'];
139+
140+
fetchMock.getOnce('http://api.xsnippet.org/syntaxes', JSON.stringify(syntaxes));
141+
142+
const store = createStore();
143+
await store.dispatch(actions.fetchSyntaxes);
144+
145+
expect(store.getState().toJS()).toEqual({
146+
recent: [],
147+
snippets: {},
148+
syntaxes,
149+
});
150+
});
151+
152+
it('should create an action to post snippet', async () => {
153+
const snippet = {
154+
id: 4,
155+
content: 'Batman',
156+
syntax: 'JavaScript',
157+
};
158+
159+
fetchMock.postOnce('http://api.xsnippet.org/snippets', JSON.stringify(snippet));
160+
161+
const store = createStore();
162+
await store.dispatch(actions.postSnippet(snippet, () => {}));
163+
164+
expect(store.getState().toJS()).toEqual({
165+
recent: [],
166+
snippets: {
167+
4: {
168+
id: 4,
169+
content: 'Batman',
170+
syntax: 'JavaScript',
171+
},
172+
},
173+
syntaxes: [],
174+
});
175+
});
176+
});

0 commit comments

Comments
 (0)