tricks.test.js
3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
var tricks = require('../src/js/tricks');
describe('tricks', function() {
describe('debounce', function() {
var spy;
beforeEach(function() {
spyOn(window, 'setTimeout');
spy = jasmine.createSpy('debounced?');
});
it('test debounced functions.', function() {
var fn = tricks.debounce(spy, 50);
fn();
expect(window.setTimeout).toHaveBeenCalledWith(jasmine.any(Function), 50);
window.setTimeout.calls.argsFor(0)[0]();
expect(spy).toHaveBeenCalled();
});
it('debounced function can accept parameters', function() {
var fn;
window.setTimeout.and.callFake(function(func) {
func();
});
fn = tricks.debounce(spy);
fn('hello world!');
expect(spy.calls.argsFor(0)).toEqual(['hello world!']);
});
});
describe('throttle', function() {
var spy;
function reload(arr) {
var i = 0,
bullet;
function fire() {
bullet = arr[i];
i += 1;
return bullet;
}
return fire;
}
beforeEach(function() {
spy = jasmine.createSpy('throttled?');
});
it('test throttled functions.', function() {
var magazine = [1, 3, 6, 8, 9],
fireGun = reload(magazine),
fn;
spyOn(tricks, 'timestamp').and.callFake(function() {
return fireGun();
});
spyOn(tricks, 'debounce').and.returnValue(function() {});
fn = tricks.throttle(spy, 7);
fn();
fn();
fn();
fn();
fn();
expect(spy.calls.count()).toBe(2);
expect(tricks.debounce).toHaveBeenCalled();
});
it('debounced method must invoke with additional parameter', function() {
var magazine = [1, 3, 6, 8, 9],
fireGun = reload(magazine),
fn,
debounced;
spyOn(tricks, 'timestamp').and.callFake(function() {
return fireGun();
});
debounced = jasmine.createSpy('debounced');
spyOn(tricks, 'debounce').and.returnValue(debounced);
fn = tricks.throttle(spy, 7);
fn('hello');
fn('hello');
fn('hello');
fn('hello');
fn('hello');
expect(spy.calls.count()).toBe(2);
expect(tricks.debounce).toHaveBeenCalled();
expect(debounced.calls.count()).toBe(4);
// debounce가 이미 콜백을 apply 처리하고 있는데, Mock을 했기 때문에
// args 를 그냥 넘겨준다 따라서 TC에서만은 각 debounce의 파라미터가 배열 형태임.
expect(debounced.calls.allArgs()).toEqual([
[['hello']],
[['hello']],
[['hello']],
[['hello']]
]);
});
it('reset can remove slugs related with throttling.', function() {
var magazine = [1, 3, 6, 8, 9],
fireGun = reload(magazine),
fn;
spyOn(tricks, 'timestamp').and.callFake(function() {
return fireGun();
});
fn = tricks.throttle(spy, 7);
fn();
expect(spy.calls.count()).toBe(1);
fn.reset();
fireGun = reload(magazine);
fn();
expect(spy.calls.count()).toBe(2);
});
it('throttled functions can accept parameters.', function() {
var fn = tricks.throttle(spy);
fn('hello world!');
expect(spy.calls.argsFor(0)).toEqual(['hello world!']);
});
});
});