joist-test-utils 1.0.727 → 1.0.730
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/toMatchEntity.js +37 -16
- package/build/toMatchEntity.js.map +1 -1
- package/package.json +1 -1
package/build/toMatchEntity.js
CHANGED
|
@@ -6,12 +6,13 @@ async function toMatchEntity(actual, expected) {
|
|
|
6
6
|
// Because the `actual` entity has lots of __orm, Reference, Collection, etc cruft in it,
|
|
7
7
|
// we make a simplified copy of it, where we use the keys in `expected` to pull out/eval a
|
|
8
8
|
// subset of the complex keys in an entity to be "dumb data" versions of themselves.
|
|
9
|
-
const
|
|
9
|
+
const clean = {};
|
|
10
|
+
const { em } = actual;
|
|
10
11
|
// Because we might assert again `expect(entity).toMatchEntity({ children: [{ name: "p1" }])`, we keep
|
|
11
12
|
// a queue of entities/copies to make, and work through it as we recurse through the expected/actual pair.
|
|
12
|
-
const queue = [[actual, expected,
|
|
13
|
+
const queue = [[actual, expected, clean]];
|
|
13
14
|
while (queue.length > 0) {
|
|
14
|
-
const [actual, expected,
|
|
15
|
+
const [actual, expected, clean] = queue.pop();
|
|
15
16
|
const keys = Object.keys(expected);
|
|
16
17
|
for (const key of keys) {
|
|
17
18
|
const value = actual[key];
|
|
@@ -21,45 +22,65 @@ async function toMatchEntity(actual, expected) {
|
|
|
21
22
|
if (loaded instanceof Array) {
|
|
22
23
|
const actualList = loaded;
|
|
23
24
|
const expectedList = expected[key];
|
|
24
|
-
const
|
|
25
|
+
const cleanList = [];
|
|
25
26
|
// Do a hacky zip of each actual/expected pair
|
|
26
27
|
for (let i = 0; i < Math.max(actualList.length, expectedList.length); i++) {
|
|
27
28
|
const actualI = actualList[i];
|
|
28
29
|
const expectedI = expectedList[i];
|
|
29
30
|
// If actual is a list of entities (and expected is not), make a copy of each
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
// so that we can recurse into their `{ title: ... }` properties.
|
|
32
|
+
if ((0, joist_orm_1.isEntity)(actualI) && !(0, joist_orm_1.isEntity)(expectedI) && expectedI) {
|
|
33
|
+
const cleanI = {};
|
|
34
|
+
queue.push([actualI, expectedI, cleanI]);
|
|
35
|
+
cleanList.push(cleanI);
|
|
34
36
|
}
|
|
35
37
|
else {
|
|
36
|
-
|
|
38
|
+
// Given we're stopping here, make sure neither side is an entity
|
|
39
|
+
if (i < expectedList.length) {
|
|
40
|
+
expectedList[i] = maybeTestId(em, expectedI);
|
|
41
|
+
}
|
|
42
|
+
if (i < actualList.length) {
|
|
43
|
+
cleanList.push(maybeTestId(em, actualI));
|
|
44
|
+
}
|
|
37
45
|
}
|
|
38
46
|
}
|
|
39
|
-
|
|
47
|
+
clean[key] = cleanList;
|
|
40
48
|
}
|
|
41
49
|
else {
|
|
42
50
|
// If the `.load` result wasn't a list, assume it's an entity that we'll copy
|
|
43
51
|
if ((0, joist_orm_1.isEntity)(loaded) && !(0, joist_orm_1.isEntity)(expected[key])) {
|
|
44
|
-
const
|
|
45
|
-
queue.push([loaded, expected[key],
|
|
46
|
-
|
|
52
|
+
const loadedClean = {};
|
|
53
|
+
queue.push([loaded, expected[key], loadedClean]);
|
|
54
|
+
clean[key] = loadedClean;
|
|
47
55
|
}
|
|
48
56
|
else {
|
|
49
|
-
|
|
57
|
+
expected[key] = maybeTestId(em, expected[key]);
|
|
58
|
+
clean[key] = maybeTestId(em, loaded);
|
|
50
59
|
}
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
else {
|
|
54
63
|
// Otherwise assume it's regular data. Probably need to handle getters/promises?
|
|
55
|
-
|
|
64
|
+
clean[key] = value;
|
|
56
65
|
}
|
|
57
66
|
}
|
|
58
67
|
}
|
|
59
68
|
// Blatantly grab `toMatchObject` from the guts of expect
|
|
60
69
|
const { getMatchers } = require("expect/build/jestMatchersObject");
|
|
61
70
|
// @ts-ignore
|
|
62
|
-
return getMatchers().toMatchObject.call(this,
|
|
71
|
+
return getMatchers().toMatchObject.call(this, clean, expected);
|
|
63
72
|
}
|
|
64
73
|
exports.toMatchEntity = toMatchEntity;
|
|
74
|
+
function maybeTestId(em, maybeEntity) {
|
|
75
|
+
return (0, joist_orm_1.isEntity)(maybeEntity) ? getTestId(em, maybeEntity) : maybeEntity;
|
|
76
|
+
}
|
|
77
|
+
/** Returns either the persisted id or `tag#<offset-in-EntityManager>`. */
|
|
78
|
+
function getTestId(em, entity) {
|
|
79
|
+
if (entity.id) {
|
|
80
|
+
return entity.id;
|
|
81
|
+
}
|
|
82
|
+
const meta = (0, joist_orm_1.getMetadata)(entity);
|
|
83
|
+
const sameType = em.entities.filter((e) => e instanceof meta.cstr);
|
|
84
|
+
return `${meta.tagName}#${sameType.indexOf(entity) + 1}`;
|
|
85
|
+
}
|
|
65
86
|
//# sourceMappingURL=toMatchEntity.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toMatchEntity.js","sourceRoot":"","sources":["../src/toMatchEntity.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"toMatchEntity.js","sourceRoot":"","sources":["../src/toMatchEntity.ts"],"names":[],"mappings":";;;AACA,yCASmB;AAEZ,KAAK,UAAU,aAAa,CAAI,MAAc,EAAE,QAA0B;IAC/E,yFAAyF;IACzF,0FAA0F;IAC1F,oFAAoF;IACpF,MAAM,KAAK,GAAG,EAAE,CAAC;IACjB,MAAM,EAAE,EAAE,EAAE,GAAG,MAAM,CAAC;IAEtB,sGAAsG;IACtG,0GAA0G;IAC1G,MAAM,KAAK,GAAsB,CAAC,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC;IAC7D,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;QACvB,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,EAAG,CAAC;QAC/C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC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