Welcome to mirror list, hosted at ThFree Co, Russian Federation.

jquery.avatarSpec.js « specs « tests « js « core - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e13b7f26ff3b794be556a81bec6770ab393053f (plain)
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
/**
 * Copyright (c) 2015 Roeland Jago Douma <roeland@famdouma.nl>
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */

describe('jquery.avatar tests', function() {
	
	var $div;
	var devicePixelRatio;

	beforeEach(function() {
		$('#testArea').append($('<div id="avatardiv">'));
		$div = $('#avatardiv');

		devicePixelRatio = window.devicePixelRatio;
		window.devicePixelRatio = 1;

		spyOn(window, 'Image').and.returnValue({
			onload: function() {
			},
			onerror: function() {
			}
		});
	});

	afterEach(function() {
		$div.remove();

		window.devicePixelRatio = devicePixelRatio;
	});

	describe('size', function() {
		it('undefined', function() {
			$div.avatar('foo');

			expect($div.height()).toEqual(64);
			expect($div.width()).toEqual(64);
		});

		it('undefined but div has height', function() {
			$div.height(9);
			$div.avatar('foo');

			expect(window.Image).toHaveBeenCalled();
			window.Image().onerror();

			expect($div.height()).toEqual(9);
			expect($div.width()).toEqual(9);
		});

		it('undefined but data size is set', function() {
			$div.data('size', 10);
			$div.avatar('foo');

			expect(window.Image).toHaveBeenCalled();
			window.Image().onerror();

			expect($div.height()).toEqual(10);
			expect($div.width()).toEqual(10);
		});


		it('defined', function() {
			$div.avatar('foo', 8);

			expect(window.Image).toHaveBeenCalled();
			window.Image().onerror();

			expect($div.height()).toEqual(8);
			expect($div.width()).toEqual(8);
		});
	});

	it('undefined user', function() {
		spyOn($div, 'imageplaceholder');
		spyOn($div, 'css');

		$div.avatar();
		
		expect($div.imageplaceholder).toHaveBeenCalledWith('?');
		expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
	});

	describe('no avatar', function() {
		it('show placeholder for existing user', function() {
			spyOn($div, 'imageplaceholder');
			$div.avatar('foo', undefined, undefined, undefined, undefined, 'bar');

			expect(window.Image).toHaveBeenCalled();
			window.Image().onerror();
			expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'bar');
		});

		it('show placeholder for non existing user', function() {
			spyOn($div, 'imageplaceholder');
			spyOn($div, 'css');
			$div.avatar('foo');

			expect(window.Image).toHaveBeenCalled();
			window.Image().onerror();

			expect($div.imageplaceholder).toHaveBeenCalledWith('?');
			expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
		});

		it('show no placeholder is ignored', function() {
			spyOn($div, 'imageplaceholder');
			spyOn($div, 'css');
			$div.avatar('foo', undefined, undefined, true);

			expect(window.Image).toHaveBeenCalled();
			window.Image().onerror();

			expect($div.imageplaceholder).toHaveBeenCalledWith('?');
			expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
		});
	});

	describe('url generation', function() {
		beforeEach(function() {
			window.devicePixelRatio = 1;
		});

		it('default', function() {
			window.devicePixelRatio = 1;
			$div.avatar('foo', 32);

			expect(window.Image).toHaveBeenCalled();
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
		});

		it('high DPI icon', function() {
			window.devicePixelRatio = 4;
			$div.avatar('foo', 32);

			expect(window.Image).toHaveBeenCalled();
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/128');
		});

		it('high DPI icon round up size', function() {
			window.devicePixelRatio = 1.9;
			$div.avatar('foo', 32);

			expect(window.Image).toHaveBeenCalled();
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
		});
	});

	describe('valid avatar', function() {
		beforeEach(function() {
			window.devicePixelRatio = 1;
		});

		it('default (no ie8 fix)', function() {
			$div.avatar('foo', 32);

			expect(window.Image).toHaveBeenCalled();
			window.Image().onload();

			expect(window.Image().height).toEqual(32);
			expect(window.Image().width).toEqual(32);
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
		});

		it('default high DPI icon', function() {
			window.devicePixelRatio = 1.9;

			$div.avatar('foo', 32);

			expect(window.Image).toHaveBeenCalled();
			window.Image().onload();

			expect(window.Image().height).toEqual(32);
			expect(window.Image().width).toEqual(32);
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
		});

		it('with ie8 fix (ignored)', function() {
			$div.avatar('foo', 32, true);

			expect(window.Image).toHaveBeenCalled();
			window.Image().onload();

			expect(window.Image().height).toEqual(32);
			expect(window.Image().width).toEqual(32);
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
		});

		it('unhide div', function() {
			$div.hide();

			$div.avatar('foo', 32);

			expect(window.Image).toHaveBeenCalled();
			window.Image().onload();

			expect(window.Image().height).toEqual(32);
			expect(window.Image().width).toEqual(32);
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
		});

		it('callback called', function() {
			var observer = {callback: function() { dump("FOO"); }};

			spyOn(observer, 'callback');

			$div.avatar('foo', 32, undefined, undefined, function() {
				observer.callback();
			});

			expect(window.Image).toHaveBeenCalled();
			window.Image().onload();

			expect(window.Image().height).toEqual(32);
			expect(window.Image().width).toEqual(32);
			expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
			expect(observer.callback).toHaveBeenCalled();
		});
	});
});