Ver Fonte

Upgrade karma and test code

Emanuele há 8 anos atrás
pai
commit
bea6c5dce9
6 ficheiros alterados com 113 adições e 70 exclusões
  1. 7 1
      .babelrc
  2. 57 0
      karma.conf.js
  3. 0 17
      karma.js
  4. 29 20
      package.json
  5. 2 2
      src/js/bootstrap-switch.js
  6. 18 30
      src/js/bootstrap-switch.test.js

+ 7 - 1
.babelrc

@@ -1,5 +1,11 @@
 {
-  "presets": ["env"],
+  "presets": [
+    ["env", {
+      "targets": {
+        "browsers": ["last 2 versions"]
+      }
+    }]
+  ],
   "env": {
     "production": {
       "presets": ["babili"]

+ 57 - 0
karma.conf.js

@@ -0,0 +1,57 @@
+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: ['jasmine'],
+
+    // list of files / patterns to load in the browser
+    files: [
+      'node_modules/babel-polyfill/dist/polyfill.js',
+      'node_modules/jquery/dist/jquery.js',
+      'node_modules/bootstrap/dist/js/bootstrap.js',
+      'test/*.js'
+    ],
+
+    // list of files to exclude
+    exclude: [
+    ],
+
+    // preprocess matching files before serving them to the browser
+    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
+    preprocessors: {
+    },
+
+    // test results reporter to use
+    // possible values: 'dots', 'progress'
+    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
+    reporters: ['progress'],
+
+    // 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: ['PhantomJS', 'Firefox'],
+
+    // Continuous Integration mode
+    // if true, Karma captures browsers, runs the tests and exits
+    singleRun: false,
+
+    // Concurrency level
+    // how many browser should be started simultaneous
+    concurrency: Infinity
+  })
+}

+ 0 - 17
karma.js

@@ -1,17 +0,0 @@
-module.exports = function (config) {
-  config.set({
-    autoWatch: true,
-    singleRun: false,
-    frameworks: ['jasmine'],
-    files: [
-      'node_modules/jquery/dist/jquery.js',
-      'node_modules/bootstrap/dist/js/bootstrap.js',
-      'test/bootstrap-switch.js',
-      'test/bootstrap-switch.tests.js'
-    ],
-    colors: true,
-    browsers: ['PhantomJS'],
-    reporters: ['progress'],
-    plugins: ['karma-jasmine', 'karma-phantomjs-launcher']
-  })
-}

+ 29 - 20
package.json

@@ -14,14 +14,17 @@
     "email": "[email protected]",
     "url": "http://larentis.eu"
   },
