-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathConverter-test.ts
156 lines (140 loc) · 3.98 KB
/
Converter-test.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
import assert = require("assert");
import { createConverter, Entity, Identifier, ValueObject } from "../src";
// Entity A
class AIdentifier extends Identifier<string> {}
interface AProps {
id: AIdentifier;
a1: number;
a2: string;
}
class AEntity extends Entity<AProps> {
constructor(args: AProps) {
super(args);
}
}
interface AEntityJSON {
id: string;
a1: number;
a2: string;
}
const AConverter = createConverter<AProps, AEntityJSON>(AEntity, {
id: [prop => prop.toValue(), json => new AIdentifier(json)],
a1: [prop => prop, json => json],
a2: [prop => prop, json => json]
});
// ValueObject B
interface BValueProps {
b1: number;
b2: string;
}
class BValue extends ValueObject<BValueProps> {}
interface BValueJSON {
b1: number;
b2: string;
}
const BConverter = createConverter<BValueProps, BValueJSON>(BValue, {
b1: [prop => prop, json => json],
b2: [prop => prop, json => json]
});
// Parent has A and B
class ParentIdentifier extends Identifier<string> {}
interface ParentJSON {
id: string;
a: AEntityJSON;
b: BValueJSON;
}
interface ParentProps {
id: ParentIdentifier;
a: AEntity;
b: BValue;
}
class ParentEntity extends Entity<ParentProps> {}
const ParentConverter = createConverter<ParentProps, ParentJSON>(ParentEntity, {
id: [prop => prop.toValue(), json => new ParentIdentifier(json)],
a: AConverter,
b: BConverter
});
describe("Converter", function() {
it("should convert JSON <-> Entity", () => {
const converter = createConverter<AProps, AEntityJSON>(AEntity, {
id: [prop => prop.toValue(), json => new AIdentifier(json)],
a1: [prop => prop, json => json],
a2: [prop => prop, json => json]
});
const entity = new AEntity({
id: new AIdentifier("a"),
a1: 42,
a2: "b prop"
});
const json = converter.toJSON(entity);
assert.deepStrictEqual(json, {
id: "a",
a1: 42,
a2: "b prop"
});
const entity2 = converter.fromJSON(json);
assert.deepStrictEqual(entity, entity2);
});
it("should convert Props <-> Entity", () => {
const entity = new AEntity({
id: new AIdentifier("a"),
a1: 42,
a2: "b prop"
});
const json = AConverter.toJSON(entity);
assert.deepStrictEqual(json, {
id: "a",
a1: 42,
a2: "b prop"
});
const props = AConverter.JSONToProps(json);
assert.deepStrictEqual(props, entity.props);
});
it("should convert JSON <-> ValueObject", () => {
const value = new BValue({
b1: 42,
b2: "b prop"
});
const json = BConverter.toJSON(value);
assert.deepStrictEqual(json, {
b1: 42,
b2: "b prop"
});
const value2 = BConverter.fromJSON(json);
assert.deepStrictEqual(value, value2);
});
it("should convert by child Converter", () => {
const a = new AEntity({
id: new AIdentifier("a"),
a1: 42,
a2: "b prop"
});
const b = new BValue({
b1: 42,
b2: "b prop"
});
const parent = new ParentEntity({
id: new ParentIdentifier("parent"),
a,
b
});
const parentJSON = ParentConverter.toJSON(parent);
assert.deepStrictEqual(parentJSON, {
id: "parent",
a: {
a1: 42,
a2: "b prop",
id: "a"
},
b: {
b1: 42,
b2: "b prop"
}
});
const parent_2 = ParentConverter.fromJSON(parentJSON);
assert.ok(parent.equals(parent_2));
assert.deepStrictEqual(parent, parent_2);
const parentJSON_2 = ParentConverter.propsToJSON(parent.props);
assert.deepStrictEqual(parentJSON, parentJSON_2);
});
});