test-media-image-widget.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* globals wp */
  2. /* jshint qunit: true */
  3. /* eslint-env qunit */
  4. /* eslint-disable no-magic-numbers */
  5. ( function() {
  6. 'use strict';
  7. QUnit.module( 'Image Media Widget' );
  8. QUnit.test( 'image widget control', function( assert ) {
  9. var ImageWidgetControl, imageWidgetControlInstance, imageWidgetModelInstance, mappedProps, testImageUrl, templateProps;
  10. testImageUrl = 'http://s.w.org/style/images/wp-header-logo.png';
  11. assert.equal( typeof wp.mediaWidgets.controlConstructors.media_image, 'function', 'wp.mediaWidgets.controlConstructors.media_image is a function' );
  12. ImageWidgetControl = wp.mediaWidgets.controlConstructors.media_image;
  13. assert.ok( ImageWidgetControl.prototype instanceof wp.mediaWidgets.MediaWidgetControl, 'wp.mediaWidgets.controlConstructors.media_image subclasses wp.mediaWidgets.MediaWidgetControl' );
  14. imageWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_image();
  15. imageWidgetControlInstance = new ImageWidgetControl({
  16. el: jQuery( '<div></div>' ),
  17. syncContainer: jQuery( '<div></div>' ),
  18. model: imageWidgetModelInstance
  19. });
  20. // Test mapModelToPreviewTemplateProps() when no data is set.
  21. templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
  22. assert.equal( templateProps.caption, undefined, 'mapModelToPreviewTemplateProps should not return attributes that are should_preview_update false' );
  23. assert.equal( templateProps.attachment_id, 0, 'mapModelToPreviewTemplateProps should return default values' );
  24. assert.equal( templateProps.currentFilename, '', 'mapModelToPreviewTemplateProps should return a currentFilename' );
  25. // Test mapModelToPreviewTemplateProps() when data is set on model.
  26. imageWidgetControlInstance.model.set( { url: testImageUrl, alt: 'some alt text', link_type: 'none' } );
  27. templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
  28. assert.equal( templateProps.currentFilename, 'wp-header-logo.png', 'mapModelToPreviewTemplateProps should set currentFilename based off of url' );
  29. assert.equal( templateProps.url, testImageUrl, 'mapModelToPreviewTemplateProps should return the proper url' );
  30. assert.equal( templateProps.alt, 'some alt text', 'mapModelToPreviewTemplateProps should return the proper alt text' );
  31. assert.equal( templateProps.link_type, undefined, 'mapModelToPreviewTemplateProps should ignore attributes that are not needed in the preview' );
  32. assert.equal( templateProps.error, false, 'mapModelToPreviewTemplateProps should return error state' );
  33. // Test mapModelToPreviewTemplateProps() when error is set on model.
  34. imageWidgetControlInstance.model.set( 'error', 'missing_attachment' );
  35. templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
  36. assert.equal( templateProps.error, 'missing_attachment', 'mapModelToPreviewTemplateProps should return error string' );
  37. // Reset model.
  38. imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: null });
  39. // Test isSelected().
  40. assert.equal( imageWidgetControlInstance.isSelected(), false, 'media_image.isSelected() should return false when no media is selected' );
  41. imageWidgetControlInstance.model.set({ error: 'missing_attachment', attachment_id: 777 });
  42. assert.equal( imageWidgetControlInstance.isSelected(), false, 'media_image.isSelected() should return false when media is selected and error is set' );
  43. imageWidgetControlInstance.model.set({ error: false, attachment_id: 777 });
  44. assert.equal( imageWidgetControlInstance.isSelected(), true, 'media_image.isSelected() should return true when media is selected and no error exists' );
  45. imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: testImageUrl });
  46. assert.equal( imageWidgetControlInstance.isSelected(), true, 'media_image.isSelected() should return true when url is set and no error exists' );
  47. // Reset model.
  48. imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: null });
  49. // Test editing of widget title.
  50. imageWidgetControlInstance.render();
  51. imageWidgetControlInstance.$el.find( '.title' ).val( 'Chicken and Ribs' ).trigger( 'input' );
  52. assert.equal( imageWidgetModelInstance.get( 'title' ), 'Chicken and Ribs', 'Changing title should update model title attribute' );
  53. // Test mapMediaToModelProps.
  54. mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'file', url: testImageUrl } );
  55. assert.equal( mappedProps.link_url, testImageUrl, 'mapMediaToModelProps should set file link_url according to mediaFrameProps.link' );
  56. mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'post', postUrl: 'https://wordpress.org/image-2/' } );
  57. assert.equal( mappedProps.link_url, 'https://wordpress.org/image-2/', 'mapMediaToModelProps should set file link_url according to mediaFrameProps.link' );
  58. mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'custom', linkUrl: 'https://wordpress.org' } );
  59. assert.equal( mappedProps.link_url, 'https://wordpress.org', 'mapMediaToModelProps should set custom link_url according to mediaFrameProps.linkUrl' );
  60. // Test mapModelToMediaFrameProps().
  61. imageWidgetControlInstance.model.set({ error: false, url: testImageUrl, 'link_type': 'custom', 'link_url': 'https://wordpress.org', 'size': 'custom', 'width': 100, 'height': 150, 'title': 'widget title', 'image_title': 'title of image' });
  62. mappedProps = imageWidgetControlInstance.mapModelToMediaFrameProps( imageWidgetControlInstance.model.toJSON() );
  63. assert.equal( mappedProps.linkUrl, 'https://wordpress.org', 'mapModelToMediaFrameProps should set linkUrl from model.link_url' );
  64. assert.equal( mappedProps.link, 'custom', 'mapModelToMediaFrameProps should set link from model.link_type' );
  65. assert.equal( mappedProps.width, 100, 'mapModelToMediaFrameProps should set width when model.size is custom' );
  66. assert.equal( mappedProps.height, 150, 'mapModelToMediaFrameProps should set height when model.size is custom' );
  67. assert.equal( mappedProps.title, 'title of image', 'mapModelToMediaFrameProps should set title from model.image_title' );
  68. });
  69. QUnit.test( 'image widget control renderPreview', function( assert ) {
  70. var imageWidgetControlInstance, imageWidgetModelInstance, done;
  71. done = assert.async();
  72. imageWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_image();
  73. imageWidgetControlInstance = new wp.mediaWidgets.controlConstructors.media_image({
  74. el: jQuery( '<div></div>' ),
  75. syncContainer: jQuery( '<div></div>' ),
  76. model: imageWidgetModelInstance
  77. });
  78. assert.equal( imageWidgetControlInstance.$el.find( 'img' ).length, 0, 'No images should be rendered' );
  79. imageWidgetControlInstance.model.set({ error: false, url: 'http://s.w.org/style/images/wp-header-logo.png' });
  80. // Due to renderPreview being deferred.
  81. setTimeout( function() {
  82. assert.equal( imageWidgetControlInstance.$el.find( 'img[src="http://s.w.org/style/images/wp-header-logo.png"]' ).length, 1, 'One image should be rendered' );
  83. done();
  84. }, 50 );
  85. done();
  86. });
  87. QUnit.test( 'image media model', function( assert ) {
  88. var ImageWidgetModel, imageWidgetModelInstance;
  89. assert.equal( typeof wp.mediaWidgets.modelConstructors.media_image, 'function', 'wp.mediaWidgets.modelConstructors.media_image is a function' );
  90. ImageWidgetModel = wp.mediaWidgets.modelConstructors.media_image;
  91. assert.ok( ImageWidgetModel.prototype instanceof wp.mediaWidgets.MediaWidgetModel, 'wp.mediaWidgets.modelConstructors.media_image subclasses wp.mediaWidgets.MediaWidgetModel' );
  92. imageWidgetModelInstance = new ImageWidgetModel();
  93. _.each( imageWidgetModelInstance.attributes, function( value, key ) {
  94. assert.equal( value, ImageWidgetModel.prototype.schema[ key ][ 'default' ], 'Should properly set default for ' + key );
  95. });
  96. });
  97. })();