-
-
Notifications
You must be signed in to change notification settings - Fork 10k
/
img_url_spec.js
202 lines (176 loc) · 7.48 KB
/
img_url_spec.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
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
var should = require('should'),
sinon = require('sinon'),
urlUtils = require('../../utils/urlUtils'),
// Stuff we are testing
helpers = require('../../../frontend/helpers'),
common = require('../../../server/lib/common');
describe('{{image}} helper', function () {
var logWarnStub;
beforeEach(function () {
logWarnStub = sinon.stub(common.logging, 'warn');
});
afterEach(function () {
sinon.restore();
});
describe('without sub-directory', function () {
let sandbox;
before(function () {
sandbox = sinon.createSandbox();
urlUtils.stubUrlUtils({url: 'http://localhost:82832/'}, sandbox);
});
after(function () {
sandbox.restore();
});
it('should output relative url of image', function () {
var rendered = helpers.img_url('/content/images/image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should output relative url of image if the input is absolute', function () {
var rendered = helpers.img_url('http://localhost:82832/content/images/image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should output absolute url of image if the option is present ', function () {
var rendered = helpers.img_url('/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
should.exist(rendered);
rendered.should.equal('http://localhost:82832/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should NOT output absolute url of image if the option is "false" ', function () {
var rendered = helpers.img_url('/content/images/image-relative-url.png', {hash: {absolute: 'false'}});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
});
it('should output author url', function () {
var rendered = helpers.img_url('/content/images/author-image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/author-image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should have no output if the image attributeis not provided (with warning)', function () {
var rendered = helpers.img_url({hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.true();
});
it('should have no output if the image attribute evaluates to undefined (with warning)', function () {
var rendered = helpers.img_url(undefined, {hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.true();
});
it('should have no output if the image attribute evaluates to null (no waring)', function () {
var rendered = helpers.img_url(null, {hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.false();
});
});
describe('with sub-directory', function () {
let sandbox;
before(function () {
sandbox = sinon.createSandbox();
urlUtils.stubUrlUtils({url: 'http://localhost:82832/blog'}, sandbox);
});
after(function () {
sandbox.restore();
});
it('should output relative url of image', function () {
var rendered = helpers.img_url('/blog/content/images/image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/blog/content/images/image-relative-url.png');
});
it('should output absolute url of image if the option is present ', function () {
var rendered = helpers.img_url('/blog/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
should.exist(rendered);
rendered.should.equal('http://localhost:82832/blog/content/images/image-relative-url.png');
});
it('should not change output for an external url', function () {
var rendered = helpers.img_url('http://example.com/picture.jpg', {});
should.exist(rendered);
rendered.should.equal('http://example.com/picture.jpg');
});
});
describe('image_sizes', function () {
let sandbox;
before(function () {
sandbox = sinon.createSandbox();
urlUtils.stubUrlUtils({url: 'http://localhost:82832/'}, sandbox);
});
after(function () {
sandbox.restore();
});
it('should output correct url for absolute paths which are internal', function () {
var rendered = helpers.img_url('http://localhost:82832/content/images/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
});
it('should output the correct url for protocol relative urls', function () {
var rendered = helpers.img_url('//website.com/whatever/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
rendered.should.equal('//website.com/whatever/my-coole-img.jpg');
});
it('should output the correct url for relative paths', function () {
var rendered = helpers.img_url('/content/images/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
});
it('should output the correct url for relative paths without leading slash', function () {
var rendered = helpers.img_url('content/images/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
rendered.should.equal('content/images/size/w400/my-coole-img.jpg');
});
});
});