-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
176 lines (133 loc) · 4.13 KB
/
test.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
describe('demolish', function () {
'use strict';
var EventEmitter = require('events').EventEmitter
, assume = require('assume')
, demolish = require('./');
it('is exported as a function', function () {
assume(demolish).is.a('function');
});
it('returns a function when executed', function () {
assume(demolish()).is.a('function');
});
it('will emit a destroy event', function (next) {
function Foo() {
this.bar = 1;
}
Foo.prototype.__proto__ = EventEmitter.prototype;
Foo.prototype.destroy = demolish('bar');
var foo = new Foo();
foo.on('destroy', next);
assume(foo.destroy).is.a('function');
foo.destroy();
assume(foo.bar).equals(null);
});
it('calls the destroy method of things', function (next) {
function Foo() {
this.bar = {
destroy: next
};
}
Foo.prototype.__proto__ = EventEmitter.prototype;
Foo.prototype.destroy = demolish('bar');
var foo = new Foo();
assume(foo.destroy).is.a('function');
foo.destroy();
assume(foo.bar).equals(null);
});
it('still allows destruction if prop is falsy', function () {
function Foo() {
this.bar = 0;
}
Foo.prototype.destroy = demolish('bar');
var foo = new Foo();
assume(foo.destroy()).equals(true);
});
it('doesn\'t add null properties when properties don\'t exist', function () {
function Foo() {}
Foo.prototype.destroy = demolish('bar, baz');
var foo = new Foo();
assume(foo.destroy()).equals(true);
assume(foo.bar).equals(undefined);
assume(foo.baz).equals(undefined);
});
it('returns true on first destroy and false on second', function (next) {
function Foo() {
this.bar = 1;
}
Foo.prototype.__proto__ = EventEmitter.prototype;
Foo.prototype.destroy = demolish('bar');
var foo = new Foo();
foo.on('destroy', function () {
assume(foo.destroy()).is.false();
next();
});
assume(foo.destroy()).is.true();
});
it('runs additional before hooks', function () {
function Foo() {
this.bar = 1;
}
Foo.prototype.__proto__ = EventEmitter.prototype;
Foo.prototype.destroy = demolish('bar', {
before: 'removeAllListeners'
});
var foo = new Foo();
/* istanbul ignore next */
foo.on('destroy', function () {
throw new Error('I should never be called as all events should be removed');
});
assume(foo.listeners('destroy').length).equals(1);
assume(foo.destroy()).is.true();
assume(foo.listeners('destroy').length).equals(0);
});
it('runs before hook if its a function', function () {
function Foo() {
this.bar = 1;
}
Foo.prototype.__proto__ = EventEmitter.prototype;
Foo.prototype.destroy = demolish('bar', {
before: function () {
this.removeAllListeners();
}
});
var foo = new Foo();
/* istanbul ignore next */
foo.on('destroy', function () {
throw new Error('I should never be called as all events should be removed');
});
assume(foo.listeners('destroy').length).equals(1);
assume(foo.destroy()).is.true();
assume(foo.listeners('destroy').length).equals(0);
});
it('runs additional after hooks', function (next) {
function Foo() {
this.bar = 1;
}
Foo.prototype.__proto__ = EventEmitter.prototype;
Foo.prototype.destroy = demolish('bar', {
after: 'removeAllListeners'
});
var foo = new Foo();
foo.on('destroy', next);
assume(foo.listeners('destroy').length).equals(1);
assume(foo.destroy()).is.true();
assume(foo.listeners('destroy').length).equals(0);
});
it('can execute before / after functions', function (next) {
function Foo() {
this.bar = 1;
}
Foo.prototype.__proto__ = EventEmitter.prototype;
Foo.prototype.destroy = demolish('bar', {
after: ['removeAllListeners', function () {
assume(this).equals(foo);
next();
}]
});
var foo = new Foo();
foo.on('destroy', function () {});
assume(foo.listeners('destroy').length).equals(1);
assume(foo.destroy()).is.true();
assume(foo.listeners('destroy').length).equals(0);
});
});