/tests/unit/support/support_core.js

https://github.com/gabrielschulhof/jquery-mobile · JavaScript · 120 lines · 86 code · 23 blank · 11 comment · 5 complexity · d98f252044f2ddf2ed80ad0d0734254a MD5 · raw file

  1. /*
  2. * Mobile support unit tests
  3. */
  4. define( [ "qunit", "jquery" ], function( QUnit, $ ) {
  5. $.testHelper.excludeFileProtocol( function() {
  6. var prependToFn = $.fn.prependTo,
  7. moduleName = "support";
  8. QUnit.module( moduleName, {
  9. teardown: function() {
  10. //NOTE undo any mocking
  11. $.fn.prependTo = prependToFn;
  12. }
  13. } );
  14. // NOTE following two tests have debatable value as they only
  15. // prevent property name changes and improper attribute checks
  16. QUnit.asyncTest( "detects functionality from basic affirmative properties and attributes", function( assert ) {
  17. // TODO expose properties for less brittle tests
  18. $.extend( window, {
  19. WebKitTransitionEvent: true
  20. } );
  21. window.history.pushState = function() {};
  22. window.history.replaceState = function() {};
  23. $.mobile.media = function() { return true; };
  24. $.testHelper.reloadModule( moduleName ).done( function() {
  25. assert.ok( $.support.cssTransitions, "css transitions are supported" );
  26. assert.ok( $.support.pushState, "push state is supported" );
  27. assert.ok( $.support.mediaquery, "media queries are supported" );
  28. QUnit.start();
  29. } );
  30. } );
  31. QUnit.asyncTest( "detects orientation change", function( assert ) {
  32. $.extend( window, {
  33. orientation: true,
  34. onorientationchange: true
  35. } );
  36. $.testHelper.reloadModule( "support/orientation" ).done( function() {
  37. assert.ok( $.support.orientation, "orientation is supported" );
  38. QUnit.start();
  39. } );
  40. } );
  41. QUnit.asyncTest( "detects touch", function( assert ) {
  42. document.ontouchend = true;
  43. $.testHelper.reloadModule( "support/touch" ).done( function() {
  44. assert.ok( $.mobile.support.touch, "touch is supported" );
  45. assert.ok( $.support.touch, "touch is supported" );
  46. QUnit.start();
  47. } );
  48. } );
  49. QUnit.asyncTest( "detects functionality from basic negative properties and attributes (where possible)", function( assert ) {
  50. delete window.orientation;
  51. $.testHelper.reloadModule( "support/orientation" ).done( function() {
  52. assert.ok( !$.support.orientation, "orientation is not supported" );
  53. QUnit.start();
  54. } );
  55. } );
  56. // NOTE mocks prependTo to simulate base href updates or lack thereof
  57. var mockBaseCheck = function( url ) {
  58. var prependToFn = $.fn.prependTo;
  59. $.fn.prependTo = function( selector ) {
  60. var result = prependToFn.call( this, selector );
  61. if ( this[ 0 ].href && this[ 0 ].href.indexOf( "testurl" ) !== -1 ) {
  62. result = [ { href: url } ];
  63. }
  64. return result;
  65. };
  66. };
  67. QUnit.asyncTest( "detects no dynamic base tag when new base element added and base href unchanged", function( assert ) {
  68. mockBaseCheck( "testurl" );
  69. $.testHelper.reloadModule( moduleName ).done( function() {
  70. assert.ok( !$.support.dynamicBaseTag );
  71. QUnit.start();
  72. } );
  73. } );
  74. QUnit.asyncTest( "jQM's IE browser check properly detects IE versions", function( assert ) {
  75. assert.expect( 1 );
  76. if ( !$.browser ) {
  77. assert.ok( true, "Cannot perform test because $.browser has been removed" );
  78. QUnit.start();
  79. return;
  80. }
  81. $.testHelper.reloadModule( moduleName ).done( function() {
  82. //Here we're just comparing our version to what the conditional compilation finds
  83. var ie = !!$.browser.msie, //Get a boolean
  84. version = parseInt( $.browser.version, 10 ),
  85. jqmdetectedver = $.mobile.browser.oldIE;
  86. if ( ie ) {
  87. assert.deepEqual( version, jqmdetectedver, "It's IE and the version is correct" );
  88. } else {
  89. assert.deepEqual( ie, jqmdetectedver, "It's not IE" );
  90. }
  91. QUnit.start();
  92. } );
  93. } );
  94. //TODO propExists testing, refactor propExists into mockable method
  95. //TODO scrollTop testing, refactor scrollTop logic into mockable method
  96. } );
  97. } );