Quelltext der Datei: http://www.michaelster.ch/jsTestFolder/jQuery/bowser-master/test/test.js

Dateigrösse: 4.27 kb

[Anzeige ohne Zeilennummern]


  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 
  8 var g
  9   , ua
 10   , p
 11   , assert = require('assert')
 12   , browser = require('../src/bowser')
 13   , allUserAgents = require('../src/useragents').useragents
 14 
 15 /**
 16  * Get the length of an object.
 17  * http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array
 18  *
 19  * @param  {Object} obj
 20  * @return {Number}
 21  */
 22 function objLength(obj) {
 23   var size = 0
 24     , key
 25   for (key in obj) {
 26     if (obj.hasOwnProperty(key)) size++
 27   }
 28   return size
 29 }
 30 
 31 function objKeys(obj) {
 32   var keys = []
 33     , key
 34   for (key in obj) {
 35     if (obj.hasOwnProperty(key)) {
 36       keys.push(key)
 37     }
 38   }
 39   keys.sort();
 40   return keys.join(', ')
 41 }
 42 
 43 /* Groups */
 44 for (g in allUserAgents) { (function(group, userAgents) {
 45   describe(group, function() {
 46 
 47     /* User Agents */
 48     for (ua in userAgents) { (function(userAgent, expections) {
 49       describe('user agent "' + userAgent + '"', function() {
 50 
 51         expections.name = group
 52 
 53         /* Get the result from bowser. */
 54         var result = browser._detect(userAgent)
 55 
 56         /* At first, check if the result has the correct length. */
 57         it('should have ' + objLength(expections) + ' properties', function() {
 58           assert.equal(objKeys(result), objKeys(expections))
 59         })
 60 
 61         /* Properties */
 62         for (p in expections) { (function(property, value, resultValue) {
 63 
 64           /* Now ensure correctness of every property. */
 65           it('\'s Property "' + property + '" should be ' + value, function() {
 66             assert.equal(resultValue, value)
 67           })
 68 
 69         })(p, expections[p], result[p])}
 70 
 71       })
 72     })(ua, userAgents[ua])}
 73 
 74   })
 75 })(g, allUserAgents[g])}
 76 
 77 var comparisionsTasks = [
 78   ['9.0', '10', -1],
 79   ['11', '10', 1],
 80   ['1.10.2.1',  '1.8.2.1.90', 1],
 81   ['1.010.2.1', '1.08.2.1.90', 1],
 82   ['1.10.2.1', '1.10.2.1', 0],
 83   ['1.10.2.1', '1.0800.2', -1],
 84   ['1.0.0-alpha', '1.0.0-alpha.1', -1],
 85   ['1.0.0-alpha.1', '1.0.0-alpha.beta', -1],
 86   ['1.0.0-alpha.beta', '1.0.0-beta', -1],
 87   ['1.0.0-beta', '1.0.0-beta.2', -1],
 88   ['1.0.0-beta.11', '1.0.0-rc.1', -1],
 89   ['1.0.0-rc.1', '1.0.0', -1]
 90 ];
 91 
 92 describe('Browser versions comparision', function() {
 93   for(g in comparisionsTasks) {
 94     var task = comparisionsTasks[g],
 95         version = task[0],
 96         version2 = task[1],
 97         matching = task[2] === 0 ? ' == ' : (task[2] > 0) ? ' > ' : ' < ';
 98     it('version ' + version + ' should be' + matching + 'version ' + version2, function(){
 99       assert.equal(browser.compareVersions([version, version2]), task[2]);
100     });
101   }
102 });
103 
104 describe('Unsupported browser check', function() {
105 
106   before(function() {
107     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";
108   });
109 
110   it('should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified', function() {
111     var unsupported = browser.isUnsupportedBrowser({msie: "10"}, this.ie10_6);
112     assert.equal(unsupported, false);
113   });
114 
115   it('should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified in strict mode', function() {
116     var unsupported = browser.isUnsupportedBrowser({msie: "10"}, true, this.ie10_6);
117     assert.equal(unsupported, false);
118   });
119 
120   it('should NOT be passed by #check for IE10.6 and for IE11 miminal version specified', function() {
121     var supported = browser.check({msie: "11"}, this.ie10_6);
122     assert.equal(supported, false);
123   });
124 
125   it('should NOT be passed by #check for IE10.6 and for IE11 miminal version specified in strict mode', function() {
126     var supported = browser.check({msie: "11"}, true, this.ie10_6);
127     assert.equal(supported, false);
128   });
129 
130   it('should be passed by #check for IE10.6 when version was not specified', function() {
131     var supported = browser.check({}, this.ie10_6);
132     assert.equal(supported, true);
133   });
134 
135   it('should NOT be passed by #check for IE10.6 when version was not specified in strict mode', function() {
136     var supported = browser.check({}, true, this.ie10_6);
137     assert.equal(supported, false);
138   });
139 
140 })
141