-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeteor-mock-id.ts
74 lines (64 loc) · 2.08 KB
/
meteor-mock-id.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Meteor } from 'meteor/meteor'
import { Mongo } from 'meteor/mongo'
const constructor = Mongo.Collection
let anonCounter = 0
const newCollection = function (this: Mongo.Collection<unknown>) {
const ret = constructor.apply(this, arguments)
const collection = this
const baseName: string = collection._name ?? `anon${anonCounter++}.`
let counter = 2
const name = baseName.toLowerCase().replace('_', '')
collection._makeNewID = function _makeNewID() {
counter = counters.get(name) || 2
while (
counter.toString().indexOf('0') !== -1 ||
counter.toString().indexOf('1') !== -1
) {
counter++
}
const shortName = name.slice(0, 17 - counter.toString().length)
const newId = `${shortName}${counter++}`.padEnd(17, 'x')
counters.set(name, counter)
return newId
}
// override insert using custom _id (used by dburles:factory)
const _insert = collection.insert
collection.insert = function (doc, ...args) {
if (doc._id) {
const err = new Error()
if (err.stack?.indexOf('packages/dburles:factory/factory.js') !== -1) {
doc._id = this._makeNewID()
}
}
return _insert.call(this, doc, ...args)
}
return ret
}
for (const prop in constructor) {
if (constructor.hasOwnProperty(prop)) {
newCollection[prop] = constructor[prop]
}
}
// don't patch during E2E acceptance test to allow for easy parallelization
if (!process.env.METEOR_E2E_TEST) {
newCollection.prototype = Mongo.Collection.prototype
newCollection.prototype.constructor = newCollection
Mongo.Collection = newCollection
Meteor.Collection = Mongo.Collection
// allow simpl-schema to validate these ids
const RegEx = require('simpl-schema').RegEx
if (RegEx) {
const IdReString = RegEx.Id.toString()
const newReString = `(${IdReString.slice(
1,
IdReString.length - 1,
)})|([a-z]+\\d+x*)`
RegEx.Id = new RegExp(newReString)
}
global.afterEach(resetCounters)
global.beforeEach(resetCounters)
}
let counters = new Map<string, number>()
export function resetCounters() {
counters = new Map<string, number>()
}