Add _.once tests for memorized return values.

This commit is contained in:
John-David Dalton
2014-06-10 09:50:10 -07:00
parent 885b56000b
commit c72171d1da

View File

@@ -6839,33 +6839,33 @@
QUnit.module('lodash.once'); QUnit.module('lodash.once');
(function() { (function() {
test('should execute `func` once', 1, function() { test('should execute `func` once', 2, function() {
var count = 0, var count = 0,
once = _.once(function() { count++; }); once = _.once(function() { return ++count; });
once(); once();
once(); strictEqual(once(), 1);
strictEqual(count, 1); strictEqual(count, 1);
}); });
test('should not set a `this` binding', 1, function() { test('should not set a `this` binding', 2, function() {
var once = _.once(function() { this.count++; }), var once = _.once(function() { return ++this.count; }),
object = { 'count': 0, 'once': once }; object = { 'count': 0, 'once': once };
object.once(); object.once();
object.once(); strictEqual(object.once(), 1);
strictEqual(object.count, 1); strictEqual(object.count, 1);
}); });
test('should ignore recursive calls', 1, function() { test('should ignore recursive calls', 2, function() {
var count = 0; var count = 0;
var once = _.once(function() { var once = _.once(function() {
count++;
once(); once();
return ++count;
}); });
once(); strictEqual(once(), 1);
strictEqual(count, 1); strictEqual(count, 1);
}); });