/** * @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( 'document' ); } ); } ); 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( '
Automattic
Logo
' ); } ); 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"]Example Ceramic[/caption]' ); expect( value.type ).toEqual( 'dl' ); expect( ReactDomServer.renderToStaticMarkup( value ) ).toEqual( '
Example
Ceramic
' ); } ); } ); 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( '' ); } ); 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( 'Automattic' ); } ); 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( '' ); } ); 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( '' ); } ); 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( 'Automattic' ); } ); test( 'should avoid XSS because React', () => { const value = markup.mimeTypes.image( site, { ID: 1, URL: '"">"', thumbnails: {}, width: 276, } ); expect( value ).toEqual( '' ); } ); 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( 'Automattic' ); } ); 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( 'WordPress' ); } ); 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"]Automattic 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( 'Automattic' ); } ); 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( 'Automattic' ); } ); 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( 'Automattic' ); } ); } ); 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]' ); } ); } ); } ); } );