2017-04-09 14:13:00 +00:00
import test from 'ava' ;
2017-04-09 19:36:03 +00:00
import sinon from 'sinon' ;
2017-04-09 14:13:00 +00:00
import Parser from '../../src/parser' ;
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.1165' ;
2018-06-30 11:26:39 +00:00
const parser = new Parser ( UA , true ) ;
2017-04-09 14:13:00 +00:00
2018-06-27 20:09:45 +00:00
test ( 'constructor' , ( t ) => {
2017-04-09 14:13:00 +00:00
t . truthy ( parser instanceof Parser ) ;
} ) ;
2018-06-27 20:09:45 +00:00
test ( 'Parser.getUA returns a correct UA' , ( t ) => {
2017-04-09 14:13:00 +00:00
t . is ( parser . getUA ( ) , UA ) ;
} ) ;
2018-06-27 20:09:45 +00:00
test ( 'Parser.test' , ( t ) => {
2017-04-09 14:13:00 +00:00
t . truthy ( parser . test ( /Chrome/i ) ) ;
} ) ;
2017-04-09 19:09:47 +00:00
2018-07-02 19:24:02 +00:00
test ( 'Parser.parseBrowser is being called when the Parser.getBrowser() is called' , ( t ) => {
2018-06-30 11:26:39 +00:00
const spy = sinon . spy ( parser , 'parseBrowser' ) ;
2017-04-09 19:36:03 +00:00
const b = parser . getBrowser ( ) ;
t . truthy ( spy . called ) ;
2017-04-09 19:09:47 +00:00
t . is ( b . name , 'Opera' ) ;
2017-04-09 19:30:19 +00:00
t . is ( b . version , '43.0.2442.1165' ) ;
2018-06-30 11:26:39 +00:00
parser . parseBrowser . restore ( ) ;
2017-04-09 19:46:23 +00:00
} ) ;
2018-06-27 20:09:45 +00:00
test ( 'Parser.getBrowserName returns a correct result' , ( t ) => {
2017-04-09 19:46:23 +00:00
t . is ( parser . getBrowserName ( ) , 'Opera' ) ;
} ) ;
2018-06-27 20:09:45 +00:00
test ( 'Parser.getBrowserVersion returns a correct result' , ( t ) => {
2017-04-09 19:46:23 +00:00
t . is ( parser . getBrowserVersion ( ) , '43.0.2442.1165' ) ;
2017-04-09 19:09:47 +00:00
} ) ;
2017-04-15 19:46:18 +00:00
2018-07-02 19:24:02 +00:00
test ( 'Parser.parseOS is being called when getOS() called' , ( t ) => {
2018-06-30 11:26:39 +00:00
const spy = sinon . spy ( parser , 'parseOS' ) ;
2017-04-15 19:46:18 +00:00
parser . getOS ( ) ;
t . truthy ( spy . called ) ;
2018-06-30 11:26:39 +00:00
parser . parseOS . restore ( ) ;
2017-04-15 19:46:18 +00:00
} ) ;
2018-06-27 20:09:45 +00:00
test ( 'Parser.getOSName gives a name of the browser' , ( t ) => {
2017-04-15 19:46:18 +00:00
t . is ( parser . getOSName ( ) , 'macOS' ) ;
} ) ;
2018-06-27 20:09:45 +00:00
test ( 'Parser.getOSName gives a lower-cased name of the browser' , ( t ) => {
2017-08-19 16:55:31 +00:00
t . is ( parser . getOSName ( true ) , 'macos' ) ;
} ) ;
2018-06-27 20:09:45 +00:00
test ( 'Parser.getOSVersion returns a correct result' , ( t ) => {
2017-04-15 19:46:18 +00:00
t . is ( parser . getOSVersion ( ) , '10.12.4' ) ;
} ) ;
2018-06-30 11:26:39 +00:00
test ( 'Skip parsing shouldn\'t parse' , ( t ) => {
t . deepEqual ( ( new Parser ( UA , true ) ) . getResult ( ) , { } ) ;
} ) ;
2018-07-02 19:24:02 +00:00
2018-09-09 12:02:29 +00:00
test ( 'Parser.satisfies should make simple comparisons' , ( t ) => {
2018-08-16 16:04:10 +00:00
// also covers Parser.compareVersion() method
2018-07-07 15:35:18 +00:00
t . is ( parser . satisfies ( { opera : '>42' } ) , true ) ;
2018-08-16 16:04:10 +00:00
t . is ( parser . satisfies ( { opera : '<44' } ) , true ) ;
t . is ( parser . satisfies ( { opera : '=43.0.2442.1165' } ) , true ) ;
t . is ( parser . satisfies ( { opera : '~43.0' } ) , true ) ;
t . is ( parser . satisfies ( { opera : '~43' } ) , true ) ;
2018-07-02 19:24:02 +00:00
} ) ;
2018-09-09 12:02:29 +00:00
test ( 'Parser.satisfies should make complex comparison' , ( t ) => {
2018-07-07 15:35:18 +00:00
t . is ( parser . satisfies ( {
2018-07-02 19:24:02 +00:00
macos : {
2018-07-02 20:30:48 +00:00
safari : '>11' ,
2018-07-02 19:24:02 +00:00
} ,
2018-07-02 20:30:48 +00:00
ios : {
safari : '>10' ,
} ,
opera : '>42' ,
2018-07-02 19:24:02 +00:00
} ) , true ) ;
} ) ;
2018-07-04 20:09:33 +00:00
2018-09-09 12:02:29 +00:00
test ( 'Parser.satisfies should respect platform and OS specific declarations' , ( t ) => {
2018-07-07 15:35:18 +00:00
t . is ( parser . satisfies ( {
macos : {
2018-07-08 09:08:49 +00:00
opera : '>45' ,
2018-07-07 15:35:18 +00:00
} ,
opera : '>42' ,
} ) , false ) ;
2018-07-08 09:08:49 +00:00
t . is ( parser . satisfies ( {
desktop : {
opera : '>45' ,
} ,
opera : '>42' ,
} ) , false ) ;
t . is ( parser . satisfies ( {
macos : {
opera : '>45' ,
} ,
desktop : {
opera : '>42' ,
} ,
opera : '>42' ,
} ) , false ) ;
t . is ( parser . satisfies ( {
macos : {
chrome : '>45' ,
} ,
desktop : {
chrome : '>42' ,
} ,
firefox : '>42' ,
} ) , void 0 ) ;
2018-07-07 15:35:18 +00:00
} ) ;
2018-09-09 12:02:29 +00:00
test ( 'Parser.satisfies for versionless UA strings' , ( t ) => {
const _parser = new Parser ( 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 [FBAN/FBIOS;FBDV/iPhone7,2;FBMD/iPhone;FBSN/iOS;FBSV/11.4.1;FBSS/2;FBCR/vfnl;FBID/phone;FBLC/nl_NL;FBOP/5;FBRV/0]' ) ;
t . is ( _parser . satisfies ( {
safari : '>9' ,
} ) , void 0 ) ;
} ) ;
2018-07-04 20:09:33 +00:00
test ( 'Parser.is should pass' , ( t ) => {
t . is ( parser . is ( 'opera' ) , true ) ;
t . is ( parser . is ( 'desktop' ) , true ) ;
t . is ( parser . is ( 'macos' ) , true ) ;
} ) ;
2018-07-17 20:43:54 +00:00
test ( 'Parser.some should pass' , ( t ) => {
t . is ( parser . some ( [ 'opera' , 'chrome' , 'firefox' ] ) , true ) ;
t . is ( parser . some ( [ 'macos' , 'windows' ] ) , true ) ;
t . is ( parser . some ( [ 'chrome' , 'firefox' ] ) , false ) ;
t . is ( parser . some ( [ ] ) , false ) ;
t . is ( parser . some ( ) , false ) ;
} ) ;