@ -1,5 +1,3 @@
import { strict as assert } from 'assert' ;
import sinon from 'sinon' ;
import { isPrefixedFormattedHexString } from '../../../shared/modules/network.utils' ;
import {
ENVIRONMENT _TYPE _POPUP ,
@ -13,201 +11,154 @@ import {
} from '../../../shared/constants/app' ;
import { getEnvironmentType , getPlatform } from './util' ;
describe ( 'app utils' , function ( ) {
describe ( 'getEnvironmentType' , function ( ) {
it ( 'should return popup type' , function ( ) {
describe ( 'app utils' , ( ) => {
describe ( 'getEnvironmentType' , ( ) => {
it ( 'should return popup type' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/popup.html' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _POPUP ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _POPUP ) ;
} ) ;
it ( 'should return notification type' , function ( ) {
it ( 'should return notification type' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/notification.html' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _NOTIFICATION ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _NOTIFICATION ) ;
} ) ;
it ( 'should return fullscreen type for home.html' , function ( ) {
it ( 'should return fullscreen type for home.html' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/home.html' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _FULLSCREEN ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _FULLSCREEN ) ;
} ) ;
it ( 'should return fullscreen type for phishing.html' , function ( ) {
it ( 'should return fullscreen type for phishing.html' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/phishing.html' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _FULLSCREEN ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _FULLSCREEN ) ;
} ) ;
it ( 'should return background type' , function ( ) {
it ( 'should return background type' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/_generated_background_page.html' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _BACKGROUND ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _BACKGROUND ) ;
} ) ;
it ( 'should return the correct type for a URL with a hash fragment' , function ( ) {
it ( 'should return the correct type for a URL with a hash fragment' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/popup.html#hash' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _POPUP ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _POPUP ) ;
} ) ;
it ( 'should return the correct type for a URL with query parameters' , function ( ) {
it ( 'should return the correct type for a URL with query parameters' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/popup.html?param=foo' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _POPUP ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _POPUP ) ;
} ) ;
it ( 'should return the correct type for a URL with query parameters and a hash fragment' , function ( ) {
it ( 'should return the correct type for a URL with query parameters and a hash fragment' , ( ) => {
const environmentType = getEnvironmentType (
'http://extension-id/popup.html?param=foo#hash' ,
) ;
assert . equal ( environmentType , ENVIRONMENT _TYPE _POPUP ) ;
expect ( environmentType ) . toStrictEqual ( ENVIRONMENT _TYPE _POPUP ) ;
} ) ;
} ) ;
describe ( 'isPrefixedFormattedHexString' , function ( ) {
it ( 'should return true for valid hex strings' , function ( ) {
assert . equal (
isPrefixedFormattedHexString ( '0x1' ) ,
true ,
'should return true' ,
) ;
describe ( 'isPrefixedFormattedHexString' , ( ) => {
it ( 'should return true for valid hex strings' , ( ) => {
expect ( isPrefixedFormattedHexString ( '0x1' ) ) . toStrictEqual ( true ) ;
assert . equal (
isPrefixedFormattedHexString ( '0xa' ) ,
true ,
'should return true' ,
) ;
expect ( isPrefixedFormattedHexString ( '0xa' ) ) . toStrictEqual ( true ) ;
assert . equal (
expect (
isPrefixedFormattedHexString ( '0xabcd1123fae909aad87452' ) ,
true ,
'should return true' ,
) ;
) . toStrictEqual ( true ) ;
} ) ;
it ( 'should return false for invalid hex strings' , function ( ) {
assert . equal (
isPrefixedFormattedHexString ( '0x' ) ,
false ,
'should return false' ,
) ;
it ( 'should return false for invalid hex strings' , ( ) => {
expect ( isPrefixedFormattedHexString ( '0x' ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( '0x0' ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( '0x0' ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( '0x01' ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( '0x01' ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( ' 0x1' ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( ' 0x1' ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( '0x1 ' ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( '0x1 ' ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( '0x1afz' ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( '0x1afz' ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( 'z' ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( 'z' ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( 2 ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( 2 ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( [ '0x1' ] ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( [ '0x1' ] ) ) . toStrictEqual ( false ) ;
assert . equal (
isPrefixedFormattedHexString ( ) ,
false ,
'should return false' ,
) ;
expect ( isPrefixedFormattedHexString ( ) ) . toStrictEqual ( false ) ;
} ) ;
} ) ;
describe ( 'getPlatform' , function ( ) {
const setBrowserSpecificWindow = ( browser ) => {
switch ( browser ) {
case 'firefox' : {
sinon . stub ( window , 'navigator' ) . value ( {
userAgent :
describe ( 'getPlatform' , ( ) => {
let userAgent , setBrowserSpecificWindow ;
beforeEach ( ( ) => {
userAgent = jest . spyOn ( window . navigator , 'userAgent' , 'get' ) ;
setBrowserSpecificWindow = ( browser ) => {
switch ( browser ) {
case 'firefox' : {
userAgent . mockReturnValue (
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0' ,
} ) ;
break ;
}
case 'edge' : {
sinon . stub ( window , 'navigator' ) . value ( {
userAgent :
) ;
break ;
}
case 'edge' : {
userAgent . mockReturnValue (
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30' ,
} ) ;
break ;
}
case 'opera' : {
sinon . stub ( window , 'navigator' ) . value ( {
userAgent :
) ;
break ;
}
case 'opera' : {
userAgent . mockReturnValue (
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 OPR/80.0.4170.63' ,
} ) ;
break ;
}
default : {
sinon . stub ( window , 'navigator' ) . value ( {
userAgent :
) ;
break ;
}
default : {
userAgent . mockReturnValue (
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36' ,
} ) ;
break ;
) ;
break ;
}
}
}
} ;
} ;
} ) ;
it ( 'should detect Firefox' , function ( ) {
it ( 'should detect Firefox' , ( ) => {
setBrowserSpecificWindow ( 'firefox' ) ;
assert . equal ( getPlatform ( ) , PLATFORM _FIREFOX ) ;
expect ( getPlatform ( ) ) . toStrictEqual ( PLATFORM _FIREFOX ) ;
} ) ;
it ( 'should detect Edge' , function ( ) {
it ( 'should detect Edge' , ( ) => {
setBrowserSpecificWindow ( 'edge' ) ;
assert . equal ( getPlatform ( ) , PLATFORM _EDGE ) ;
expect ( getPlatform ( ) ) . toStrictEqual ( PLATFORM _EDGE ) ;
} ) ;
it ( 'should detect Opera' , function ( ) {
it ( 'should detect Opera' , ( ) => {
setBrowserSpecificWindow ( 'opera' ) ;
assert . equal ( getPlatform ( ) , PLATFORM _OPERA ) ;
expect ( getPlatform ( ) ) . toStrictEqual ( PLATFORM _OPERA ) ;
} ) ;
it ( 'should detect Chrome' , function ( ) {
it ( 'should detect Chrome' , ( ) => {
setBrowserSpecificWindow ( 'chrome' ) ;
assert . equal ( getPlatform ( ) , PLATFORM _CHROME ) ;
expect ( getPlatform ( ) ) . toStrictEqual ( PLATFORM _CHROME ) ;
} ) ;
} ) ;
} ) ;