-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
179 lines (141 loc) · 4.81 KB
/
index.spec.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
177
178
179
/*
* bouncer
* https://github.com/3rd-party-bouncer/bouncer
*
* Licensed under the MIT license.
*/
'use strict';
var Bouncer = require( './' );
var Runner = require( './lib/runner' );
var proxyquire = require( 'proxyquire' );
var util = require( 'util' );
var EventEmitter = require( 'events' ).EventEmitter;
module.exports = {
bouncer : {
constructor : {
setOptions : function( test ) {
var options = {
allowedDomains : [ 'foo.com', 'bar.io' ],
key : 'xxx',
runs : 42,
server : 'somewebpagetest.instance',
url : 'http://www.perf-tooling.today'
};
var bouncer = new Bouncer( options );
test.equal( bouncer.options.runner.allowedDomains, options.allowedDomains );
test.equal( bouncer.options.runner.allowedDomains.length, 2 );
test.equal( bouncer.options.runner.allowedDomains[ 0 ].domain, 'foo' );
test.equal( bouncer.options.runner.allowedDomains[ 0 ].tld, 'com' );
test.equal( bouncer.options.runner.allowedDomains[ 1 ].domain, 'bar' );
test.equal( bouncer.options.runner.allowedDomains[ 1 ].tld, 'io' );
test.equal( bouncer.options.runner.url, options.url );
test.equal( bouncer.options.wpt.key, options.key );
test.equal( bouncer.options.wpt.runs, options.runs );
test.equal( bouncer.options.wpt.server, options.server );
test.equal( bouncer.runner instanceof Runner, true );
test.equal( typeof bouncer.on, 'function' );
test.equal( typeof bouncer.emit, 'function' );
test.done();
},
emittedEvents : {
msg : function( test ) {
var bouncer = new Bouncer( {
url : 'foo',
allowedDomains : [ 'bar', 'baz' ]
} );
bouncer.on( 'bouncer:msg', function( msg ) {
test.equal( msg, 'A random message' );
test.done();
} );
bouncer.runner.emit( 'bouncer:msg', 'A random message' );
},
error : function( test ) {
var bouncer = new Bouncer( {
url : 'foo',
allowedDomains : [ 'bar', 'baz' ]
} );
bouncer.on( 'bouncer:error', function( msg ) {
test.equal( msg, 'A random error' );
test.done();
} );
bouncer.runner.emit( 'bouncer:error', 'A random error' );
},
data : function( test ) {
var bouncer = new Bouncer( {
url : 'foo',
allowedDomains : [ 'bar', 'baz' ]
} );
bouncer.on( 'bouncer:data', function( msg ) {
test.equal( msg, 'Received data' );
test.done();
} );
bouncer.runner.emit( 'bouncer:data', 'Received data' );
},
debug : function( test ) {
var bouncer = new Bouncer( {
url : 'foo',
allowedDomains : [ 'bar', 'baz' ]
} );
bouncer.on( 'bouncer:debug', function( msg ) {
test.equal( msg, 'A random debug message' );
test.done();
} );
bouncer.runner.emit( 'bouncer:debug', 'A random debug message' );
}
},
error : {
noUrlError : function ( test ) {
test.expect( 1 );
test.throws( function() {
new Bouncer( {} );
}, Error );
test.done();
}
}
},
run : {
callback : {
success : function( test ) {
var RunnerStub = function() {
this.run = function( callback ) {
callback( null, 'whoooop' );
}
};
util.inherits( RunnerStub, EventEmitter );
var Bouncer = proxyquire(
'./',
{
'./lib/runner' : RunnerStub
}
);
var bouncer = new Bouncer( { url : 'fooo', allowedDomains : [ 'foo' ] } );
bouncer.run( function( error, data ) {
test.equal( error, null );
test.equal( data, 'whoooop' );
test.done();
} );
},
error : function( test ) {
var RunnerStub = function() {
this.run = function( callback ) {
callback( new Error( 'Error' ) );
}
};
util.inherits( RunnerStub, EventEmitter );
var Bouncer = proxyquire(
'./',
{
'./lib/runner' : RunnerStub
}
);
var bouncer = new Bouncer( { url : 'fooo', allowedDomains : [ 'foo' ] } );
bouncer.run( function( error, data ) {
test.equal( error instanceof Error, true );
test.equal( error.message, 'Error' );
test.done();
} );
}
}
}
}
};