-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.spec.ts
175 lines (153 loc) · 4.01 KB
/
transaction.spec.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {
expect as chaiExpect,
} from 'chai';
import {
createTransaction,
} from './transaction';
import {
AnyObject,
} from '../types';
import { randomString } from '../utils';
// Contrived ETL example middleware
export const extractMiddleware = (api: AnyObject) => {
const { type } = api.action;
return (middleware: any) => {
return (params: any) => {
const {
action,
state,
} = params;
if (type === 'TRANSACTION_COMMIT') {
if (action.type === 'REFORMAT_OBJECT') {
const keys = [state.value.source.data.strings[0], state.value.source.data.strings[2]];
const values = [state.value.source.data.strings[1], state.value.source.data.strings[3]];
return middleware({ action: {
type: action.type,
data: { keys, values },
}, state });
}
} else if (type === 'TRANSACTION_ROLLBACK') {
if (action.type === 'REFORMAT_OBJECT') {
return middleware(params);
}
} else {
return middleware(params);
}
};
};
};
export const transformMiddleware = (api: AnyObject) => {
const { type } = api.action;
return (middleware: any) => {
return (params: any) => {
const {
action,
state,
} = params;
if (type === 'TRANSACTION_COMMIT') {
if (action.type === 'REFORMAT_OBJECT') {
const data = {
[action.data.keys[0]]: action.data.values[0],
[action.data.keys[1]]: action.data.values[1],
}
return middleware({
carlo: 'test',
action: {
type: action.type,
data,
},
state,
});
}
} else if (type === 'TRANSACTION_ROLLBACK') {
if (action.type === 'REFORMAT_OBJECT') {
return middleware(params);
}
} else {
return middleware(params);
}
};
};
};
export const loadMiddleware = (api: AnyObject) => {
const { type } = api.action;
return (middleware: any) => {
return (params: any) => {
const {
action,
state,
} = params;
if (type === 'TRANSACTION_COMMIT') {
if (action.type === 'REFORMAT_OBJECT') {
return middleware({ action, state: {
value: {
source: state.value.source,
target: action.data,
}
} });
}
} else if (type === 'TRANSACTION_ROLLBACK') {
if (action.type === 'REFORMAT_OBJECT') {
return middleware(params);
}
} else {
return middleware(params);
}
};
};
};
describe('transaction', () => {
const action = {
type: 'REFORMAT_OBJECT',
}
let source: AnyObject, target: AnyObject, keyOne: string,
keyTwo: string, valueOne: string, valueTwo: string, contrivedEtl: any,
state: AnyObject;
beforeEach(() => {
keyOne = randomString();
valueOne = randomString();
keyTwo = randomString();
valueTwo = randomString();
source = {
data: {
strings: [keyOne, valueOne, keyTwo, valueTwo],
}
};
target = {
values: [],
};
state = {
revisionId: 'test',
value: {
source,
target,
},
};
contrivedEtl = createTransaction({
middleware: [
{
middleware: extractMiddleware,
},
{
middleware: transformMiddleware,
},
{
middleware: loadMiddleware,
},
],
});
});
it('should instantiate', async () => {
chaiExpect(createTransaction()).to.be.not.null;
});
it('should process commit chain', async () => {
const extracted = contrivedEtl(state, action);
const result = extracted.commit();
chaiExpect(result.state.value.target).to.have.keys(keyOne, keyTwo);
});
it('should process rollback chain', async () => {
const extracted = contrivedEtl(state, action);
const result = extracted.rollback();
chaiExpect(result.state.value.target.values.length).to.eq(0);
});
});