forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegateService.unit.js
175 lines (156 loc) · 5.92 KB
/
delegateService.unit.js
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
describe('DelegateFactory', function() {
function setup(methods) {
var delegate;
inject(function($log, $injector) {
delegate = $injector.instantiate(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, and make its own handle', function() {
spyOn(ionic.Utils, 'nextUid').andCallFake(function() {
return 'uid';
});
var delegate = setup();
var instance = {};
var deregister = delegate._registerInstance(instance);
expect(ionic.Utils.nextUid).toHaveBeenCalled();
expect(instance.$$delegateHandle).toBe('uid');
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').andCallFake(function() {
return 'ret1';
})
};
var instance2 = {
fn: jasmine.createSpy('fn').andCallFake(function() {
return 'ret2';
})
};
var deregister = delegate._registerInstance(instance1);
delegate._registerInstance(instance2);
expect(delegate.fn()).toBe('ret1');
deregister();
expect(delegate.fn()).toBe('ret2');
});
it('$getByHandle should return this for blank handle', function() {
var delegate = setup();
expect(delegate.$getByHandle()).toBe(delegate);
});
describe('$getByHandle', function() {
var delegate, instance1, instance2, instance3;
beforeEach(function() {
delegate = setup(['a']);
instance1 = {
a: jasmine.createSpy('a1').andCallFake(function() {
return 'a1';
}),
};
instance2 = {
a: jasmine.createSpy('a2').andCallFake(function() {
return 'a2';
}),
};
instance3 = {
a: jasmine.createSpy('a3').andCallFake(function() {
return '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();
var 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();
var result = delegateInstance.a(2);
expect(instance1.a).not.toHaveBeenCalled();
expect(instance2.a).toHaveBeenCalledWith(2);
expect(instance3.a).not.toHaveBeenCalled();
expect(result).toBe('a2');
});
});
});