-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
index.js
91 lines (69 loc) · 1.95 KB
/
index.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eslint-env browser */
/**
* Wraps the given class constructor to keep track of the
* created object and set it on the window object.
*/
export function CypressSingleton (name) {
return function (target) {
// @ts-ignore
if (!window.Cypress) {
return target
}
// save a reference to the original constructor
const original = target
// a utility function to generate instances of a class
function construct (constructor, args) {
const c = function () {
return constructor.apply(this, args)
}
c.prototype = constructor.prototype
return new c()
}
// the new constructor behavior
const f = function (...args) {
const instance = construct(original, args)
// @ts-ignore
window[name || target.name] = instance
return instance
}
// copy prototype so intanceof operator still works
f.prototype = original.prototype
return f
}
}
/**
* Wraps the given constructor and puts all created instances
* into a list attached to the "window" object.
*/
export function CypressInstances (name) {
return function (target) {
// @ts-ignore
if (!window.Cypress) {
return target
}
// save a reference to the original constructor
const original = target
// a utility function to generate instances of a class
function construct (constructor, args) {
const c = function () {
return constructor.apply(this, args)
}
c.prototype = constructor.prototype
return new c()
}
// the new constructor behavior
const f = function (...args) {
const instance = construct(original, args)
const listName = name || target.name
// @ts-ignore
if (!window[listName]) {
window[listName] = []
}
window[listName].push(instance)
return instance
}
// copy prototype so intanceof operator still works
f.prototype = original.prototype
return f
}
}