-
-
Notifications
You must be signed in to change notification settings - Fork 810
/
Copy pathtest.js
131 lines (111 loc) · 2.69 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
'use strict';
// MODULES //
var tape = require( 'tape' );
var proxyquire = require( 'proxyquire' );
var detect = require( './../lib' );
// VARIABLES //
var hasWeakMap = ( typeof WeakMap === 'function' );
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof detect, 'function', 'main export is a function' );
t.end();
});
tape( 'feature detection result is a boolean', function test( t ) {
t.equal( typeof detect(), 'boolean', 'detection result is a boolean' );
t.end();
});
tape( 'if `WeakMap` is supported, detection result is `true`', function test( t ) {
var mocked;
if ( hasWeakMap ) {
t.equal( detect(), true, 'detection result is `true`' );
} else {
t.equal( detect(), false, 'detection result is `false`' );
}
mocked = proxyquire( './../lib/detect_weakmap_support.js', {
'./weakmap.js': Mock
});
t.equal( mocked(), true, 'detection result is `true` (mocked)' );
t.end();
function Mock() {
var o = {};
function has( key ) {
return o.hasOwnProperty( key );
}
function set( key, value ) {
o[ key ] = value;
}
function get( key ) {
return o[ key ];
}
return {
'has': has,
'set': set,
'get': get
};
}
});
tape( 'if `WeakMap` is not supported, detection result is `false`', function test( t ) {
var mocked;
if ( hasWeakMap ) {
t.equal( detect(), true, 'detection result is `true`' );
} else {
t.equal( detect(), false, 'detection result is `false`' );
}
mocked = proxyquire( './../lib/detect_weakmap_support.js', {
'./weakmap.js': {}
});
t.equal( mocked(), false, 'detection result is `false` (mocked)' );
mocked = proxyquire( './../lib/detect_weakmap_support.js', {
'./weakmap.js': Mock1
});
t.equal( mocked(), false, 'detection result is `false` (no `has` method)' );
mocked = proxyquire( './../lib/detect_weakmap_support.js', {
'./weakmap.js': Mock2
});
t.equal( mocked(), false, 'detection result is `false` (no `set` method)' );
mocked = proxyquire( './../lib/detect_weakmap_support.js', {
'./weakmap.js': Mock3
});
t.equal( mocked(), false, 'detection result is `false` (no `get` method)' );
t.end();
function Mock1() {
var o = {};
function set( key, value ) {
o[ key ] = value;
}
function get( key ) {
return o[ key ];
}
return {
'set': set,
'get': get
};
}
function Mock2() {
var o = {};
function has( key ) {
return o.hasOwnProperty( key );
}
function get( key ) {
return o[ key ];
}
return {
'has': has,
'get': get
};
}
function Mock3() {
var o = {};
function has( key ) {
return o.hasOwnProperty( key );
}
function set( key, value ) {
o[ key ] = value;
}
return {
'has': has,
'set': set
};
}
});