prisma-mock 0.5.3 → 0.5.5
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/README.md +1 -1
- package/lib/index.js +15 -3
- package/lib/utils/deepCopy.d.ts +1 -0
- package/lib/utils/deepCopy.js +14 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -205,7 +205,7 @@ TODO (path, string_contains, string_starts_with, string_ends_with, array_contain
|
|
|
205
205
|
## Prisma Client methods
|
|
206
206
|
|
|
207
207
|
- $transaction
|
|
208
|
-
-
|
|
208
|
+
- $transaction (interactive)
|
|
209
209
|
- TODO: $transaction (isolation)
|
|
210
210
|
|
|
211
211
|
# Contribution
|
package/lib/index.js
CHANGED
|
@@ -29,6 +29,7 @@ const jest_mock_extended_1 = require("jest-mock-extended");
|
|
|
29
29
|
const defaults_1 = __importStar(require("./defaults"));
|
|
30
30
|
const shallowCompare_1 = require("./utils/shallowCompare");
|
|
31
31
|
const deepEqual_1 = require("./utils/deepEqual");
|
|
32
|
+
const deepCopy_1 = require("./utils/deepCopy");
|
|
32
33
|
function IsFieldDefault(f) {
|
|
33
34
|
return f.name !== undefined;
|
|
34
35
|
}
|
|
@@ -135,10 +136,21 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
135
136
|
};
|
|
136
137
|
client["$transaction"].mockImplementation(async (actions) => {
|
|
137
138
|
const res = [];
|
|
138
|
-
|
|
139
|
-
|
|
139
|
+
if (Array.isArray(actions)) {
|
|
140
|
+
for (const action of actions) {
|
|
141
|
+
res.push(await action);
|
|
142
|
+
}
|
|
143
|
+
return res;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const snapshot = (0, deepCopy_1.deepCopy)(data);
|
|
147
|
+
try {
|
|
148
|
+
await actions(client);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
data = snapshot;
|
|
152
|
+
}
|
|
140
153
|
}
|
|
141
|
-
return res;
|
|
142
154
|
});
|
|
143
155
|
client["$connect"].mockImplementation(async () => { });
|
|
144
156
|
client["$disconnect"].mockImplementation(async () => { });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deepCopy<T>(source: T): T;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deepCopy = void 0;
|
|
4
|
+
function deepCopy(source) {
|
|
5
|
+
if (Object(source) !== source) {
|
|
6
|
+
// primitive
|
|
7
|
+
return source;
|
|
8
|
+
}
|
|
9
|
+
else if (Array.isArray(source)) {
|
|
10
|
+
return source.map(deepCopy);
|
|
11
|
+
}
|
|
12
|
+
return Object.fromEntries(Object.entries(source).map(([k, v]) => ([k, deepCopy(v)])));
|
|
13
|
+
}
|
|
14
|
+
exports.deepCopy = deepCopy;
|