react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / underscore.string / test / test_underscore / functions.js
80684 views$(document).ready(function() {12module("Functions");34test("functions: bind", function() {5var context = {name : 'moe'};6var func = function(arg) { return "name: " + (this.name || arg); };7var bound = _.bind(func, context);8equals(bound(), 'name: moe', 'can bind a function to a context');910bound = _(func).bind(context);11equals(bound(), 'name: moe', 'can do OO-style binding');1213bound = _.bind(func, null, 'curly');14equals(bound(), 'name: curly', 'can bind without specifying a context');1516func = function(salutation, name) { return salutation + ': ' + name; };17func = _.bind(func, this, 'hello');18equals(func('moe'), 'hello: moe', 'the function was partially applied in advance');1920var func = _.bind(func, this, 'curly');21equals(func(), 'hello: curly', 'the function was completely applied in advance');2223var func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; };24func = _.bind(func, this, 'hello', 'moe', 'curly');25equals(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');2627func = function(context, message) { equals(this, context, message); };28_.bind(func, 0, 0, 'can bind a function to `0`')();29_.bind(func, '', '', 'can bind a function to an empty string')();30_.bind(func, false, false, 'can bind a function to `false`')();3132// These tests are only meaningful when using a browser without a native bind function33// To test this with a modern browser, set underscore's nativeBind to undefined34var F = function () { return this; };35var Boundf = _.bind(F, {hello: "moe curly"});36equal(new Boundf().hello, undefined, "function should not be bound to the context, to comply with ECMAScript 5");37equal(Boundf().hello, "moe curly", "When called without the new operator, it's OK to be bound to the context");38});3940test("functions: bindAll", function() {41var curly = {name : 'curly'}, moe = {42name : 'moe',43getName : function() { return 'name: ' + this.name; },44sayHi : function() { return 'hi: ' + this.name; }45};46curly.getName = moe.getName;47_.bindAll(moe, 'getName', 'sayHi');48curly.sayHi = moe.sayHi;49equals(curly.getName(), 'name: curly', 'unbound function is bound to current object');50equals(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');5152curly = {name : 'curly'};53moe = {54name : 'moe',55getName : function() { return 'name: ' + this.name; },56sayHi : function() { return 'hi: ' + this.name; }57};58_.bindAll(moe);59curly.sayHi = moe.sayHi;60equals(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');61});6263test("functions: memoize", function() {64var fib = function(n) {65return n < 2 ? n : fib(n - 1) + fib(n - 2);66};67var fastFib = _.memoize(fib);68equals(fib(10), 55, 'a memoized version of fibonacci produces identical results');69equals(fastFib(10), 55, 'a memoized version of fibonacci produces identical results');7071var o = function(str) {72return str;73};74var fastO = _.memoize(o);75equals(o('toString'), 'toString', 'checks hasOwnProperty');76equals(fastO('toString'), 'toString', 'checks hasOwnProperty');77});7879asyncTest("functions: delay", 2, function() {80var delayed = false;81_.delay(function(){ delayed = true; }, 100);82setTimeout(function(){ ok(!delayed, "didn't delay the function quite yet"); }, 50);83setTimeout(function(){ ok(delayed, 'delayed the function'); start(); }, 150);84});8586asyncTest("functions: defer", 1, function() {87var deferred = false;88_.defer(function(bool){ deferred = bool; }, true);89_.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);90});9192asyncTest("functions: throttle", 2, function() {93var counter = 0;94var incr = function(){ counter++; };95var throttledIncr = _.throttle(incr, 100);96throttledIncr(); throttledIncr(); throttledIncr();97setTimeout(throttledIncr, 70);98setTimeout(throttledIncr, 120);99setTimeout(throttledIncr, 140);100setTimeout(throttledIncr, 190);101setTimeout(throttledIncr, 220);102setTimeout(throttledIncr, 240);103_.delay(function(){ ok(counter == 1, "incr was called immediately"); }, 30);104_.delay(function(){ ok(counter == 4, "incr was throttled"); start(); }, 400);105});106107asyncTest("functions: throttle arguments", 2, function() {108var value = 0;109var update = function(val){ value = val; };110var throttledUpdate = _.throttle(update, 100);111throttledUpdate(1); throttledUpdate(2); throttledUpdate(3);112setTimeout(function(){ throttledUpdate(4); }, 120);113setTimeout(function(){ throttledUpdate(5); }, 140);114setTimeout(function(){ throttledUpdate(6); }, 260);115setTimeout(function(){ throttledUpdate(7); }, 270);116_.delay(function(){ ok(value == 1, "updated to latest value"); }, 40);117_.delay(function(){ ok(value == 7, "updated to latest value"); start(); }, 400);118});119120asyncTest("functions: throttle once", 1, function() {121var counter = 0;122var incr = function(){ counter++; };123var throttledIncr = _.throttle(incr, 100);124throttledIncr();125_.delay(function(){ ok(counter == 1, "incr was called once"); start(); }, 220);126});127128asyncTest("functions: throttle twice", 1, function() {129var counter = 0;130var incr = function(){ counter++; };131var throttledIncr = _.throttle(incr, 100);132throttledIncr(); throttledIncr();133_.delay(function(){ ok(counter == 2, "incr was called twice"); start(); }, 220);134});135136asyncTest("functions: debounce", 1, function() {137var counter = 0;138var incr = function(){ counter++; };139var debouncedIncr = _.debounce(incr, 50);140debouncedIncr(); debouncedIncr(); debouncedIncr();141setTimeout(debouncedIncr, 30);142setTimeout(debouncedIncr, 60);143setTimeout(debouncedIncr, 90);144setTimeout(debouncedIncr, 120);145setTimeout(debouncedIncr, 150);146_.delay(function(){ ok(counter == 1, "incr was debounced"); start(); }, 220);147});148149test("functions: once", function() {150var num = 0;151var increment = _.once(function(){ num++; });152increment();153increment();154equals(num, 1);155});156157test("functions: wrap", function() {158var greet = function(name){ return "hi: " + name; };159var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });160equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');161162var inner = function(){ return "Hello "; };163var obj = {name : "Moe"};164obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; });165equals(obj.hi(), "Hello Moe");166});167168test("functions: compose", function() {169var greet = function(name){ return "hi: " + name; };170var exclaim = function(sentence){ return sentence + '!'; };171var composed = _.compose(exclaim, greet);172equals(composed('moe'), 'hi: moe!', 'can compose a function that takes another');173174composed = _.compose(greet, exclaim);175equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');176});177178test("functions: after", function() {179var testAfter = function(afterAmount, timesCalled) {180var afterCalled = 0;181var after = _.after(afterAmount, function() {182afterCalled++;183});184while (timesCalled--) after();185return afterCalled;186};187188equals(testAfter(5, 5), 1, "after(N) should fire after being called N times");189equals(testAfter(5, 4), 0, "after(N) should not fire unless called N times");190});191192});193194195