| 1 |
'use strict'; |
| 2 |
|
| 3 |
var expect = require('expect'); |
| 4 |
|
| 5 |
var defaultResolution = require('default-resolution'); |
| 6 |
|
| 7 |
var lastRun = require('../'); |
| 8 |
|
| 9 |
describe('lastRun', function() { |
| 10 |
|
| 11 |
var since; |
| 12 |
|
| 13 |
beforeEach(function(done) { |
| 14 |
since = Date.now(); |
| 15 |
// Account for default resolution |
| 16 |
since = since - (since % defaultResolution()); |
| 17 |
done(); |
| 18 |
}); |
| 19 |
|
| 20 |
it('should record function capture time', function(done) { |
| 21 |
function test() {} |
| 22 |
|
| 23 |
lastRun.capture(test); |
| 24 |
|
| 25 |
expect(lastRun(test)).toExist(); |
| 26 |
expect(lastRun(test)).toBeLessThanOrEqualTo(Date.now()); |
| 27 |
done(); |
| 28 |
}); |
| 29 |
|
| 30 |
it('should accept a timestamp', function(done) { |
| 31 |
function test() {} |
| 32 |
|
| 33 |
lastRun.capture(test, since); |
| 34 |
|
| 35 |
expect(lastRun(test)).toExist(); |
| 36 |
expect(lastRun(test)).toEqual(since); |
| 37 |
done(); |
| 38 |
}); |
| 39 |
|
| 40 |
it('removes last run time with release method', function(done) { |
| 41 |
function test() {} |
| 42 |
|
| 43 |
lastRun.capture(test); |
| 44 |
|
| 45 |
expect(lastRun(test)).toExist(); |
| 46 |
|
| 47 |
lastRun.release(test); |
| 48 |
|
| 49 |
expect(lastRun(test)).toNotExist(); |
| 50 |
done(); |
| 51 |
}); |
| 52 |
|
| 53 |
it('does not error on release if not captures', function(done) { |
| 54 |
function test() {} |
| 55 |
|
| 56 |
lastRun.release(test); |
| 57 |
|
| 58 |
expect(lastRun(test)).toNotExist(); |
| 59 |
done(); |
| 60 |
}); |
| 61 |
|
| 62 |
it('should return undefined for a function not captured', function(done) { |
| 63 |
function test() {} |
| 64 |
|
| 65 |
expect(lastRun(test)).toNotExist(); |
| 66 |
done(); |
| 67 |
}); |
| 68 |
|
| 69 |
it('should throw on non-functions', function(done) { |
| 70 |
function obj() { |
| 71 |
lastRun({}); |
| 72 |
} |
| 73 |
|
| 74 |
function str() { |
| 75 |
lastRun('wat'); |
| 76 |
} |
| 77 |
|
| 78 |
function num() { |
| 79 |
lastRun(1); |
| 80 |
} |
| 81 |
|
| 82 |
function undef() { |
| 83 |
lastRun(undefined); |
| 84 |
} |
| 85 |
|
| 86 |
function nul() { |
| 87 |
lastRun(null); |
| 88 |
} |
| 89 |
|
| 90 |
expect(obj).toThrow('Only functions can check lastRun'); |
| 91 |
expect(str).toThrow('Only functions can check lastRun'); |
| 92 |
expect(num).toThrow('Only functions can check lastRun'); |
| 93 |
expect(undef).toThrow('Only functions can check lastRun'); |
| 94 |
expect(nul).toThrow('Only functions can check lastRun'); |
| 95 |
done(); |
| 96 |
}); |
| 97 |
|
| 98 |
it('works with anonymous functions', function(done) { |
| 99 |
var test = function() {}; |
| 100 |
|
| 101 |
lastRun.capture(test); |
| 102 |
|
| 103 |
expect(lastRun(test)).toExist(); |
| 104 |
expect(lastRun(test)).toBeLessThanOrEqualTo(Date.now()); |
| 105 |
done(); |
| 106 |
}); |
| 107 |
|
| 108 |
it('should give time with 1s resolution', function(done) { |
| 109 |
var resolution = 1000; // 1s |
| 110 |
since = Date.now(); |
| 111 |
since = since - (since % resolution); |
| 112 |
|
| 113 |
function test() {} |
| 114 |
lastRun.capture(test); |
| 115 |
|
| 116 |
expect(lastRun(test, resolution)).toEqual(since); |
| 117 |
done(); |
| 118 |
}); |
| 119 |
|
| 120 |
it('should accept a string for resolution', function(done) { |
| 121 |
var resolution = '1000'; // 1s |
| 122 |
since = Date.now(); |
| 123 |
since = since - (since % 1000); |
| 124 |
|
| 125 |
function test() {} |
| 126 |
lastRun.capture(test); |
| 127 |
|
| 128 |
expect(lastRun(test, resolution)).toEqual(since); |
| 129 |
done(); |
| 130 |
}); |
| 131 |
|
| 132 |
it('should use default resolution when forced to 0ms resolution', function(done) { |
| 133 |
var resolution = 0; |
| 134 |
|
| 135 |
function test() {} |
| 136 |
lastRun.capture(test); |
| 137 |
|
| 138 |
expect(lastRun(test, resolution)).toEqual(since); |
| 139 |
done(); |
| 140 |
}); |
| 141 |
|
| 142 |
it('throws on non-enumerable functions when using weakmap shim', function(done) { |
| 143 |
|
| 144 |
function extensions() { |
| 145 |
var test = function() {}; |
| 146 |
Object.preventExtensions(test); |
| 147 |
lastRun.capture(test); |
| 148 |
} |
| 149 |
|
| 150 |
function seal() { |
| 151 |
var test = function() {}; |
| 152 |
Object.seal(test); |
| 153 |
lastRun.capture(test); |
| 154 |
} |
| 155 |
|
| 156 |
function freeze() { |
| 157 |
var test = function() {}; |
| 158 |
Object.freeze(test); |
| 159 |
lastRun.capture(test); |
| 160 |
} |
| 161 |
|
| 162 |
if (/v0.10/.test(process.version)) { |
| 163 |
expect(extensions).toThrow('Only extensible functions can be captured'); |
| 164 |
expect(seal).toThrow('Only extensible functions can be captured'); |
| 165 |
expect(freeze).toThrow('Only extensible functions can be captured'); |
| 166 |
} else { |
| 167 |
expect(extensions).toNotThrow(); |
| 168 |
expect(seal).toNotThrow(); |
| 169 |
expect(freeze).toNotThrow(); |
| 170 |
} |
| 171 |
done(); |
| 172 |
}); |
| 173 |
|
| 174 |
}); |