Skip to content

Commit 84827a4

Browse files
authored
Merge pull request #2894 from asauber/test-gen-actions
Add tests for the core Redux action creator generator
2 parents 040c74d + 34e764a commit 84827a4

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

src/api/internal.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,24 @@ function parseIntIfActualInt(string) {
4646
return isNaN(string) ? string : parseInt(string);
4747
}
4848

49-
const actionGenerators = {
50-
[ONE]: c => (resource, ...ids) => (dispatch) =>
49+
const actionCreatorGenerators = {
50+
// These functions take a config and return Redux Action Creators
51+
[ONE]: config => (resource, ...ids) => (dispatch) =>
5152
dispatch({
5253
resource,
5354
dispatch,
54-
type: `GEN@${fullyQualified(c)}/ONE`,
55+
type: `GEN@${fullyQualified(config)}/ONE`,
5556
ids: ids.map(parseIntIfActualInt),
5657
}),
57-
[MANY]: c => (page, ...ids) => (dispatch) =>
58+
[MANY]: config => (page, ...ids) => (dispatch) =>
5859
dispatch({
5960
page,
6061
dispatch,
61-
type: `GEN@${fullyQualified(c)}/MANY`,
62+
type: `GEN@${fullyQualified(config)}/MANY`,
6263
ids: ids.map(parseIntIfActualInt),
6364
}),
64-
[DELETE]: c => (...ids) =>
65-
({ type: `GEN@${fullyQualified(c)}/DELETE`, ids: ids.map(parseIntIfActualInt) }),
65+
[DELETE]: config => (...ids) =>
66+
({ type: `GEN@${fullyQualified(config)}/DELETE`, ids: ids.map(parseIntIfActualInt) }),
6667
};
6768

6869
/**
@@ -76,8 +77,8 @@ export function genActions(config) {
7677
[DELETE]: 'delete',
7778
};
7879
config.supports.forEach((feature) => {
79-
if (typeof actionGenerators[feature] !== 'undefined') {
80-
actions[fns[feature]] = actionGenerators[feature](config);
80+
if (typeof actionCreatorGenerators[feature] !== 'undefined') {
81+
actions[fns[feature]] = actionCreatorGenerators[feature](config);
8182
}
8283
});
8384
if (config.subresources) {

src/api/internal.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { genActions } from './internal';
2+
3+
import { testConfigOne, testConfigMany, testConfigDelete } from '~/data/reduxGen';
4+
import { resource, page } from '~/data/reduxGen';
5+
6+
describe('internal', () => {
7+
it('generates an action to update one resource', function () {
8+
const dispatch = jest.fn();
9+
10+
const config = testConfigOne;
11+
12+
const actions = genActions(config);
13+
const oneThunk = actions.one(resource, '23');
14+
oneThunk(dispatch);
15+
16+
expect(dispatch.mock.calls[0][0].resource.label).toBe('nodebalancer-1');
17+
expect(dispatch.mock.calls[0][0].type).toBe('GEN@nodebalancers/ONE');
18+
expect(dispatch.mock.calls[0][0].ids[0]).toBe(23);
19+
});
20+
21+
it('generates an action to update many resources', function () {
22+
const dispatch = jest.fn();
23+
24+
const config = testConfigMany;
25+
26+
const actions = genActions(config);
27+
const manyThunk = actions.many(page);
28+
manyThunk(dispatch);
29+
expect(dispatch.mock.calls[0][0].page.nodebalancers[0].label)
30+
.toBe('nodebalancer-1');
31+
expect(dispatch.mock.calls[0][0].page.nodebalancers[1].label)
32+
.toBe('nodebalancer-2');
33+
expect(dispatch.mock.calls[0][0].type).toBe('GEN@nodebalancers/MANY');
34+
});
35+
36+
it('generates an action to delete a resource', function () {
37+
const config = testConfigDelete;
38+
39+
const actions = genActions(config);
40+
const deleteAction = actions.delete('23');
41+
expect(deleteAction.ids[0]).toBe(23);
42+
expect(deleteAction.type).toBe('GEN@nodebalancers/DELETE');
43+
});
44+
});

src/data/reduxGen.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ONE, MANY, DELETE } from '~/api/internal';
2+
3+
import { configsNodeBalancer, noGroupNodeBalancer } from '~/data/nodebalancers';
4+
5+
export const resource = { ...configsNodeBalancer };
6+
export const page = {
7+
data: [configsNodeBalancer, noGroupNodeBalancer],
8+
pages: 1,
9+
results: 1,
10+
page: 1,
11+
nodebalancers: [configsNodeBalancer, noGroupNodeBalancer],
12+
};
13+
14+
export const testConfigOne = {
15+
name: 'nodebalancers',
16+
primaryKey: 'id',
17+
endpoint: id => `/nodebalancers/${id}`,
18+
supports: [ONE],
19+
};
20+
21+
export const testConfigMany = {
22+
name: 'nodebalancers',
23+
primaryKey: 'id',
24+
endpoint: id => `/nodebalancers/${id}`,
25+
supports: [MANY],
26+
};
27+
28+
export const testConfigDelete = {
29+
name: 'nodebalancers',
30+
primaryKey: 'id',
31+
endpoint: id => `/nodebalancers/${id}`,
32+
supports: [DELETE],
33+
};

0 commit comments

Comments
 (0)