/tests/unit/core/core.js

https://github.com/adrianpike/jquery-mobile · JavaScript · 148 lines · 95 code · 41 blank · 12 comment · 1 complexity · 10dc1739af5d27c40b831b16957a39f2 MD5 · raw file

  1. /*
  2. * mobile core unit tests
  3. */
  4. (function($){
  5. var libName = "jquery.mobile.core.js",
  6. setGradeA = function(value, version) {
  7. $.support.mediaquery = value;
  8. $.mobile.browser.ie = version;
  9. },
  10. extendFn = $.extend;
  11. module(libName, {
  12. setup: function(){
  13. // NOTE reset for gradeA tests
  14. $('html').removeClass('ui-mobile');
  15. // NOTE reset for pageLoading tests
  16. $('.ui-loader').remove();
  17. },
  18. teardown: function(){
  19. $.extend = extendFn;
  20. }
  21. });
  22. $.testHelper.excludeFileProtocol(function(){
  23. test( "grade A browser either supports media queries or is IE 7+", function(){
  24. setGradeA(false, 6);
  25. $.testHelper.reloadLib(libName);
  26. ok(!$.mobile.gradeA());
  27. setGradeA(true, 8);
  28. $.testHelper.reloadLib(libName);
  29. ok($.mobile.gradeA());
  30. });
  31. });
  32. function clearNSNormalizeDictionary()
  33. {
  34. var dict = $.mobile.nsNormalizeDict;
  35. for ( var prop in dict ) {
  36. delete dict[ prop ];
  37. }
  38. }
  39. test( "$.mobile.nsNormalize works properly with namespace defined (test default)", function(){
  40. // Start with a fresh namespace property cache, just in case
  41. // the previous test mucked with namespaces.
  42. clearNSNormalizeDictionary();
  43. equal($.mobile.nsNormalize("foo"), "nstestFoo", "appends ns and initcaps");
  44. equal($.mobile.nsNormalize("fooBar"), "nstestFooBar", "leaves capped strings intact");
  45. equal($.mobile.nsNormalize("foo-bar"), "nstestFooBar", "changes dashed strings");
  46. equal($.mobile.nsNormalize("foo-bar-bak"), "nstestFooBarBak", "changes multiple dashed strings");
  47. // Reset the namespace property cache for the next test.
  48. clearNSNormalizeDictionary();
  49. });
  50. test( "$.mobile.nsNormalize works properly with an empty namespace", function(){
  51. var realNs = $.mobile.ns;
  52. $.mobile.ns = "";
  53. // Start with a fresh namespace property cache, just in case
  54. // the previous test mucked with namespaces.
  55. clearNSNormalizeDictionary();
  56. equal($.mobile.nsNormalize("foo"), "foo", "leaves uncapped and undashed");
  57. equal($.mobile.nsNormalize("fooBar"), "fooBar", "leaves capped strings intact");
  58. equal($.mobile.nsNormalize("foo-bar"), "fooBar", "changes dashed strings");
  59. equal($.mobile.nsNormalize("foo-bar-bak"), "fooBarBak", "changes multiple dashed strings");
  60. $.mobile.ns = realNs;
  61. // Reset the namespace property cache for the next test.
  62. clearNSNormalizeDictionary();
  63. });
  64. //data tests
  65. test( "$.fn.jqmData and $.fn.jqmRemoveData methods are working properly", function(){
  66. var data;
  67. same( $("body").jqmData("foo", true), $("body"), "setting data returns the element" );
  68. same( $("body").jqmData("foo"), true, "getting data returns the right value" );
  69. same( $("body").data($.mobile.nsNormalize("foo")), true, "data was set using namespace" );
  70. same( $("body").jqmData("foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
  71. data = $.extend( {}, $("body").data() );
  72. delete data[ $.expando ]; //discard the expando for that test
  73. same( data , { "nstestFoo": true }, "passing .data() no arguments returns a hash with all set properties" );
  74. same( $("body").jqmData(), undefined, "passing no arguments returns undefined" );
  75. same( $("body").jqmData(undefined), undefined, "passing a single undefined argument returns undefined" );
  76. same( $("body").jqmData(undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
  77. same( $("body").jqmRemoveData("foo"), $("body"), "jqmRemoveData returns the element" );
  78. same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
  79. });
  80. test( "$.jqmData and $.jqmRemoveData methods are working properly", function(){
  81. same( $.jqmData(document.body, "foo", true), true, "setting data returns the value" );
  82. same( $.jqmData(document.body, "foo"), true, "getting data returns the right value" );
  83. same( $.data(document.body, $.mobile.nsNormalize("foo")), true, "data was set using namespace" );
  84. same( $.jqmData(document.body, "foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
  85. same( $.jqmData(document.body), undefined, "passing no arguments returns undefined" );
  86. same( $.jqmData(document.body, undefined), undefined, "passing a single undefined argument returns undefined" );
  87. same( $.jqmData(document.body, undefined, undefined), undefined, "passing 2 undefined arguments returns undefined" );
  88. same( $.jqmRemoveData(document.body, "foo"), undefined, "jqmRemoveData returns the undefined value" );
  89. same( $("body").jqmData("foo"), undefined, "jqmRemoveData properly removes namespaced data" );
  90. });
  91. test( "addDependents works properly", function() {
  92. same( $("#parent").jqmData('dependents'), undefined );
  93. $( "#parent" ).addDependents( $("#dependent") );
  94. same( $("#parent").jqmData('dependents').length, 1 );
  95. });
  96. test( "removeWithDependents removes the parent element and ", function(){
  97. $( "#parent" ).addDependents( $("#dependent") );
  98. same($( "#parent, #dependent" ).length, 2);
  99. $( "#parent" ).removeWithDependents();
  100. same($( "#parent, #dependent" ).length, 0);
  101. });
  102. test( "$.fn.getEncodedText should return the encoded value where $.fn.text doesn't", function() {
  103. same( $("#encoded").text(), "foo>");
  104. same( $("#encoded").getEncodedText(), "foo>");
  105. same( $("#unencoded").getEncodedText(), "foo");
  106. });
  107. })(jQuery);