File size: 11,135 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
 * @jest-environment jsdom
 */

import ReactDomServer from 'react-dom/server';
import markup from '../markup';

describe( 'markup', () => {
	const site = {};

	describe( '#get()', () => {
		test( 'should return an empty string if not passed any arguments', () => {
			const value = markup.get( site );

			expect( value ).toEqual( '' );
		} );

		test( 'should defer to a specific mime type handler if one exists', () => {
			jest.spyOn( markup.mimeTypes, 'image' );
			markup.get( site, { mime_type: 'image/png' } );

			expect( markup.mimeTypes.image ).toHaveBeenCalled();
		} );

		test( 'should return a link for a mime type prefix without a specific handler', () => {
			jest.spyOn( markup, 'link' );
			markup.get( site, { mime_type: 'application/pdf' } );

			expect( markup.link ).toHaveBeenCalled();
		} );
	} );

	describe( '#link()', () => {
		test( 'should return a link for a mime type prefix without a specific handler', () => {
			const value = markup.link( {
				URL: 'http://example.com/wp-content/uploads/document.pdf',
				title: 'document',
			} );

			expect( value ).toEqual(
				'<a href="http://example.com/wp-content/uploads/document.pdf" title="document">document</a>'
			);
		} );
	} );

	describe( '#caption()', () => {
		test( 'should accept a media object, returning a caption element', () => {
			const value = markup.caption( site, {
				ID: 1,
				URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
				alt: 'Automattic',
				caption: 'Logo',
				thumbnails: {},
				width: 276,
			} );

			expect( value.type ).toEqual( 'dl' );
			expect( ReactDomServer.renderToStaticMarkup( value ) ).toEqual(
				'<dl class="wp-caption" style="width:276px"><dt class="wp-caption-dt"><img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png" alt="Automattic" width="276" class="alignnone size-full wp-image-1"/></dt><dd class="wp-caption-dd">Logo</dd></dl>'
			);
		} );

		test( 'should accept a non-captioned image, returning null', () => {
			const value = markup.caption( site, {
				ID: 1,
				URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
				alt: 'Automattic',
				thumbnails: {},
				width: 276,
			} );

			expect( value ).toBeNull();
		} );

		test( 'should accept a captioned string, returning a React element', () => {
			const value = markup.caption(
				site,
				'[caption id="attachment_1627" align="aligncenter" width="660"]<img class="size-full wp-image-1627" src="https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg" alt="Example" width="660" height="660" /> Ceramic[/caption]'
			);

			expect( value.type ).toEqual( 'dl' );
			expect( ReactDomServer.renderToStaticMarkup( value ) ).toEqual(
				'<dl class="wp-caption aligncenter" style="width:660px"><dt class="wp-caption-dt"><img class="size-full wp-image-1627" src="https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg" alt="Example" width="660" height="660" /></dt><dd class="wp-caption-dd">Ceramic</dd></dl>'
			);
		} );
	} );

	describe( '.mimeTypes', () => {
		describe( '#image()', () => {
			test( 'should not set width auto if media width cannot be determined', () => {
				const value = markup.mimeTypes.image( site, {
					ID: 'media-4',
					URL: 'http%3A//example.com/ddd1d6b0-f31b-4937-ae9e-97f1d660cf71',
					thumbnails: {},
				} );

				expect( value ).toEqual(
					'<img src="http%3A//example.com/ddd1d6b0-f31b-4937-ae9e-97f1d660cf71" class="alignnone size-full wp-image-media-4"/>'
				);
			} );

			test( 'should return an img element for an image', () => {
				const value = markup.mimeTypes.image( site, {
					ID: 1,
					URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
					alt: 'Automattic',
					thumbnails: {},
					width: 276,
				} );

				expect( value ).toEqual(
					'<img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png" alt="Automattic" width="276" class="alignnone size-full wp-image-1"/>'
				);
			} );

			test( 'should respect the max width for a site size', () => {
				const siteWithLargeSize = {
					options: {
						image_large_width: 1024,
						image_large_height: 1024,
					},
				};

				const value = markup.mimeTypes.image(
					siteWithLargeSize,
					{
						ID: 1,
						URL: 'http://example.wordpress.com/image.png',
						thumbnails: {},
						width: 5000,
						height: 2000,
					},
					{ size: 'large' }
				);

				expect( value ).toEqual(
					'<img src="http://example.wordpress.com/image.png?w=1024" width="1024" height="410" class="alignnone size-large wp-image-1"/>'
				);
			} );

			test( 'should respect the max height for a site size', () => {
				const siteWithLargeSize = {
					options: {
						image_large_width: 1024,
						image_large_height: 1024,
					},
				};

				const value = markup.mimeTypes.image(
					siteWithLargeSize,
					{
						ID: 1,
						URL: 'http://example.wordpress.com/image.png',
						thumbnails: {},
						width: 2000,
						height: 5000,
					},
					{ size: 'large' }
				);

				expect( value ).toEqual(
					'<img src="http://example.wordpress.com/image.png?w=410" width="410" height="1024" class="alignnone size-large wp-image-1"/>'
				);
			} );

			test( 'should include a resize parameter if forceResize option passed', () => {
				const value = markup.mimeTypes.image(
					site,
					{
						ID: 1,
						URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
						alt: 'Automattic',
						thumbnails: {},
						width: 276,
					},
					{ forceResize: true }
				);

				expect( value ).toEqual(
					'<img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png?w=276" alt="Automattic" width="276" class="alignnone size-full wp-image-1"/>'
				);
			} );

			test( 'should avoid XSS because React', () => {
				const value = markup.mimeTypes.image( site, {
					ID: 1,
					URL: '""><SCRIPT>alert("XSS")</SCRIPT>"',
					thumbnails: {},
					width: 276,
				} );

				expect( value ).toEqual(
					'<img src="&quot;&quot;&gt;&lt;SCRIPT&gt;alert(&quot;XSS&quot;)&lt;/SCRIPT&gt;&quot;" width="276" class="alignnone size-full wp-image-1"/>'
				);
			} );

			test( "should attempt to find the site's thumbnail width if a size is specified", () => {
				const siteWithWidth = {
					options: {
						image_large_width: 200,
					},
				};

				const value = markup.mimeTypes.image(
					siteWithWidth,
					{
						ID: 1,
						URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
						alt: 'Automattic',
						thumbnails: {},
						width: 276,
					},
					{ size: 'large' }
				);

				expect( value ).toEqual(
					'<img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png?w=200" alt="Automattic" width="200" class="alignnone size-large wp-image-1"/>'
				);
			} );

			test( "should attempt to find the media's own thumbnail width if a size is specified", () => {
				const jetpackSite = { jetpack: true };

				const value = markup.mimeTypes.image(
					jetpackSite,
					{
						ID: 1,
						URL: 'http://example.com/wp-content/uploads/2015/05/logo11w.png',
						alt: 'WordPress',
						thumbnails: {
							large: 'http://example.com/wp-content/uploads/2015/05/logo11w-1024x1024.png',
						},
						width: 5380,
					},
					{ size: 'large' }
				);

				expect( value ).toEqual(
					'<img src="http://example.com/wp-content/uploads/2015/05/logo11w-1024x1024.png" alt="WordPress" width="1024" class="alignnone size-large wp-image-1"/>'
				);
			} );

			test( 'should wrap a captioned image in a caption shortcode', () => {
				const value = markup.mimeTypes.image( site, {
					ID: 1,
					URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
					alt: 'Automattic',
					caption: 'Logo',
					thumbnails: {},
					width: 276,
				} );

				expect( value ).toEqual(
					'[caption id="attachment_1" width="276"]<img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png" alt="Automattic" width="276" class="alignnone size-full wp-image-1"/> Logo[/caption]'
				);
			} );

			test( 'should calculate the height when specifying a size', () => {
				const value = markup.mimeTypes.image(
					site,
					{
						ID: 1,
						URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
						alt: 'Automattic',
						thumbnails: {},
						width: 2760,
						height: 300,
					},
					{ size: 'large' }
				);

				expect( value ).toEqual(
					'<img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png?w=1024" alt="Automattic" width="1024" height="111" class="alignnone size-large wp-image-1"/>'
				);
			} );

			test( 'should include a data-istransient="istransient" attribute when media.transient is truthy', () => {
				const value = markup.mimeTypes.image( site, {
					ID: 1,
					URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
					alt: 'Automattic',
					thumbnails: {},
					width: 2760,
					height: 300,
					transient: true,
				} );

				expect( value ).toEqual(
					'<img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png" alt="Automattic" width="2760" height="300" class="alignnone size-full wp-image-1" data-istransient="istransient"/>'
				);
			} );

			test( 'should not include a data-istransient attribute when media.transient is falsy', () => {
				const value = markup.mimeTypes.image( site, {
					ID: 1,
					URL: 'https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png',
					alt: 'Automattic',
					thumbnails: {},
					width: 2760,
					height: 300,
					transient: false,
				} );

				expect( value ).toEqual(
					'<img src="https://s1.wp.com/wp-content/themes/a8c/automattic-2011/images/automattic-logo.png" alt="Automattic" width="2760" height="300" class="alignnone size-full wp-image-1"/>'
				);
			} );
		} );

		describe( '#audio()', () => {
			test( 'should return an `audio` shortcode for an audio item', () => {
				const value = markup.mimeTypes.audio( site, {
					URL: 'http://example.com/wp-content/uploads/2015/06/loop.mp3',
				} );

				expect( value ).toEqual(
					'[audio src="http://example.com/wp-content/uploads/2015/06/loop.mp3"][/audio]'
				);
			} );
		} );

		describe( '#video()', () => {
			test( 'should return a `wpvideo` shortcode for a VideoPress video', () => {
				const value = markup.mimeTypes.video( site, {
					videopress_guid: '11acMj3O',
				} );

				expect( value ).toEqual( '[wpvideo 11acMj3O]' );
			} );

			test( 'should return a `video` shortcode for a video', () => {
				const value = markup.mimeTypes.video( site, {
					URL: 'http://example.com/wp-content/uploads/2015/06/loop.mp4',
					height: 454,
					width: 1436,
				} );

				expect( value ).toEqual(
					'[video src="http://example.com/wp-content/uploads/2015/06/loop.mp4" height="454" width="1436"][/video]'
				);
			} );
		} );
	} );
} );