forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelegateService.unit.js
More file actions
233 lines (200 loc) · 8.23 KB
/
delegateService.unit.js
File metadata and controls
233 lines (200 loc) · 8.23 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
describe('DelegateFactory', function() {
function setup(methods) {
var delegate;
inject(function($log, $injector) {
delegate = $injector.instantiate(ionic.DelegateService(methods || []));
});
return delegate;
}
it('should have properties', function() {
expect(setup()._instances).toEqual([]);
expect(setup()._registerInstance).toEqual(jasmine.any(Function));
expect(setup().$getByHandle).toEqual(jasmine.any(Function));
});
it('should allow reg & dereg of instance with handle', function() {
var delegate = setup();
var instance = {};
var deregister = delegate._registerInstance(instance, 'handle');
expect(instance.$$delegateHandle).toBe('handle');
expect(delegate._instances[0]).toBe(instance);
deregister();
expect(delegate._instances.length).toBe(0);
});
it('should allow reg & dereg of instance, without handle', function() {
var delegate = setup();
var instance = {};
var deregister = delegate._registerInstance(instance, null);
expect(instance.$$delegateHandle).toBe(null);
expect(delegate._instances[0]).toBe(instance);
deregister();
expect(delegate._instances.length).toBe(0);
});
it('should have given methodNames on root', function() {
var delegate = setup(['foo', 'bar']);
expect(delegate.foo).toEqual(jasmine.any(Function));
expect(delegate.bar).toEqual(jasmine.any(Function));
});
it('should call given methodNames on all instances with args', function() {
var delegate = setup(['foo', 'bar']);
var instance1 = {
foo: jasmine.createSpy('foo1'),
bar: jasmine.createSpy('bar1')
};
var instance2 = {
foo: jasmine.createSpy('foo1'),
bar: jasmine.createSpy('bar1')
};
delegate._registerInstance(instance1);
delegate._registerInstance(instance2);
expect(instance1.foo).not.toHaveBeenCalled();
expect(instance2.foo).not.toHaveBeenCalled();
delegate.foo('a', 'b', 'c');
expect(instance1.foo).toHaveBeenCalledWith('a', 'b', 'c');
expect(instance2.foo).toHaveBeenCalledWith('a', 'b', 'c');
instance1.foo.reset();
instance2.foo.reset();
delegate.foo(1, 2, 3);
expect(instance1.foo).toHaveBeenCalledWith(1, 2, 3);
expect(instance2.foo).toHaveBeenCalledWith(1, 2, 3);
expect(instance1.bar).not.toHaveBeenCalled();
expect(instance2.bar).not.toHaveBeenCalled();
delegate.bar('something');
expect(instance1.bar).toHaveBeenCalledWith('something');
expect(instance2.bar).toHaveBeenCalledWith('something');
});
it('should return the first return value from multiple instances', function() {
var delegate = setup(['fn']);
var instance1 = {
fn: jasmine.createSpy('fn').andReturn('ret1')
};
var instance2 = {
fn: jasmine.createSpy('fn').andReturn('ret2')
};
var deregister = delegate._registerInstance(instance1);
delegate._registerInstance(instance2);
expect(delegate.fn()).toBe('ret1');
deregister();
expect(delegate.fn()).toBe('ret2');
});
it('should return the first active instance return value when multiple instances w/ undefined handle', function() {
var delegate = setup(['fn']);
var instance1 = {
fn: jasmine.createSpy('fn').andReturn('ret1')
};
var instance2 = {
fn: jasmine.createSpy('fn').andReturn('ret2')
};
delegate._registerInstance(instance1, undefined, function() {
return false;
});
delegate._registerInstance(instance2, undefined, function() {
return true;
});
expect(instance1.fn).not.toHaveBeenCalled();
expect(delegate.fn()).toBe('ret2');
});
it('should return the first active instance return value when multiple instances w/ same handle', function() {
var delegate = setup(['fn']);
var instance1 = {
fn: jasmine.createSpy('fn').andReturn('ret1')
};
var instance2 = {
fn: jasmine.createSpy('fn').andReturn('ret2')
};
delegate._registerInstance(instance1, 'myhandle', function() {
return true;
});
delegate._registerInstance(instance2, 'myhandle', function() {
return false;
});
expect(instance2.fn).not.toHaveBeenCalled();
expect(delegate.fn()).toBe('ret1');
});
describe('$getByHandle', function() {
var delegate, instance1, instance2, instance3;
beforeEach(function() {
delegate = setup(['a']);
instance1 = {
a: jasmine.createSpy('a1').andReturn('a1')
};
instance2 = {
a: jasmine.createSpy('a2').andReturn('a2')
};
instance3 = {
a: jasmine.createSpy('a3').andReturn('a3')
};
});
it('should return an InstanceWithHandle object with fields', function() {
expect(delegate.$getByHandle('one').a).toEqual(jasmine.any(Function));
expect(delegate.$getByHandle('two').a).toEqual(jasmine.any(Function));
expect(delegate.$getByHandle('invalid').a).toEqual(jasmine.any(Function));
});
it('should noop & warn if calling for a non-added instance', inject(function($log) {
spyOn($log, 'warn');
expect(delegate.$getByHandle('one').a()).toBeUndefined();
expect($log.warn).toHaveBeenCalled();
}));
it('should call an added instance\'s method', function() {
delegate._registerInstance(instance1, '1');
delegate._registerInstance(instance2, '2');
var result = delegate.$getByHandle('1').a(1,2,3);
expect(instance1.a).toHaveBeenCalledWith(1,2,3);
expect(instance2.a).not.toHaveBeenCalled();
expect(result).toBe('a1');
instance1.a.reset();
result = delegate.$getByHandle('2').a(2,3,4);
expect(instance2.a).toHaveBeenCalledWith(2,3,4);
expect(instance1.a).not.toHaveBeenCalled();
expect(result).toBe('a2');
});
it('should call multiple instances with the same handle', function() {
var deregister = delegate._registerInstance(instance1, '1');
delegate._registerInstance(instance2, '1');
delegate._registerInstance(instance3, 'other');
var delegateInstance = delegate.$getByHandle('1');
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).not.toHaveBeenCalled();
expect(instance3.a).not.toHaveBeenCalled();
var result = delegateInstance.a('1');
expect(instance1.a).toHaveBeenCalledWith('1');
expect(instance2.a).toHaveBeenCalledWith('1');
expect(instance3.a).not.toHaveBeenCalled();
expect(result).toBe('a1');
instance1.a.reset();
deregister();
result = delegateInstance.a(2);
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).toHaveBeenCalledWith(2);
expect(instance3.a).not.toHaveBeenCalled();
expect(result).toBe('a2');
});
it('should get the only active instance from multiple instances with the same handle', function() {
delegate._registerInstance(instance1, 'myhandle', function() { return false; });
delegate._registerInstance(instance2, 'myhandle', function() { return true; });
delegate._registerInstance(instance3, 'myhandle', function() { return false; });
var delegateInstance = delegate.$getByHandle('myhandle');
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).not.toHaveBeenCalled();
expect(instance3.a).not.toHaveBeenCalled();
var result = delegateInstance.a('test-a');
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).toHaveBeenCalledWith('test-a');
expect(instance3.a).not.toHaveBeenCalled();
expect(result).toBe('a2');
});
it('should get active instance when multiple sname name handles, among multiple active instances', function() {
delegate._registerInstance(instance1, 'myhandle-1', function() { return true; });
delegate._registerInstance(instance2, 'myhandle-2', function() { return false; });
delegate._registerInstance(instance3, 'myhandle-2', function() { return true; });
var delegateInstance = delegate.$getByHandle('myhandle-2');
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).not.toHaveBeenCalled();
expect(instance3.a).not.toHaveBeenCalled();
var result = delegateInstance.a('test-a');
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).not.toHaveBeenCalled();
expect(instance3.a).toHaveBeenCalledWith('test-a');
expect(result).toBe('a3');
});
});
});