3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
15 describe("Ext.Function", function() {
16 describe("bind", function() {
20 beforeEach(function() {
21 fn = jasmine.createSpy("bindSpy");
24 it("should return a function if a function is passed as first argument", function() {
25 bind = Ext.Function.bind(fn, this);
27 expect(typeof bind === "function").toBe(true);
30 it("should use the correct scope", function() {
31 bind = Ext.Function.bind(fn, fakeScope);
35 expect(fn.calls[0].object).toBe(fakeScope);
38 it("should call the first function when it is executed", function() {
39 bind = Ext.Function.bind(fn, this);
43 expect(fn).toHaveBeenCalled();
46 describe("argument passing", function() {
48 it("should use default args if none are passed", function() {
49 bind = Ext.Function.bind(fn, this, ['a', 'b']);
53 expect(fn).toHaveBeenCalledWith('a', 'b');
56 it("should use passed args if they are present", function() {
57 bind = Ext.Function.bind(fn, this);
61 expect(fn).toHaveBeenCalledWith('c', 'd');
64 it("should append args", function() {
65 bind = Ext.Function.bind(fn, this, ['a', 'b'], true);
69 expect(fn).toHaveBeenCalledWith('c', 'd', 'a', 'b');
72 it("should append args at the given index", function() {
73 bind = Ext.Function.bind(fn, this, ['a', 'b'], 0);
77 expect(fn).toHaveBeenCalledWith('a', 'b', 'c', 'd');
82 describe("createInterceptor", function() {
86 interceptorIsRunFirst,
87 interceptedIsRunAfter;
89 beforeEach(function() {
90 interceptorIsRunFirst = false;
91 interceptedIsRunAfter = false;
93 interceptorFn = jasmine.createSpy("interceptorSpy").andCallFake(function() {
94 interceptorIsRunFirst = true;
96 interceptedFn = jasmine.createSpy("interceptedSpy").andCallFake(function() {
97 interceptedIsRunAfter = interceptorIsRunFirst;
101 describe("if no function is passed", function() {
102 it("should return the same function", function() {
103 expect(Ext.Function.createInterceptor(interceptedFn)).toEqual(interceptedFn);
107 describe("if a function is passed", function() {
108 beforeEach(function() {
109 interceptor = Ext.Function.createInterceptor(interceptedFn, interceptorFn, fakeScope);
113 it("should return a new function", function() {
114 expect(typeof interceptor === "function").toBe(true);
115 expect(interceptor).not.toEqual(interceptedFn);
118 it("should set the correct scope for the interceptor function", function() {
119 expect(interceptorFn.calls[0].object).toBe(fakeScope);
122 it("should call the interceptor function first", function() {
123 expect(interceptedIsRunAfter).toBe(true);
128 describe("if the interceptor function returns false", function() {
129 it("should not execute the original function", function() {
130 interceptor = Ext.Function.createInterceptor(interceptedFn, function() {
135 expect(interceptedFn).not.toHaveBeenCalled();
140 describe("defer", function() {
143 beforeEach(function(){
144 fn = jasmine.createSpy("deferSpy");
147 it("should execute the function after the specified number of milliseconds", function() {
151 return fn.calls.length === 1;
152 }, "fn was never called");
155 expect(fn).toHaveBeenCalled();
159 it("should execute the function directly if the specified number of milliseconds is <= 0", function() {
162 expect(fn).toHaveBeenCalled();
165 it("should set the correct scope", function() {
166 Ext.defer(fn, 10, fakeScope);
169 return fn.calls.length === 1;
170 }, "fn was never called");
173 expect(fn.calls[0].object).toBe(fakeScope);
177 it("should pass the correct arguments", function() {
178 Ext.defer(fn, 10, this, [1, 2, 3]);
181 return fn.calls.length === 1;
182 }, "fn was never called");
185 expect(fn).toHaveBeenCalledWith(1,2,3);
189 it("should return a timeout number", function() {
190 expect(typeof Ext.defer(function() {}, 10) === 'number').toBe(true);
194 describe("createSequence", function() {
201 beforeEach(function() {
202 origFnIsRunFirst = false;
203 newFnIsRunAfter = false;
205 origFn = jasmine.createSpy("interceptedSpy").andCallFake(function() {
206 origFnIsRunFirst = true;
209 newFn = jasmine.createSpy("sequenceSpy").andCallFake(function() {
210 newFnIsRunAfter = origFnIsRunFirst;
214 describe("if no function is passed", function() {
215 it("should return the same function", function() {
216 expect(Ext.Function.createSequence(origFn)).toEqual(origFn);
220 describe("if a function is passed", function() {
221 beforeEach(function() {
222 sequence = Ext.Function.createSequence(origFn, newFn, fakeScope);
226 it("should return a new function", function() {
227 expect(typeof sequence === "function").toBe(true);
228 expect(sequence).not.toEqual(origFn);
231 it("should set the correct scope for the sequence function", function() {
232 expect(newFn.calls[0].object).toBe(fakeScope);
235 it("should call the sequence function first", function() {
236 expect(newFnIsRunAfter).toBe(true);