joist-test-utils 1.35.3 → 1.36.0
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/index.d.ts +1 -1
- package/build/toMatchEntity.d.ts +3 -2
- package/build/toMatchEntity.js +48 -49
- package/build/toMatchEntity.js.map +1 -1
- package/package.json +4 -1
package/build/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { toMatchEntity } from "./toMatchEntity";
|
|
|
5
5
|
declare global {
|
|
6
6
|
namespace jest {
|
|
7
7
|
interface Matchers<R, T = {}> {
|
|
8
|
-
toMatchEntity(expected: MatchedEntity<T>):
|
|
8
|
+
toMatchEntity(expected: MatchedEntity<T>): CustomMatcherResult;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
}
|
package/build/toMatchEntity.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="jest" />
|
|
2
2
|
import CustomMatcherResult = jest.CustomMatcherResult;
|
|
3
3
|
import { AsyncProperty, Collection, Entity, Reference } from "joist-orm";
|
|
4
|
-
export declare function toMatchEntity<T>(actual: Entity, expected: MatchedEntity<T>):
|
|
4
|
+
export declare function toMatchEntity<T>(actual: Entity, expected: MatchedEntity<T>): CustomMatcherResult;
|
|
5
5
|
/**
|
|
6
6
|
* Given a Joist entity `T`, "flattens out" the Reference/Collections.
|
|
7
7
|
*
|
|
@@ -12,5 +12,6 @@ export declare function toMatchEntity<T>(actual: Entity, expected: MatchedEntity
|
|
|
12
12
|
* that are returned from GraphQL object resolvers.
|
|
13
13
|
*/
|
|
14
14
|
export declare type MatchedEntity<T> = {
|
|
15
|
-
[K in keyof T]?: T[K] extends Reference<any, infer U, any> ? MatchedEntity<U> | U : T[K] extends Collection<any, infer U> ? Array<MatchedEntity<U> | U> : T[K] extends AsyncProperty<any, infer V> ? V : T[K] extends Entity
|
|
15
|
+
[K in keyof T]?: T[K] extends Reference<any, infer U, any> ? MatchedEntity<U> | U : T[K] extends Collection<any, infer U> ? Array<MatchedEntity<U> | U> : T[K] extends AsyncProperty<any, infer V> ? V : T[K] extends Entity | null | undefined ? MatchedEntity<T[K]> | T[K] | null | undefined : T[K] extends ReadonlyArray<infer U | undefined> ? readonly (MatchedEntity<U> | U | undefined)[] : T[K] extends ReadonlyArray<infer U> | null ? readonly (MatchedEntity<U> | U | null)[] : // We recurse similar to a DeepPartial
|
|
16
|
+
MatchedEntity<T[K]> | null;
|
|
16
17
|
};
|
package/build/toMatchEntity.js
CHANGED
|
@@ -6,68 +6,67 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.toMatchEntity = void 0;
|
|
7
7
|
// @ts-ignore
|
|
8
8
|
const matchers_1 = __importDefault(require("expect/build/matchers"));
|
|
9
|
+
const is_plain_object_1 = require("is-plain-object");
|
|
9
10
|
const joist_orm_1 = require("joist-orm");
|
|
10
|
-
|
|
11
|
+
function toMatchEntity(actual, expected) {
|
|
11
12
|
// Because the `actual` entity has lots of __orm, Reference, Collection, etc cruft in it,
|
|
12
13
|
// we make a simplified copy of it, where we use the keys in `expected` to pull out/eval a
|
|
13
14
|
// subset of the complex keys in an entity to be "dumb data" versions of themselves.
|
|
14
|
-
const clean = {};
|
|
15
|
-
const { em } = actual;
|
|
15
|
+
const clean = Array.isArray(expected) ? [] : {};
|
|
16
16
|
// Because we might assert again `expect(entity).toMatchEntity({ children: [{ name: "p1" }])`, we keep
|
|
17
17
|
// a queue of entities/copies to make, and work through it as we recurse through the expected/actual pair.
|
|
18
18
|
const queue = [[actual, expected, clean]];
|
|
19
19
|
while (queue.length > 0) {
|
|
20
20
|
const [actual, expected, clean] = queue.pop();
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (i < expectedList.length) {
|
|
46
|
-
expectedList[i] = maybeTestId(em, expectedI);
|
|
47
|
-
}
|
|
48
|
-
if (i < actualList.length) {
|
|
49
|
-
cleanList.push(maybeTestId(em, actualI));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
clean[key] = cleanList;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
// If the `.load` result wasn't a list, assume it's an entity that we'll copy
|
|
57
|
-
if ((0, joist_orm_1.isEntity)(loaded) && !(0, joist_orm_1.isEntity)(expected[key])) {
|
|
58
|
-
const loadedClean = {};
|
|
59
|
-
queue.push([loaded, expected[key], loadedClean]);
|
|
60
|
-
clean[key] = loadedClean;
|
|
21
|
+
for (const key of Object.keys(expected)) {
|
|
22
|
+
let actualValue = actual[key];
|
|
23
|
+
const expectedValue = expected?.[key];
|
|
24
|
+
// We assume the test is asserting already-loaded / DeepNew entities, so just call .get
|
|
25
|
+
if ((0, joist_orm_1.isReference)(actualValue) ||
|
|
26
|
+
(0, joist_orm_1.isCollection)(actualValue) ||
|
|
27
|
+
(0, joist_orm_1.isAsyncProperty)(actualValue) ||
|
|
28
|
+
(0, joist_orm_1.isPersistedAsyncProperty)(actualValue)) {
|
|
29
|
+
actualValue = actualValue.get;
|
|
30
|
+
}
|
|
31
|
+
if (actualValue instanceof Array) {
|
|
32
|
+
const actualList = actualValue;
|
|
33
|
+
const expectedList = expectedValue;
|
|
34
|
+
const cleanList = [];
|
|
35
|
+
// Do a hacky zip of each actual/expected pair
|
|
36
|
+
for (let i = 0; i < Math.max(actualList.length, expectedList.length); i++) {
|
|
37
|
+
const actualI = actualList[i];
|
|
38
|
+
const expectedI = expectedList[i];
|
|
39
|
+
// If actual is a list of entities (and expected is not), make a copy of each
|
|
40
|
+
// so that we can recurse into their `{ title: ... }` properties.
|
|
41
|
+
if ((0, joist_orm_1.isEntity)(actualI) && (0, is_plain_object_1.isPlainObject)(expectedI)) {
|
|
42
|
+
const cleanI = Array.isArray(expectedI) ? [] : {};
|
|
43
|
+
queue.push([actualI, expectedI, cleanI]);
|
|
44
|
+
cleanList.push(cleanI);
|
|
61
45
|
}
|
|
62
46
|
else {
|
|
63
|
-
|
|
64
|
-
|
|
47
|
+
// Given we're stopping here, make sure neither side is an entity
|
|
48
|
+
if (i < expectedList.length) {
|
|
49
|
+
expectedList[i] = maybeTestId(expectedI);
|
|
50
|
+
}
|
|
51
|
+
if (i < actualList.length) {
|
|
52
|
+
cleanList.push(maybeTestId(actualI));
|
|
53
|
+
}
|
|
65
54
|
}
|
|
66
55
|
}
|
|
56
|
+
clean[key] = cleanList;
|
|
57
|
+
}
|
|
58
|
+
else if ((0, is_plain_object_1.isPlainObject)(expectedValue)) {
|
|
59
|
+
// We have an expected value that the user wants to fuzzy match against, so recurse
|
|
60
|
+
// to pick out a subset of clean values (i.e. not the connection pool) to assert against.
|
|
61
|
+
const cleanValue = Array.isArray(expectedValue) ? [] : {};
|
|
62
|
+
queue.push([actualValue, expectedValue, cleanValue]);
|
|
63
|
+
clean[key] = cleanValue;
|
|
67
64
|
}
|
|
68
65
|
else {
|
|
69
|
-
//
|
|
70
|
-
clean
|
|
66
|
+
// We've hit a non-list/non-object literal expected value, so clean both
|
|
67
|
+
// expected+clean keys to make sure they're not entities, and stop recursion.
|
|
68
|
+
expected[key] = maybeTestId(expectedValue);
|
|
69
|
+
clean[key] = maybeTestId(actualValue);
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
}
|
|
@@ -75,8 +74,8 @@ async function toMatchEntity(actual, expected) {
|
|
|
75
74
|
return matchers_1.default.toMatchObject.call(this, clean, expected);
|
|
76
75
|
}
|
|
77
76
|
exports.toMatchEntity = toMatchEntity;
|
|
78
|
-
function maybeTestId(
|
|
79
|
-
return (0, joist_orm_1.isEntity)(maybeEntity) ? getTestId(em, maybeEntity) : maybeEntity;
|
|
77
|
+
function maybeTestId(maybeEntity) {
|
|
78
|
+
return (0, joist_orm_1.isEntity)(maybeEntity) ? getTestId(maybeEntity.em, maybeEntity) : maybeEntity;
|
|
80
79
|
}
|
|
81
80
|
/** Returns either the persisted id or `tag#<offset-in-EntityManager>`. */
|
|
82
81
|
function getTestId(em, entity) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toMatchEntity.js","sourceRoot":"","sources":["../src/toMatchEntity.ts"],"names":[],"mappings":";;;;;;AACA,aAAa;AACb,qEAA6C;AAC7C,yCAYmB;
|
|
1
|
+
{"version":3,"file":"toMatchEntity.js","sourceRoot":"","sources":["../src/toMatchEntity.ts"],"names":[],"mappings":";;;;;;AACA,aAAa;AACb,qEAA6C;AAC7C,qDAAgD;AAChD,yCAYmB;AAEnB,SAAgB,aAAa,CAAI,MAAc,EAAE,QAA0B;IACzE,yFAAyF;IACzF,0FAA0F;IAC1F,oFAAoF;IACpF,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAEhD,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,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YACvC,IAAI,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YAC9B,MAAM,aAAa,GAAG,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;YAEtC,uFAAuF;YACvF,IACE,IAAA,uBAAW,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AC,CAAC,YAAY,IAAI,CAAC,IAAI,CAAC,CAAC;IACnE,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;AAC3D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "joist-test-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.36.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"build"
|
|
12
12
|
],
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"is-plain-object": "^5.0.0"
|
|
15
|
+
},
|
|
13
16
|
"devDependencies": {
|
|
14
17
|
"@swc/core": "^1.2.220",
|
|
15
18
|
"@swc/jest": "^0.2.22",
|