Skip to content

Commit

Permalink
add: travis, lint, baidu/chrome/qq/uc
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Apr 24, 2018
1 parent 254124e commit 850730c
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 99 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"env": {
"amd": true
},
"rules": {
"no-extend-native": "off",
"indent": ["error", 4],
"semi": ["error", "always"]
},
"extends": "standard"
}
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: node_js
sudo: required
dist: trusty
node_js:
- lts/*
env:
- BROWSER=ChromeHeadless
addons:
chrome: stable
cache:
directories:
- "node_modules"
script:
- apm install
- npm run lint
- npm run ci
before_install:
- google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
146 changes: 67 additions & 79 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,75 @@
// Karma configuration
// Generated on Wed Apr 04 2018 00:03:37 GMT+0800 (CST)

module.exports = function(config) {
config.set({
module.exports = function (config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'requirejs', 'chai-as-promised', 'chai-sinon'],

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'requirejs', 'chai-as-promised', 'chai-sinon'],


// list of files / patterns to load in the browser
files: [
'test-main.js',
// list of files / patterns to load in the browser
files: [
'test-main.js',
{pattern: 'src/**/*.js', included: false},
{pattern: 'amd_modules/**/*.js', included: false},
{pattern: 'test/**/*.js', included: false}
],


// list of files to exclude
exclude: [
'**/*.swp'
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'src/**/*.js': ['coverage']
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha'],
coverageReporter: {
reporters: [
{
type: 'text-summary'
}, {
type: 'lcov',
dir: 'coverage/'
}
]
},


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
],

// list of files to exclude
exclude: [
'**/*.swp'
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'src/**/*.js': ['coverage']
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha'],
coverageReporter: {
reporters: [
{
type: 'text-summary'
}, {
type: 'lcov',
dir: 'coverage/'
}
]
},

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
});
};
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"description": "A javascript tools lib for the frontend",
"main": "src/index.js",
"scripts": {
"lint": "eslint src test *.js",
"preversion": "npm run lint && npm test",
"postversion": "git push && git push --tags",
"ci": "karma start --reporters mocha,coverage,coveralls",
"test": "karma start"
},
"repository": {
Expand All @@ -19,6 +23,13 @@
"devDependencies": {
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"eslint": "^3.19.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-mocha": "^4.9.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"karma": "^2.0.0",
"karma-chai": "^0.1.0",
"karma-chai-as-promised": "^0.1.2",
Expand Down
42 changes: 31 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
define(function() {
define(function () {
var traits = {
// OS
isAndroid: /android/i,
isIOS: /(iPhone|iPod|iPad)/,
isWinPhone: /Windows Phone ([\d.]+)/,

function Browser () {
this._name = '';
// Browser
isBaiduBox: /baiduboxapp/,
isBaiduBoxLite: /lite baiduboxapp/,
isQQ: /QQBrowser/,
isBaiduBrowser: /baidubrowser/,
isSearchCraft: /SearchCraft/i,
isUC: /UCBrowser/,
isChromeDesktop: /Chrome\//,
isChromeMobile: /Chrome\/(\S*) Mobile/,
isSogouMobile: /SogouMobileBrowser/,
isMiuiBrowser: /MiuiBrowser\/(\S*)/
};

Browser.prototype._getBrowserName = function (ua) {
var ua = ua || navigator.userAgent;
if (ua.indexOf('QQBrowser') > -1) {
this._name = 'qq';
}
}
function factory (userAgentString) {
var ua = {
use: factory
};

return Browser;
Object.keys(traits).forEach(function (key) {
var regexp = traits[key];
ua[key] = function () {
return regexp.test(userAgentString);
};
});
return ua;
};

});
return factory(navigator.userAgent);
});
17 changes: 17 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"rules": {
"no-unused-expressions": "off",
"no-new": "off",
"standard/no-callback-literal": "off"
},
"globals": {
"expect": true,
"sinon": true
},
"env": {
"mocha": true
},
"plugins": [
"mocha"
]
}
26 changes: 17 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
define(['src/index'], function (Browser) {
describe('Browser', function () {
describe('new', function () {
it('should get qq browser name', function () {
var ua = 'Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HUAWEI C8818 Build/HuaweiC8818) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.8 Mobile Safari/537.36';
var browser = new Browser();
browser._getBrowserName(ua);
expect(browser._name).to.equal('qq');
});
define(['src/index'], function (UA) {
var qqAndroid = 'Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HUAWEI C8818 Build/HuaweiC8818) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.8 Mobile Safari/537.36';
var qqIOS = 'Mozilla/5.0 (iPhone 84; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 MQQBrowser/7.8.0 Mobile/14G60 Safari/8536.25 MttCustomUA/2 QBWebViewType/1 WKType/1';
var ucAndoird = 'Mozilla/5.0 (Linux; U; Android 7.0; zh-CN; ZUK Z2121 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.6.8.952 Mobile Safari/537.36';

describe('UA', function () {
it('should detect chrome', function () {
expect(UA.isChromeDesktop()).to.equal(true);
expect(UA.isQQ()).to.equal(false);
expect(UA.isUC()).to.equal(false);
});
it('should detect QQ browser', function () {
expect(UA.use(qqAndroid).isQQ()).to.equal(true);
expect(UA.use(qqIOS).isQQ()).to.equal(true);
});
it('should detect UC browser', function () {
expect(UA.use(ucAndoird).isUC()).to.equal(true);
});
});
});

0 comments on commit 850730c

Please sign in to comment.