1 describe("Ext.Function", function() {
2 describe("bind", function() {
6 beforeEach(function() {
7 fn = jasmine.createSpy("bindSpy");
10 it("should return a function if a function is passed as first argument", function() {
11 bind = Ext.Function.bind(fn, this);
13 expect(typeof bind === "function").toBe(true);
16 it("should use the correct scope", function() {
17 bind = Ext.Function.bind(fn, fakeScope);
21 expect(fn.calls[0].object).toBe(fakeScope);
24 it("should call the first function when it is executed", function() {
25 bind = Ext.Function.bind(fn, this);
29 expect(fn).toHaveBeenCalled();
32 describe("argument passing", function() {
34 it("should use default args if none are passed", function() {
35 bind = Ext.Function.bind(fn, this, ['a', 'b']);
39 expect(fn).toHaveBeenCalledWith('a', 'b');
42 it("should use passed args if they are present", function() {
43 bind = Ext.Function.bind(fn, this);
47 expect(fn).toHaveBeenCalledWith('c', 'd');
50 it("should append args", function() {
51 bind = Ext.Function.bind(fn, this, ['a', 'b'], true);
55 expect(fn).toHaveBeenCalledWith('c', 'd', 'a', 'b');
58 it("should append args at the given index", function() {
59 bind = Ext.Function.bind(fn, this, ['a', 'b'], 0);
63 expect(fn).toHaveBeenCalledWith('a', 'b', 'c', 'd');
68 describe("createInterceptor", function() {
72 interceptorIsRunFirst,
73 interceptedIsRunAfter;
75 beforeEach(function() {
76 interceptorIsRunFirst = false;
77 interceptedIsRunAfter = false;
79 interceptorFn = jasmine.createSpy("interceptorSpy").andCallFake(function() {
80 interceptorIsRunFirst = true;
82 interceptedFn = jasmine.createSpy("interceptedSpy").andCallFake(function() {
83 interceptedIsRunAfter = interceptorIsRunFirst;
87 describe("if no function is passed", function() {
88 it("should return the same function", function() {
89 expect(Ext.Function.createInterceptor(interceptedFn)).toEqual(interceptedFn);
93 describe("if a function is passed", function() {
94 beforeEach(function() {
95 interceptor = Ext.Function.createInterceptor(interceptedFn, interceptorFn, fakeScope);
99 it("should return a new function", function() {
100 expect(typeof interceptor === "function").toBe(true);
101 expect(interceptor).not.toEqual(interceptedFn);
104 it("should set the correct scope for the interceptor function", function() {
105 expect(interceptorFn.calls[0].object).toBe(fakeScope);
108 it("should call the interceptor function first", function() {
109 expect(interceptedIsRunAfter).toBe(true);
114 describe("if the interceptor function returns false", function() {
115 it("should not execute the original function", function() {
116 interceptor = Ext.Function.createInterceptor(interceptedFn, function() {
121 expect(interceptedFn).not.toHaveBeenCalled();
126 describe("defer", function() {
129 beforeEach(function(){
130 fn = jasmine.createSpy("deferSpy");
133 it("should execute the function after the specified number of milliseconds", function() {
137 return fn.calls.length === 1;
138 }, "fn was never called");
141 expect(fn).toHaveBeenCalled();
145 it("should execute the function directly if the specified number of milliseconds is <= 0", function() {
148 expect(fn).toHaveBeenCalled();
151 it("should set the correct scope", function() {
152 Ext.defer(fn, 10, fakeScope);
155 return fn.calls.length === 1;
156 }, "fn was never called");
159 expect(fn.calls[0].object).toBe(fakeScope);
163 it("should pass the correct arguments", function() {
164 Ext.defer(fn, 10, this, [1, 2, 3]);
167 return fn.calls.length === 1;
168 }, "fn was never called");
171 expect(fn).toHaveBeenCalledWith(1,2,3);
175 it("should return a timeout number", function() {
176 expect(typeof Ext.defer(function() {}, 10) === 'number').toBe(true);
180 describe("createSequence", function() {
187 beforeEach(function() {
188 origFnIsRunFirst = false;
189 newFnIsRunAfter = false;
191 origFn = jasmine.createSpy("interceptedSpy").andCallFake(function() {
192 origFnIsRunFirst = true;
195 newFn = jasmine.createSpy("sequenceSpy").andCallFake(function() {
196 newFnIsRunAfter = origFnIsRunFirst;
200 describe("if no function is passed", function() {
201 it("should return the same function", function() {
202 expect(Ext.Function.createSequence(origFn)).toEqual(origFn);
206 describe("if a function is passed", function() {
207 beforeEach(function() {
208 sequence = Ext.Function.createSequence(origFn, newFn, fakeScope);
212 it("should return a new function", function() {
213 expect(typeof sequence === "function").toBe(true);
214 expect(sequence).not.toEqual(origFn);
217 it("should set the correct scope for the sequence function", function() {
218 expect(newFn.calls[0].object).toBe(fakeScope);
221 it("should call the sequence function first", function() {
222 expect(newFnIsRunAfter).toBe(true);