-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduxDomain.js
More file actions
128 lines (108 loc) · 3.61 KB
/
Copy pathreduxDomain.js
File metadata and controls
128 lines (108 loc) · 3.61 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const reduxDomain = (name) => {
const files = {};
files[`${name}.actions.ts`] = action(name);
files[`${name}.actions.test.ts`] = actionTest(name);
files[`${name}.reducer.ts`] = reducer(name);
files[`${name}.reducer.test.ts`] = reducerTest(name);
files[`${name}.types.ts`] = types(name);
files[`${name}.constants.ts`] = constants(name);
return files;
};
module.exports = reduxDomain;
const action = (name) =>
`import { DomainDispatcher as Dispatch } from './domain.types';
import DomainActions from './domain.constants';
const addErrorCreator = (error: string) => ({
type: DomainActions.AddError,
payload: {
error,
}
});
export const domainAddError = (error: string) => (dispatch: Dispatch, getState: ReduxState) => {
return dispatch(addErrorCreator(error))
}
`
.replace(/domain/g, name)
.replace(/Domain/g, cap(name));
const actionTest = (name) =>
`import { domainAddError } from './domain.actions';
describe('domainAddError', () => {
it('should dispatch correctly', () => {
const mockDispatch: jest.Mock = jest.fn((): void => {});
const testError = 'this is a test error';
domainAddError(testError)(mockDispatch)
expect(mockDispatch.mock.calls).toHaveLength(1);
expect(mockDispatch.mock.calls[0][0].payload.error).toEqual(testError);
});
});
`
.replace(/domain/g, name)
.replace(/Domain/g, cap(name));
const reducer = (name) =>
`import DomainActions from './domain.constants';
import DomainState, { DomainDispatches } from './domain.types'
export const initialState: DomainState = {
errors: [],
}
const domainReducer = (state: DomainState = initialState, action: DomainDispatches): DomainState => {
switch (action.type) {
case DomainActions.AddError:
return {
...state,
errors: [
...state.errors,
action.payload.error,
],
};
default:
return state;
};
};
export default domainReducer;
`
.replace(/domain/g, name)
.replace(/Domain/g, cap(name));
const reducerTest = (name) =>
`import domainReducer, { initialState } from './domain.reducer'
import DomainActions from './domain.constants';
describe('domainReducer', () => {
it('should correctly handle AddError', () => {
const newError = 'this is the new test error';
const action = {
type: DomainActions.AddError,
payload: {
error: newError,
}
}
const state = domainReducer(initialState, action);
expect(state.errors.length).toBe(1)
expect(state.errors[0]).toEqual(newError);
})
});
`
.replace(/domain/g, name)
.replace(/Domain/g, cap(name));
const types = (name) =>
`import DomainActions from './domain.constants'
export default interface DomainState {
errors: string[]; // TODO: Add errors to Dispatch type, as per Flux conventions
}
interface DomainDispatch<Action, Payload> {
type: Action;
payload: Payload;
}
type DomainAddError = DomainDispatch<DomainActions.AddError, { error: string }>
export type DomainDispatches = DomainAddError // | Add more types here
export type DomainDispatcher = (args: DomainDispatches) => void;
`
.replace(/domain/g, name)
.replace(/Domain/g, cap(name));
const constants = (name) =>
`enum DomainActions {
AddError = "AddError",
}
export default DomainActions
`
.replace(/domain/g, name)
.replace(/Domain/g, cap(name));
const cap = (string) => string.charAt(0).toUpperCase() + string.slice(1);