|
1
|
|
|
import { Entity } from "../../src/Entities/Entity"; |
|
2
|
|
|
import { IEntityDefinition } from "../../src/Types/Entities/IEntityDefinition"; |
|
3
|
|
|
import { UniqueEntityId } from "../../src/Utilities/Ids/UniqueEntityId"; |
|
4
|
|
|
import { EntityRequiredFieldsNotFoundError } from "../../src/Errors/Entities/EntityRequiredFieldsNotFoundError"; |
|
5
|
|
|
import { IFieldDefinition } from "../../src/Types/Entities/IFieldDefinition"; |
|
6
|
|
|
import { FieldValidator } from "../../src/Utilities/Fields/FieldValidator"; |
|
7
|
|
|
import { EntityInvalidFieldTypesError } from "../../src/Errors/Entities/EntityInvalidFieldTypesError"; |
|
8
|
|
|
|
|
9
|
|
|
jest.mock("../../src/Utilities/Ids/UniqueEntityId"); |
|
10
|
|
|
jest.mock("../../src/Utilities/Fields/FieldValidator"); |
|
11
|
|
|
|
|
12
|
|
|
describe("Entity", () => { |
|
13
|
|
|
const mockProps = { |
|
14
|
|
|
required: 1, |
|
15
|
|
|
}; |
|
16
|
|
|
|
|
17
|
|
|
const mockDefinition: IEntityDefinition = { |
|
18
|
|
|
required: [ |
|
19
|
|
|
{ |
|
20
|
|
|
name: "dummy", |
|
21
|
|
|
type: "number", |
|
22
|
|
|
}, |
|
23
|
|
|
], |
|
24
|
|
|
optional: [ |
|
25
|
|
|
{ |
|
26
|
|
|
name: "optional", |
|
27
|
|
|
type: "number", |
|
28
|
|
|
}, |
|
29
|
|
|
], |
|
30
|
|
|
}; |
|
31
|
|
|
|
|
32
|
|
|
function mockReturnTrue(): true { |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
function mockReturnMissingField(): string[] { |
|
36
|
|
|
return ["dummy"]; |
|
37
|
|
|
} |
|
38
|
|
|
function mockReturnInvalidField(): IFieldDefinition[] { |
|
39
|
|
|
return [{ name: "dummy", type: "number" }]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
class MockEntity extends Entity<any> { |
|
43
|
|
|
constructor(props: any, id?: UniqueEntityId) { |
|
44
|
|
|
super(props, mockDefinition, id); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public validateFieldsRequired( |
|
48
|
|
|
requiredValidation: FieldValidator<any> |
|
49
|
|
|
): void { |
|
50
|
|
|
super.validateFieldsRequired(requiredValidation); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public validateFieldsMatchingTypes( |
|
54
|
|
|
requiredValidation: FieldValidator<any>, |
|
55
|
|
|
optionalValidation: FieldValidator<any> |
|
56
|
|
|
) { |
|
57
|
|
|
super.validateFieldsMatchingTypes(requiredValidation, optionalValidation); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public validate(): void { |
|
61
|
|
|
super.validate(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
let eg: MockEntity; |
|
66
|
|
|
|
|
67
|
|
|
// Sets up the global valid entity and mocks |
|
68
|
|
|
beforeAll(() => { |
|
69
|
|
|
(FieldValidator as jest.MockedClass<any>) |
|
70
|
|
|
// Validation fails because of missing required fields |
|
71
|
|
|
.mockImplementation(() => { |
|
72
|
|
|
return { |
|
73
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
74
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnTrue), |
|
75
|
|
|
}; |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
eg = new MockEntity(mockProps); |
|
79
|
|
|
}); |
|
80
|
|
|
|
|
81
|
|
|
test("Entity can be successfully instantiated without id", () => { |
|
82
|
|
|
expect(eg).toBeInstanceOf(Entity); |
|
83
|
|
|
}); |
|
84
|
|
|
|
|
85
|
|
|
// Missing required fields returned by the validator |
|
86
|
|
|
test("Required field validation fails when the validator does", () => { |
|
87
|
|
|
(FieldValidator as jest.MockedClass<any>).mockImplementationOnce(() => { |
|
88
|
|
|
return { |
|
89
|
|
|
allFieldsAvailable: jest.fn(mockReturnMissingField), |
|
90
|
|
|
}; |
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
eg.validateFieldsRequired( |
|
95
|
|
|
new FieldValidator(mockDefinition.required, mockProps) |
|
96
|
|
|
); |
|
97
|
|
|
throw new Error(); |
|
98
|
|
|
} catch (e) { |
|
99
|
|
|
expect(e).toBeInstanceOf(EntityRequiredFieldsNotFoundError); |
|
100
|
|
|
} |
|
101
|
|
|
}); |
|
102
|
|
|
|
|
103
|
|
|
// Using default implementation for required fields validation - validator returns true using the default implementation in the beforeAll event |
|
104
|
|
|
test("Required field validation succeeds", () => { |
|
105
|
|
|
eg.validateFieldsRequired( |
|
106
|
|
|
new FieldValidator(mockDefinition.required, mockProps) |
|
107
|
|
|
); |
|
108
|
|
|
expect(eg).toBeInstanceOf(Entity); |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
// Non-matching types in required and optional fields returned by the validator (two passes break) |
|
112
|
|
|
test("Required and optional validation fail when the validator does", () => { |
|
113
|
|
|
(FieldValidator as jest.MockedClass<any>) |
|
114
|
|
|
.mockImplementationOnce(() => { |
|
115
|
|
|
return { |
|
116
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
117
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnInvalidField), |
|
118
|
|
|
}; |
|
119
|
|
|
}) |
|
120
|
|
|
.mockImplementationOnce(() => { |
|
121
|
|
|
return { |
|
122
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
123
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnInvalidField), |
|
124
|
|
|
}; |
|
125
|
|
|
}); |
|
126
|
|
|
|
|
127
|
|
|
try { |
|
128
|
|
|
eg.validateFieldsMatchingTypes( |
|
129
|
|
|
new FieldValidator(mockDefinition.required, mockProps), |
|
130
|
|
|
new FieldValidator(mockDefinition.optional, mockProps) |
|
131
|
|
|
); |
|
132
|
|
|
throw new Error(); |
|
133
|
|
|
} catch (e) { |
|
134
|
|
|
expect(e).toBeInstanceOf(EntityInvalidFieldTypesError); |
|
135
|
|
|
} |
|
136
|
|
|
}); |
|
137
|
|
|
|
|
138
|
|
|
// Non-matching types in required fields returned by the validator (first pass breaks) |
|
139
|
|
|
test("Required field validation fails when the validator does", () => { |
|
140
|
|
|
(FieldValidator as jest.MockedClass<any>).mockImplementationOnce(() => { |
|
141
|
|
|
return { |
|
142
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
143
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnInvalidField), |
|
144
|
|
|
}; |
|
145
|
|
|
}); |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
eg.validateFieldsMatchingTypes( |
|
149
|
|
|
new FieldValidator(mockDefinition.required, mockProps), |
|
150
|
|
|
new FieldValidator(mockDefinition.optional, mockProps) |
|
151
|
|
|
); |
|
152
|
|
|
throw new Error(); |
|
153
|
|
|
} catch (e) { |
|
154
|
|
|
expect(e).toBeInstanceOf(EntityInvalidFieldTypesError); |
|
155
|
|
|
} |
|
156
|
|
|
}); |
|
157
|
|
|
|
|
158
|
|
|
// Non-matching types in optional fields returned by the validator (second pass breaks) |
|
159
|
|
|
test("Optional field validation fails when the validator does", () => { |
|
160
|
|
|
(FieldValidator as jest.MockedClass<any>) |
|
161
|
|
|
.mockImplementationOnce(() => { |
|
162
|
|
|
return { |
|
163
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
164
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnTrue), |
|
165
|
|
|
}; |
|
166
|
|
|
}) |
|
167
|
|
|
.mockImplementationOnce(() => { |
|
168
|
|
|
return { |
|
169
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
170
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnInvalidField), |
|
171
|
|
|
}; |
|
172
|
|
|
}); |
|
173
|
|
|
|
|
174
|
|
|
try { |
|
175
|
|
|
eg.validateFieldsMatchingTypes( |
|
176
|
|
|
new FieldValidator(mockDefinition.required, mockProps), |
|
177
|
|
|
new FieldValidator(mockDefinition.optional, mockProps) |
|
178
|
|
|
); |
|
179
|
|
|
throw new Error(); |
|
180
|
|
|
} catch (e) { |
|
181
|
|
|
expect(e).toBeInstanceOf(EntityInvalidFieldTypesError); |
|
182
|
|
|
} |
|
183
|
|
|
}); |
|
184
|
|
|
|
|
185
|
|
|
// Matching fields returned by the validator - using default implementation of the validator in beforeAll event |
|
186
|
|
|
test("Field validation succeeds", () => { |
|
187
|
|
|
eg.validateFieldsMatchingTypes( |
|
188
|
|
|
new FieldValidator(mockDefinition.required, mockProps), |
|
189
|
|
|
new FieldValidator(mockDefinition.optional, mockProps) |
|
190
|
|
|
); |
|
191
|
|
|
expect(eg).toBeInstanceOf(Entity); |
|
192
|
|
|
}); |
|
193
|
|
|
|
|
194
|
|
|
test("Entity can be successfully instantiated with id", () => { |
|
195
|
|
|
const e = new MockEntity(mockProps, new UniqueEntityId()); |
|
196
|
|
|
expect(e).toBeInstanceOf(Entity); |
|
197
|
|
|
}); |
|
198
|
|
|
|
|
199
|
|
|
// Missing required fields returned by the validator |
|
200
|
|
|
test("Entity validation breaks when the validator shows missing required fields", () => { |
|
201
|
|
|
(FieldValidator as jest.MockedClass<any>).mockImplementationOnce(() => { |
|
202
|
|
|
return { |
|
203
|
|
|
allFieldsAvailable: jest.fn(mockReturnMissingField), |
|
204
|
|
|
}; |
|
205
|
|
|
}); |
|
206
|
|
|
|
|
207
|
|
|
try { |
|
208
|
|
|
eg.validate(); |
|
209
|
|
|
throw new Error(); |
|
210
|
|
|
} catch (e) { |
|
211
|
|
|
expect(e).toBeInstanceOf(EntityRequiredFieldsNotFoundError); |
|
212
|
|
|
} |
|
213
|
|
|
}); |
|
214
|
|
|
|
|
215
|
|
|
// Non-matching types in required and optional fields returned by the validator (any one/two passes break) |
|
216
|
|
|
test("Entity validation breaks when the validator shows missing required fields", () => { |
|
217
|
|
|
(FieldValidator as jest.MockedClass<any>) |
|
218
|
|
|
.mockImplementationOnce(() => { |
|
219
|
|
|
return { |
|
220
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
221
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnInvalidField), |
|
222
|
|
|
}; |
|
223
|
|
|
}) |
|
224
|
|
|
.mockImplementationOnce(() => { |
|
225
|
|
|
return { |
|
226
|
|
|
allFieldsAvailable: jest.fn(mockReturnTrue), |
|
227
|
|
|
allFieldsTypeMatch: jest.fn(mockReturnInvalidField), |
|
228
|
|
|
}; |
|
229
|
|
|
}); |
|
230
|
|
|
|
|
231
|
|
|
try { |
|
232
|
|
|
eg.validateFieldsMatchingTypes( |
|
233
|
|
|
new FieldValidator(mockDefinition.required, mockProps), |
|
234
|
|
|
new FieldValidator(mockDefinition.optional, mockProps) |
|
235
|
|
|
); |
|
236
|
|
|
throw new Error(); |
|
237
|
|
|
} catch (e) { |
|
238
|
|
|
expect(e).toBeInstanceOf(EntityInvalidFieldTypesError); |
|
239
|
|
|
} |
|
240
|
|
|
}); |
|
241
|
|
|
|
|
242
|
|
|
test("Entity validation succeeds", () => { |
|
243
|
|
|
eg.validate(); |
|
244
|
|
|
expect(eg).toBeInstanceOf(Entity); |
|
245
|
|
|
}); |
|
246
|
|
|
}); |
|
247
|
|
|
|