test-media-video-widget.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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( 'Video Media Widget' );
  8. QUnit.test( 'video widget control', function( assert ) {
  9. var VideoWidgetControl, videoWidgetControlInstance, videoWidgetModelInstance, mappedProps, testVideoUrl;
  10. testVideoUrl = 'https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4';
  11. assert.equal( typeof wp.mediaWidgets.controlConstructors.media_video, 'function', 'wp.mediaWidgets.controlConstructors.media_video is a function' );
  12. VideoWidgetControl = wp.mediaWidgets.controlConstructors.media_video;
  13. assert.ok( VideoWidgetControl.prototype instanceof wp.mediaWidgets.MediaWidgetControl, 'wp.mediaWidgets.controlConstructors.media_video subclasses wp.mediaWidgets.MediaWidgetControl' );
  14. videoWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_video();
  15. videoWidgetControlInstance = new VideoWidgetControl({
  16. el: jQuery( '<div></div>' ),
  17. syncContainer: jQuery( '<div></div>' ),
  18. model: videoWidgetModelInstance
  19. });
  20. // Test mapModelToMediaFrameProps().
  21. videoWidgetControlInstance.model.set({ error: false, url: testVideoUrl, loop: false, preload: 'meta' });
  22. mappedProps = videoWidgetControlInstance.mapModelToMediaFrameProps( videoWidgetControlInstance.model.toJSON() );
  23. assert.equal( mappedProps.url, testVideoUrl, 'mapModelToMediaFrameProps should set url' );
  24. assert.equal( mappedProps.loop, false, 'mapModelToMediaFrameProps should set loop' );
  25. assert.equal( mappedProps.preload, 'meta', 'mapModelToMediaFrameProps should set preload' );
  26. // Test mapMediaToModelProps().
  27. mappedProps = videoWidgetControlInstance.mapMediaToModelProps( { loop: false, preload: 'meta', url: testVideoUrl, title: 'random movie file title' } );
  28. assert.equal( mappedProps.title, undefined, 'mapMediaToModelProps should ignore title inputs' );
  29. assert.equal( mappedProps.loop, false, 'mapMediaToModelProps should set loop' );
  30. assert.equal( mappedProps.preload, 'meta', 'mapMediaToModelProps should set preload' );
  31. });
  32. QUnit.test( 'video widget control renderPreview', function( assert ) {
  33. var videoWidgetControlInstance, videoWidgetModelInstance, done;
  34. done = assert.async();
  35. videoWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_video();
  36. videoWidgetControlInstance = new wp.mediaWidgets.controlConstructors.media_video({
  37. el: jQuery( '<div></div>' ),
  38. syncContainer: jQuery( '<div></div>' ),
  39. model: videoWidgetModelInstance
  40. });
  41. assert.equal( videoWidgetControlInstance.$el.find( 'a' ).length, 0, 'No video links should be rendered' );
  42. videoWidgetControlInstance.model.set({ error: false, url: 'https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4' });
  43. // Due to renderPreview being deferred.
  44. setTimeout( function() {
  45. assert.equal( videoWidgetControlInstance.$el.find( 'a[href="https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4"]' ).length, 1, 'One video link should be rendered' );
  46. done();
  47. }, 50 );
  48. done();
  49. });
  50. QUnit.test( 'video media model', function( assert ) {
  51. var VideoWidgetModel, videoWidgetModelInstance;
  52. assert.equal( typeof wp.mediaWidgets.modelConstructors.media_video, 'function', 'wp.mediaWidgets.modelConstructors.media_video is a function' );
  53. VideoWidgetModel = wp.mediaWidgets.modelConstructors.media_video;
  54. assert.ok( VideoWidgetModel.prototype instanceof wp.mediaWidgets.MediaWidgetModel, 'wp.mediaWidgets.modelConstructors.media_video subclasses wp.mediaWidgets.MediaWidgetModel' );
  55. videoWidgetModelInstance = new VideoWidgetModel();
  56. _.each( videoWidgetModelInstance.attributes, function( value, key ) {
  57. assert.equal( value, VideoWidgetModel.prototype.schema[ key ][ 'default' ], 'Should properly set default for ' + key );
  58. });
  59. });
  60. })();