租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.7 KiB

3 months ago
  1. /**
  2. * Loop through all entries in our user agents object and test everything.
  3. *
  4. * @see src/useragents.js
  5. * @author hannes.diercks@jimdo.com
  6. */
  7. var g
  8. , ua
  9. , p
  10. , assert = require('assert')
  11. , browser = require('../src/bowser')
  12. , allUserAgents = require('../src/useragents').useragents
  13. /**
  14. * Get the length of an object.
  15. * http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array
  16. *
  17. * @param {Object} obj
  18. * @return {Number}
  19. */
  20. function objLength(obj) {
  21. var size = 0
  22. , key
  23. for (key in obj) {
  24. if (obj.hasOwnProperty(key)) size++
  25. }
  26. return size
  27. }
  28. function objKeys(obj) {
  29. var keys = []
  30. , key
  31. for (key in obj) {
  32. if (obj.hasOwnProperty(key)) {
  33. keys.push(key)
  34. }
  35. }
  36. keys.sort();
  37. return keys.join(', ')
  38. }
  39. /* Groups */
  40. for (g in allUserAgents) { (function(group, userAgents) {
  41. describe(group, function() {
  42. /* User Agents */
  43. for (ua in userAgents) { (function(userAgent, expections) {
  44. describe('user agent "' + userAgent + '"', function() {
  45. expections.name = group
  46. /* Get the result from bowser. */
  47. var result = browser._detect(userAgent)
  48. /* At first, check if the result has the correct length. */
  49. it('should have ' + objLength(expections) + ' properties', function() {
  50. assert.equal(objKeys(result), objKeys(expections))
  51. })
  52. /* Properties */
  53. for (p in expections) { (function(property, value, resultValue) {
  54. /* Now ensure correctness of every property. */
  55. it('\'s Property "' + property + '" should be ' + value, function() {
  56. assert.equal(resultValue, value)
  57. })
  58. })(p, expections[p], result[p])}
  59. })
  60. })(ua, userAgents[ua])}
  61. })
  62. })(g, allUserAgents[g])}
  63. var comparisionsTasks = [
  64. ['9.0', '10', -1],
  65. ['11', '10', 1],
  66. ['1.10.2.1', '1.8.2.1.90', 1],
  67. ['1.010.2.1', '1.08.2.1.90', 1],
  68. ['1.10.2.1', '1.10.2.1', 0],
  69. ['1.10.2.1', '1.0800.2', -1],
  70. ['1.0.0-alpha', '1.0.0-alpha.1', -1],
  71. ['1.0.0-alpha.1', '1.0.0-alpha.beta', -1],
  72. ['1.0.0-alpha.beta', '1.0.0-beta', -1],
  73. ['1.0.0-beta', '1.0.0-beta.2', -1],
  74. ['1.0.0-beta.11', '1.0.0-rc.1', -1],
  75. ['1.0.0-rc.1', '1.0.0', -1]
  76. ];
  77. describe('Browser versions comparision', function() {
  78. for(g in comparisionsTasks) {
  79. var task = comparisionsTasks[g],
  80. version = task[0],
  81. version2 = task[1],
  82. matching = task[2] === 0 ? ' == ' : (task[2] > 0) ? ' > ' : ' < ';
  83. it('version ' + version + ' should be' + matching + 'version ' + version2, function(){
  84. assert.equal(browser.compareVersions([version, version2]), task[2]);
  85. });
  86. }
  87. });
  88. describe('Unsupported browser check', function() {
  89. before(function() {
  90. this.ie10_6 = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0";
  91. });
  92. it('should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified', function() {
  93. var unsupported = browser.isUnsupportedBrowser({msie: "10"}, this.ie10_6);
  94. assert.equal(unsupported, false);
  95. });
  96. it('should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified in strict mode', function() {
  97. var unsupported = browser.isUnsupportedBrowser({msie: "10"}, true, this.ie10_6);
  98. assert.equal(unsupported, false);
  99. });
  100. it('should NOT be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified in strict mode', function() {
  101. var isUnsupported = browser.isUnsupportedBrowser({msie: "11"}, true, this.ie10_6);
  102. assert.equal(isUnsupported, true);
  103. });
  104. it('should NOT be passed by #check for IE10.6 and for IE11 miminal version specified', function() {
  105. var supported = browser.check({msie: "11"}, this.ie10_6);
  106. assert.equal(supported, false);
  107. });
  108. it('should NOT be passed by #check for IE10.6 and for IE11 miminal version specified in strict mode', function() {
  109. var supported = browser.check({msie: "11"}, true, this.ie10_6);
  110. assert.equal(supported, false);
  111. });
  112. it('should throw an error when minVersion map has a number, but not a string', function() {
  113. assert.throws(() => {
  114. browser.check({msie: 11}, this.ie10_6);
  115. }, /Browser version in the minVersion map should be a string/);
  116. });
  117. it('should be passed by #check for IE10.6 when version was not specified', function() {
  118. var supported = browser.check({}, this.ie10_6);
  119. assert.equal(supported, true);
  120. });
  121. it('should NOT be passed by #check for IE10.6 when version was not specified in strict mode', function() {
  122. var supported = browser.check({}, true, this.ie10_6);
  123. assert.equal(supported, false);
  124. });
  125. })