File size: 5,790 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 |
/**
* @jest-environment jsdom
*/
import { deserialize } from '../';
import { MediaTypes } from '../constants';
describe( 'MediaSerialization', () => {
describe( '#deserialize()', () => {
test( 'should parse a caption shortcode string containing an image', () => {
const parsed = deserialize(
'[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( parsed.type ).toEqual( MediaTypes.IMAGE );
expect( parsed.media.ID ).toEqual( 1627 );
expect( parsed.media.caption ).toEqual( 'Ceramic' );
expect( parsed.media.URL ).toEqual(
'https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg'
);
expect( parsed.media.alt ).toEqual( 'Example' );
expect( parsed.media.transient ).toBe( false );
expect( parsed.media.width ).toEqual( 660 );
expect( parsed.media.height ).toEqual( 660 );
expect( parsed.appearance.align ).toEqual( 'center' );
expect( parsed.appearance.size ).toEqual( 'full' );
} );
test( 'should parse an image string', () => {
const parsed = deserialize(
'<img class="size-full wp-image-1627 alignright" src="https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg" alt="Example" width="660" height="660" />'
);
expect( parsed.type ).toEqual( MediaTypes.IMAGE );
expect( parsed.media.ID ).toEqual( 1627 );
expect( parsed.media.URL ).toEqual(
'https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg'
);
expect( parsed.media.alt ).toEqual( 'Example' );
expect( parsed.media.transient ).toBe( false );
expect( parsed.media.width ).toEqual( 660 );
expect( parsed.media.height ).toEqual( 660 );
expect( parsed.appearance.size ).toEqual( 'full' );
expect( parsed.appearance.align ).toEqual( 'right' );
} );
test( 'should parse an image HTMLElement', () => {
const img = document.createElement( 'img' );
img.className = 'size-full wp-image-1627 alignright';
img.src = 'https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg';
img.alt = 'Example';
img.width = 660;
img.height = 660;
const parsed = deserialize( img );
expect( parsed.type ).toEqual( MediaTypes.IMAGE );
expect( parsed.media.ID ).toEqual( 1627 );
expect( parsed.media.URL ).toEqual(
'https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg'
);
expect( parsed.media.alt ).toEqual( 'Example' );
expect( parsed.media.transient ).toBe( false );
expect( parsed.media.width ).toEqual( 660 );
expect( parsed.media.height ).toEqual( 660 );
expect( parsed.appearance.size ).toEqual( 'full' );
expect( parsed.appearance.align ).toEqual( 'right' );
} );
test( 'should parse images with the data-istransient attribute as transient images', () => {
const parsed = deserialize(
'<img data-istransient="istransient" src="https://andrewmduthietest.files.wordpress.com/2015/01/img_0372.jpg" class="size-full wp-image-1627 alignright" alt="Example" width="660" height="660" />'
);
expect( parsed.media.transient ).toBe( true );
} );
test( 'should parse images without the data-istransient attribute as not transient images', () => {
const parsed = deserialize(
'<img src="blob:http%3A//wordpress.com/75205e1a-0f78-4a0b-b0e2-5f47a3471769" class="size-full wp-image-1627 alignright" alt="Example" width="660" height="660" />'
);
expect( parsed.media.transient ).toBe( false );
} );
test( 'should favor natural dimensions over inferred', () => {
const img = document.createElement( 'img' );
[ 'width', 'height' ].forEach( ( dimension ) => {
Object.defineProperty( img, dimension, {
get: () => 660,
} );
} );
[ 'naturalWidth', 'naturalHeight' ].forEach( ( dimension ) => {
Object.defineProperty( img, dimension, {
get: () => 1320,
} );
} );
const parsed = deserialize( img );
expect( parsed.type ).toEqual( MediaTypes.IMAGE );
expect( parsed.media.width ).toEqual( 1320 );
expect( parsed.media.height ).toEqual( 1320 );
} );
test( 'should favor attribute dimensions over natural', () => {
const img = document.createElement( 'img' );
img.width = 660;
img.height = 660;
[ 'naturalWidth', 'naturalHeight' ].forEach( ( dimension ) => {
Object.defineProperty( img, dimension, {
get: () => 1320,
} );
} );
img.setAttribute( 'width', '990' );
img.setAttribute( 'height', '990' );
const parsed = deserialize( img );
expect( parsed.type ).toEqual( MediaTypes.IMAGE );
expect( parsed.media.width ).toEqual( 990 );
expect( parsed.media.height ).toEqual( 990 );
} );
test( 'should parse a REST API media object', () => {
const media = {
ID: 3062,
URL: 'https://andrewmduthietest.files.wordpress.com/2015/08/sunset1.jpg',
guid: 'http://andrewmduthietest.files.wordpress.com/2015/08/sunset1.jpg',
date: '2015-08-18T11:32:58-04:00',
post_ID: 0,
file: 'sunset1.jpg',
mime_type: 'image/jpeg',
extension: 'jpg',
title: 'Sunset',
caption: 'Skyline',
description: '',
alt: '',
thumbnails: {},
height: 804,
width: 1150,
exif: {},
meta: {},
};
const parsed = deserialize( media );
expect( parsed.type ).toEqual( MediaTypes.IMAGE );
expect( parsed.media ).toEqual( Object.assign( { transient: false }, media ) );
expect( parsed.appearance ).toEqual( {} );
} );
test( 'should gracefully handle an unknown format', () => {
const parsed = deserialize();
expect( parsed.type ).toEqual( MediaTypes.UNKNOWN );
expect( parsed.media ).toEqual( {} );
expect( parsed.appearance ).toEqual( {} );
} );
} );
} );
|