prisma-mock 0.0.11 → 0.0.12

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/lib/index.js CHANGED
@@ -79,7 +79,6 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
79
79
  return getCamelCase(model.name) === prop;
80
80
  });
81
81
  model.fields.forEach(field => {
82
- d; //?
83
82
  if (d[field.name] && field.kind === 'object') {
84
83
  const c = d[field.name];
85
84
  if (c.connect) {
@@ -166,87 +165,110 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
166
165
  });
167
166
  return d;
168
167
  };
169
- const matchFnc = args => item => {
168
+ const matchItem = (child, item, where) => {
170
169
  var _a;
171
- if (args === null || args === void 0 ? void 0 : args.where) {
172
- for (let child in args === null || args === void 0 ? void 0 : args.where) {
173
- const val = item[child];
174
- const filter = args.where[child];
175
- if (filter instanceof Date) {
176
- if (val === undefined) {
177
- return false;
178
- }
179
- if (!(val instanceof Date) || val.getTime() !== filter.getTime()) {
180
- return false;
181
- }
170
+ const val = item[child];
171
+ const filter = where[child];
172
+ if (child === "OR") {
173
+ return matchOr(item, filter);
174
+ }
175
+ if (child === "AND") {
176
+ return matchAnd(item, filter);
177
+ }
178
+ if (child === "NOT") {
179
+ return !matchOr(item, filter);
180
+ }
181
+ if (filter instanceof Date) {
182
+ if (val === undefined) {
183
+ return false;
184
+ }
185
+ if (!(val instanceof Date) || val.getTime() !== filter.getTime()) {
186
+ return false;
187
+ }
188
+ }
189
+ else {
190
+ if (typeof filter === 'object') {
191
+ const info = model.fields.find(field => field.name === child);
192
+ if (info === null || info === void 0 ? void 0 : info.relationName) {
193
+ const childName = getCamelCase(info.type);
194
+ const res = data[childName].filter(matchFnc(Object.assign(Object.assign({}, filter), getFieldRelationshipWhere(item, info))));
195
+ return res.length > 0;
182
196
  }
183
- else {
184
- if (typeof filter === 'object') {
185
- const info = model.fields.find(field => field.name === child);
186
- if (info === null || info === void 0 ? void 0 : info.relationName) {
187
- const childName = getCamelCase(info.type);
188
- const res = data[childName].filter(matchFnc({
189
- where: Object.assign(Object.assign({}, filter), getFieldRelationshipWhere(item, info)),
190
- }));
191
- return res.length > 0;
192
- }
193
- const idFields = model.idFields || ((_a = model.primaryKey) === null || _a === void 0 ? void 0 : _a.fields);
194
- if ((idFields === null || idFields === void 0 ? void 0 : idFields.length) > 1) {
195
- if (child === idFields.join('_')) {
196
- return shallowCompare(val, filter);
197
- }
198
- }
199
- if (val === undefined) {
200
- return false;
201
- }
202
- let match = true;
203
- if ('equals' in filter && match) {
204
- match = filter.equals === val;
205
- }
206
- if ('startsWith' in filter && match) {
207
- match = val.indexOf(filter.startsWith) === 0;
208
- }
209
- if ('endsWith' in filter && match) {
210
- match =
211
- val.indexOf(filter.endsWith) ===
212
- val.length - filter.endsWith.length;
213
- }
214
- if ('contains' in filter && match) {
215
- match = val.indexOf(filter.contains) > -1;
216
- }
217
- if ('gt' in filter && match) {
218
- match = val > filter.gt;
219
- }
220
- if ('gte' in filter && match) {
221
- match = val >= filter.gte;
222
- }
223
- if ('lt' in filter && match) {
224
- match = val < filter.lt;
225
- }
226
- if ('lte' in filter && match) {
227
- match = val <= filter.lte;
228
- }
229
- if ('in' in filter && match) {
230
- match = filter.in.includes(val);
231
- }
232
- if (!match)
233
- return false;
234
- }
235
- else if (val !== filter) {
236
- return false;
197
+ const idFields = model.idFields || ((_a = model.primaryKey) === null || _a === void 0 ? void 0 : _a.fields);
198
+ if ((idFields === null || idFields === void 0 ? void 0 : idFields.length) > 1) {
199
+ if (child === idFields.join('_')) {
200
+ return shallowCompare(val, filter);
237
201
  }
238
202
  }
203
+ if (val === undefined) {
204
+ return false;
205
+ }
206
+ let match = true;
207
+ if ('equals' in filter && match) {
208
+ match = filter.equals === val;
209
+ }
210
+ if ('startsWith' in filter && match) {
211
+ match = val.indexOf(filter.startsWith) === 0;
212
+ }
213
+ if ('endsWith' in filter && match) {
214
+ match =
215
+ val.indexOf(filter.endsWith) ===
216
+ val.length - filter.endsWith.length;
217
+ }
218
+ if ('contains' in filter && match) {
219
+ match = val.indexOf(filter.contains) > -1;
220
+ }
221
+ if ('gt' in filter && match) {
222
+ match = val > filter.gt;
223
+ }
224
+ if ('gte' in filter && match) {
225
+ match = val >= filter.gte;
226
+ }
227
+ if ('lt' in filter && match) {
228
+ match = val < filter.lt;
229
+ }
230
+ if ('lte' in filter && match) {
231
+ match = val <= filter.lte;
232
+ }
233
+ if ('in' in filter && match) {
234
+ match = filter.in.includes(val);
235
+ }
236
+ if (!match)
237
+ return false;
239
238
  }
239
+ else if (val !== filter) {
240
+ return false;
241
+ }
242
+ }
243
+ return true;
244
+ };
245
+ const matchItems = (item, where) => {
246
+ for (let child in where) {
247
+ if (!matchItem(child, item, where)) {
248
+ return false;
249
+ }
250
+ }
251
+ return true;
252
+ };
253
+ const matchAnd = (item, where) => {
254
+ return where.filter((child) => matchItems(item, child)).length > 0;
255
+ };
256
+ const matchOr = (item, where) => {
257
+ return where.some((child) => matchItems(item, child));
258
+ };
259
+ const matchFnc = where => item => {
260
+ if (where) {
261
+ return matchItems(item, where);
240
262
  }
241
263
  return true;
242
264
  };
243
265
  const findOne = args => {
244
266
  if (!data[prop])
245
267
  throw new Error(`${prop} not found in data`);
246
- return includes(args)(data[prop].find(matchFnc(args)));
268
+ return includes(args)(data[prop].find(matchFnc(args.where)));
247
269
  };
248
270
  const findMany = args => {
249
- const res = data[prop].filter(matchFnc(args)).map(includes(args));
271
+ const res = data[prop].filter(matchFnc(args === null || args === void 0 ? void 0 : args.where)).map(includes(args));
250
272
  if (args === null || args === void 0 ? void 0 : args.distinct) {
251
273
  let values = {};
252
274
  return res.filter(item => {
@@ -267,7 +289,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
267
289
  return res;
268
290
  };
269
291
  const findFirst = args => {
270
- const item = data[prop].find(matchFnc(args));
292
+ const item = data[prop].find(matchFnc(args === null || args === void 0 ? void 0 : args.where));
271
293
  if (item)
272
294
  return includes(args)(item);
273
295
  return null;
@@ -277,7 +299,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
277
299
  // throw new Error(`${prop} not found in data`)
278
300
  // }
279
301
  const newItems = data[prop].map(e => {
280
- if (matchFnc(args)(e)) {
302
+ if (matchFnc(args.where)(e)) {
281
303
  let data = nestedUpdate(args, false);
282
304
  return Object.assign(Object.assign({}, e), data);
283
305
  }
@@ -289,12 +311,11 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
289
311
  const create = args => {
290
312
  const d = nestedUpdate(args, true);
291
313
  data = Object.assign(Object.assign({}, data), { [prop]: [...data[prop], d] });
292
- data; //?
293
314
  data = checkIds(model, data);
294
315
  return findOne(Object.assign({ where: { id: d.id } }, args));
295
316
  };
296
317
  const deleteMany = args => {
297
- data = Object.assign(Object.assign({}, data), { [prop]: data[prop].filter(e => !matchFnc(args)(e)) });
318
+ data = Object.assign(Object.assign({}, data), { [prop]: data[prop].filter(e => !matchFnc(args === null || args === void 0 ? void 0 : args.where)(e)) });
298
319
  };
299
320
  const includes = args => item => {
300
321
  if (!(args === null || args === void 0 ? void 0 : args.include) || !item)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",
package/lib/mock.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import { PrismaMockData } from ".";
2
- declare const mock: <P>(moduleName: string, data?: Partial<{ [key in Uncapitalize<keyof P extends string ? string & keyof P : never> extends `$${infer fnc}` ? never : Uncapitalize<keyof P extends string ? string & keyof P : never>]: P[key] extends {
3
- findUnique: (...args: any[]) => Promise<any>;
4
- } ? Partial<ReturnType<P[key]["findUnique"]> extends Promise<infer R> ? R : ReturnType<P[key]["findUnique"]>>[] : never; }>, pathToSchema?: string) => void;
5
- export default mock;
package/lib/mock.js DELETED
@@ -1,15 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const jest_mock_extended_1 = require("jest-mock-extended");
7
- const _1 = __importDefault(require("."));
8
- const mock = (moduleName, data = {}, pathToSchema) => {
9
- jest.mock(moduleName, () => (0, jest_mock_extended_1.mockDeep)());
10
- const db = jest.requireMock(moduleName);
11
- beforeEach(() => {
12
- return (0, _1.default)(data, pathToSchema, db);
13
- });
14
- };
15
- exports.default = mock;