forked from mateodelnorte/sourced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.ts
233 lines (193 loc) · 7.07 KB
/
entity.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { Entity } from "../src/entity";
class TestEntity extends Entity {
property: boolean;
property2: { subProperty: boolean; subProperty2: boolean };
constructor(snapshot?, events?) {
super();
this.property = false;
this.property2 = {
subProperty: false,
subProperty2: true,
};
this.rehydrate(snapshot, events);
}
method(param) {
this.property2 = param.data;
this.digest("method", param);
this.emit("method-ed");
}
}
describe("entity", function () {
describe("#constructor", function () {
it("should accept variations of snapshots and events to use for rehydrating", () => {
const snapshot = {
property: true,
};
const data = { hello: "sourced" };
const entity1 = new TestEntity(snapshot);
expect(entity1.property).toBe(true);
entity1.method({ data });
expect(entity1.property2).toEqual(data);
// console.warn(entity1)
const entity2 = new TestEntity(snapshot, entity1.newEvents);
expect(entity2.property).toEqual(entity1.property);
expect(entity2.property2).toEqual(entity1.property2);
const entity3 = new TestEntity(null, entity1.newEvents);
expect(entity3.property).toBe(false);
expect(entity3.property2).toEqual(entity1.property2);
});
});
describe("#digest", function () {
it("should wrap param object with method matching calling method name and add to array of newEvents", function () {
const test = new TestEntity();
const data = { test: "data" };
test.method(data);
expect(test.newEvents.length).toEqual(1);
expect(test.newEvents[0].method).toEqual("method");
expect(test.newEvents[0].data).toEqual(data);
});
it("should have versions 1 and 2 for two consecutively digested events", function () {
const test = new TestEntity();
const data = { test: "data" };
const data2 = { test: "data2" };
test.method(data);
test.method(data2);
expect(test.newEvents.length).toEqual(2);
expect(test.newEvents[0].method).toEqual("method");
expect(test.newEvents[0].data).toEqual(data);
expect(test.newEvents[1].method).toEqual("method");
expect(test.newEvents[1].data).toEqual(data2);
expect(test.version).toEqual(2);
});
});
describe("#emit", function () {
it("should emit EventEmitter style events", function () {
const test = new TestEntity();
const handler = jest.fn();
test.on("something.happened", handler);
test.emit("something.happened", { data: "data" });
expect(handler).toBeCalledWith({ data: "data" });
});
it("should emit EventEmitter style events with 2 params", function () {
const test = new TestEntity();
const handler = jest.fn();
test.on("something.happened", handler);
test.emit("something.happened", { data: "data" }, { data2: "data2" });
expect(handler).toBeCalledWith({ data: "data" }, { data2: "data2" });
});
});
describe("#enqueue", function () {
it("should enqueue EventEmitter style events by adding them to array of events to emit", function () {
const test = new TestEntity();
test.enqueue("something.happened", { data: "data" }, { data2: "data2" });
expect(test.eventsToEmit).toBeDefined();
expect(Array.prototype.slice.call(test.eventsToEmit[0], 0, 1)[0]).toEqual(
"something.happened"
);
expect(
Array.prototype.slice.call(test.eventsToEmit[0], 1)[0].data
).toBeDefined();
expect(
Array.prototype.slice.call(test.eventsToEmit[0], 1)[0].data
).toEqual("data");
expect(
Array.prototype.slice.call(test.eventsToEmit[0], 1)[1].data2
).toBeDefined();
expect(
Array.prototype.slice.call(test.eventsToEmit[0], 1)[1].data2
).toEqual("data2");
});
});
describe("#merge", function () {
it("should merge a snapshot into the current snapshot, overwriting any common properties", function () {
const snapshot = {
property: true,
property2: true,
};
const test = new TestEntity();
test.merge(snapshot);
expect(test.property).toEqual(true);
expect(test.property2).toEqual(true);
});
it("should merge a complex snapshot (missing newly added fields) while maintaining defaulted sub-object values", function () {
const snapshot = {
property: true,
};
const test = new TestEntity();
test.merge(snapshot);
expect(test.property).toEqual(true);
expect(test.property2.subProperty).toBeDefined();
expect(test.property2.subProperty).toEqual(false);
expect(test.property2.subProperty2).toBeDefined();
expect(test.property2.subProperty2).toEqual(true);
});
it("should merge a complex snapshot while maintaining defaulted sub-object values", function () {
const snapshot = {
property: true,
property2: {
subProperty: true,
subProperty2: false,
},
};
const test = new TestEntity();
test.merge(snapshot);
expect(test.property).toEqual(true);
expect(test.property2.subProperty).toBeDefined();
expect(test.property2.subProperty).toEqual(true);
expect(test.property2.subProperty2).toBeDefined();
expect(test.property2.subProperty2).toEqual(false);
});
});
describe("#replay", function () {
it("should throw an entity error with name of model when attempting to replay a method an entity does not implement", function () {
const events = [
{
method: "someMethod",
data: { some: "param" },
},
];
const test = new TestEntity();
expect(function () {
test.replay(events);
}).toThrow("method 'someMethod' does not exist on model 'TestEntity'");
});
it("should not emit events during replay", function () {
const events = [
{
method: "method",
data: { some: "param" },
},
];
const test = new TestEntity();
test.on("method-ed", function () {
throw new Error("should not emit during replay");
});
test.replay(events);
});
});
describe("#snapshot", function () {
it("should return object with current state of the entity and increase snapshotVersion", function () {
const test = new TestEntity();
const data = { data: "data" };
test.method(data);
const snapshot = test.snapshot();
expect(snapshot.property2).toEqual(data.data);
expect(snapshot.snapshotVersion).toEqual(1);
expect(snapshot.version).toEqual(1);
});
});
describe("#state", function () {
it("should return object with current state of the entity", function () {
const test = new TestEntity();
const data = { data: "data" };
test.method(data);
expect(test.newEvents.length).toBe(1);
const state = test.state();
expect(test.newEvents.length).toBe(1);
expect(state.property).toEqual(false);
expect(state.property2).toEqual(data.data);
expect(state.snapshotVersion).toEqual(0);
expect(state.version).toEqual(1);
});
});
});