diff --git a/docs/README.md b/docs/README.md index 06588bf..0b01d1c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1 +1,5 @@ # utils + +> 2018-03-31 + +- 文档:https://searchfe.github.io/utils/ \ No newline at end of file diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..b72d19d --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,87 @@ +// Karma configuration +// Generated on Wed Apr 04 2018 00:03:37 GMT+0800 (CST) + +module.exports = function(config) { + config.set({ + + // 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'], + + + // 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 + }) +} diff --git a/package.json b/package.json index 44fc038..dfc8968 100644 --- a/package.json +++ b/package.json @@ -15,5 +15,27 @@ "bugs": { "url": "https://github.com/searchfe/utils/issues" }, - "homepage": "https://github.com/searchfe/utils#readme" + "homepage": "https://github.com/searchfe/utils#readme", + "devDependencies": { + "chai": "^3.5.0", + "chai-as-promised": "^5.3.0", + "karma": "^2.0.0", + "karma-chai": "^0.1.0", + "karma-chai-as-promised": "^0.1.2", + "karma-chai-sinon": "^0.1.5", + "karma-chrome-launcher": "^2.2.0", + "karma-coverage": "^1.1.1", + "karma-coveralls": "^1.1.2", + "karma-firefox-launcher": "^1.1.0", + "karma-html-reporter": "^0.2.7", + "karma-mocha": "^1.1.1", + "karma-mocha-reporter": "^2.0.4", + "karma-opera-launcher": "^1.0.0", + "karma-requirejs": "^1.1.0", + "karma-safari-launcher": "^1.0.0", + "mocha": "^3.2.0", + "requirejs": "^2.3.5", + "sinon": "^2.1.0", + "sinon-chai": "^2.8.0" + } } diff --git a/src/index.js b/src/index.js index e69de29..10e16b2 100644 --- a/src/index.js +++ b/src/index.js @@ -0,0 +1,16 @@ +define(function() { + + function Browser () { + this._name = ''; + }; + + Browser.prototype._getBrowserName = function (ua) { + var ua = ua || navigator.userAgent; + if (ua.indexOf('QQBrowser') > -1) { + this._name = 'qq'; + } + } + + return Browser; + +}); \ No newline at end of file diff --git a/test-main.js b/test-main.js new file mode 100644 index 0000000..11547e8 --- /dev/null +++ b/test-main.js @@ -0,0 +1,33 @@ +/** + * @file testMain test entry for karma + * @author harttle + */ +var allTestFiles = []; +var TEST_REGEXP = /test\/.*\.js$/i; + +// Get a list of all the test files to include +Object.keys(window.__karma__.files).forEach(function (file) { + if (TEST_REGEXP.test(file)) { + // Normalize paths to RequireJS module names. + // If you require sub-dependencies of test files to be loaded as-is (requiring file extension) + // then do not normalize the paths + var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, ''); + allTestFiles.push(normalizedTestModule); + } +}); + +require.config({ + // Karma serves files under /base, which is the basePath from your config file + baseUrl: '/base', + + // dynamically load all test files + deps: allTestFiles, + + // external AMD modules + paths: { + '@searchfe/assert': 'amd_modules/@searchfe/assert' + }, + + // we have to kickoff jasmine, as it is asynchronous + callback: window.__karma__.start +}); diff --git a/test/index.js b/test/index.js index e69de29..5c51cac 100644 --- a/test/index.js +++ b/test/index.js @@ -0,0 +1,12 @@ +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'); + }); + }); + }); +});