-  "contributors": [{
-    "name": "Emanuele Marchi",
-    "email": "[email protected]",
-    "url": "http://lostcrew.it"
-  }, {
-    "name": "Peter Stein",
-    "url": "http://www.bdmdesign.org"
-  }],
+  "contributors": [
+    {
+      "name": "Emanuele Marchi",
+      "email": "[email protected]",
+      "url": "http://lostcrew.it"
+    },
+    {
+      "name": "Peter Stein",
+      "url": "http://www.bdmdesign.org"
+    }
+  ],
   "main": "dist/js/bootstrap-switch.js",
   "peerDependencies": {
     "bootstrap": "^3.1.1",
@@ -36,13 +39,17 @@
   "readmeFilename": "README.md",
   "devDependencies": {
     "babel-cli": "^6.22.2",
+    "babel-polyfill": "^6.22.0",
     "babel-preset-babili": "^0.0.10",
     "babel-preset-env": "^1.1.8",
+    "bootstrap": "^3.3.7",
     "jasmine": "^2.5.3",
     "jasmine-core": "^2.5.2",
-    "jquery": "^3.1.1",
+    "jquery": "^1.12.4",
     "karma": "^1.4.1",
+    "karma-babel-preprocessor": "^6.0.1",
     "karma-cli": "^1.0.1",
+    "karma-firefox-launcher": "^1.0.0",
     "karma-jasmine": "^1.1.0",
     "karma-phantomjs-launcher": "^1.0.2",
     "less": "^2.7.2",
@@ -53,9 +60,9 @@
   "scripts": {
     "js:dir": "mkdir -p dist/js",
     "js:lint": "standard src/js/**/*.js",
-    "js:test": "mkdir -p test && babel --out-file test/bootstrap-switch.test.js src/js/bootstrap-switch.test.js && karma start karma.js",
-    "js:base": "babel --out-file dist/js/bootstrap-switch.js src/js/bootstrap-switch.js",
-    "js:min": "NODE_ENV=production babel --out-file dist/js/bootstrap-switch.min.js src/js/bootstrap-switch.js",
+    "js:test": "mkdir -p test && babel -d test src/js && karma start karma.conf.js",
+    "js:base": "babel -o dist/js/bootstrap-switch.js src/js/bootstrap-switch.js",
+    "js:min": "NODE_ENV=production babel -o dist/js/bootstrap-switch.min.js src/js/bootstrap-switch.js",
     "js": "npm-run-all js:dir js:lint -p js:base js:min -s js:test",
     "css:dir": "mkdir -p dist/css",
     "css:bs2:base": "lessc src/less/bootstrap2/build.less dist/css/bootstrap2/bootstrap-switch.css",
@@ -69,13 +76,15 @@
     "start": "run-s clean build docs"
   },
   "npmName": "bootstrap-switch",
-  "npmFileMap": [{
-    "basePath": "/dist/",
-    "files": [
-      "*.js",
-      "css/bootstrap3/*.css",
-      "css/bootstrap2/*.css"
-    ]
-  }],
+  "npmFileMap": [
+    {
+      "basePath": "/dist/",
+      "files": [
+        "*.js",
+        "css/bootstrap3/*.css",
+        "css/bootstrap2/*.css"
+      ]
+    }
+  ],
   "dependencies": {}
 }

+ 2 - 2
src/js/bootstrap-switch.js

@@ -2,7 +2,7 @@
   class BootstrapSwitch {
     constructor (element, options = {}) {
       this.$element = $(element)
-      this.options = Object.asign(
+      this.options = Object.assign(
         {},
         $.fn.bootstrapSwitch.defaults,
         this._getElementOptions(),
@@ -11,7 +11,7 @@
       this.prevOptions = {}
 
       this.$wrapper = $('<div>', {
-        class () {
+        class: () => {
           const classes = []
           classes.push(this.options.state ? 'on' : 'off')
           if (this.options.size) {

+ 18 - 30
src/js/bootstrap-switch.test.js

@@ -7,7 +7,7 @@ describe('Bootstrap Switch:', function () {
     $.fx.off = true
   })
   afterEach(function () {
-    return $('.' + $.fn.bootstrapSwitch.defaults.baseClass).bootstrapSwitch('destroy')
+    return $(`.${$.fn.bootstrapSwitch.defaults.baseClass}`).destroy()
   })
 
   function createCheckbox () {
@@ -30,31 +30,23 @@ describe('Bootstrap Switch:', function () {
   }
 
   it('should set the default options as element options, except state', function () {
-    var $switch
-    $switch = createCheckbox().prop('checked', true).bootstrapSwitch()
+    const $switch = createCheckbox().prop('checked', true).bootstrapSwitch()
     expect(getOptions($switch)).toEqual($.fn.bootstrapSwitch.defaults)
   })
 
   it('should override default options with initialization ones', function () {
-    var $switch, $switch2
-    $switch = createCheckbox().prop('checked', false).bootstrapSwitch()
-    $switch2 = createCheckbox().bootstrapSwitch({
-      state: false
-    })
+    const $switch = createCheckbox().prop('checked', false).bootstrapSwitch()
+    const $switch2 = createCheckbox().bootstrapSwitch({ state: false })
     expect(getOptions($switch).state).toBe(false)
     expect(getOptions($switch2).state).toBe(false)
   })
 
   it('should something', function () {
-    var $switch, eventDoc, eventElement
-    eventDoc = eventElement = 0
-    $switch = createCheckbox().bootstrapSwitch()
-    $(document).on('switchChange.bootstrapSwitch', ':checkbox', function (event, state) {
-      return eventDoc++
-    })
-    $(':checkbox').on('switchChange.bootstrapSwitch', function (event, state) {
-      return eventElement++
-    })
+    const $switch = createCheckbox().bootstrapSwitch()
+    let eventDoc = 0
+    let eventElement = 0
+    $(document).on('switchChange.bootstrapSwitch', ':checkbox', function (event, state) { eventDoc++ })
+    $(':checkbox').on('switchChange.bootstrapSwitch', function (event, state) { eventElement++ })
     $switch.click()
     expect(eventElement).toEqual(eventDoc)
     expect(eventElement).toEqual(1)
@@ -62,14 +54,13 @@ describe('Bootstrap Switch:', function () {
 
   describe('The Checkbox Bootstrap Switch', function () {
     it('should conserve its state if onSwitchChange returns false', function () {
-      var $indeterminateSwitch, $switch
-      $switch = createCheckbox().bootstrapSwitch({
+      const $switch = createCheckbox().bootstrapSwitch({
         onSwitchChange: function (e, s) {
           expect(s).toEqual(true)
           return false
         }
       })
-      $indeterminateSwitch = createCheckbox().data('indeterminate', true).bootstrapSwitch({
+      const $indeterminateSwitch = createCheckbox().data('indeterminate', true).bootstrapSwitch({
         onSwitchChange: function (e, s) {
           expect(s).toEqual(true)
           return false
@@ -82,8 +73,7 @@ describe('Bootstrap Switch:', function () {
     })
 
     it('should change its state if onSwitchChange not returns false', function () {
-      var $switch
-      $switch = createCheckbox().bootstrapSwitch({
+      const $switch = createCheckbox().bootstrapSwitch({
         onSwitchChange: function (e, s) {
           expect(s).toEqual(true)
         }
@@ -95,10 +85,9 @@ describe('Bootstrap Switch:', function () {
 
   describe('The Radio Bootstrap Switch', function () {
     it('should conserve its state if onSwitchChange returns false', function () {
-      var $radio1, $radio2, $radio3
-      $radio1 = createRadio().prop('checked', true)
-      $radio2 = createRadio().prop('checked', false)
-      $radio3 = createRadio().prop('checked', false)
+      const $radio1 = createRadio().prop('checked', true)
+      const $radio2 = createRadio().prop('checked', false)
+      const $radio3 = createRadio().prop('checked', false)
       $('[name="name"]').bootstrapSwitch({
         onSwitchChange: function (e, s) {
           expect(s).toEqual(true)
@@ -112,10 +101,9 @@ describe('Bootstrap Switch:', function () {
     })
 
     it('should change its state if onSwitchChange not returns false', function () {
-      var $radio1, $radio2, $radio3
-      $radio1 = createRadio().prop('checked', true)
-      $radio2 = createRadio().prop('checked', false)
-      $radio3 = createRadio().prop('checked', false)
+      const $radio1 = createRadio().prop('checked', true)
+      const $radio2 = createRadio().prop('checked', false)
+      const $radio3 = createRadio().prop('checked', false)
       $('[name="name"]').bootstrapSwitch({
         onSwitchChange: function (e, s) {
           expect(s).toEqual(true)