Browse Source

tests for v-style

Evan You 11 years ago
parent
commit
534b871648
2 changed files with 45 additions and 4 deletions
  1. 7 3
      src/directives/style.js
  2. 38 1
      test/unit/specs/directives/style_spec.js

+ 7 - 3
src/directives/style.js

@@ -1,5 +1,5 @@
 var prefixes = ['-webkit-', '-moz-', '-ms-']
-var importantRE = /!important$/
+var importantRE = /!important;?$/
 
 module.exports = {
 
@@ -25,13 +25,17 @@ module.exports = {
         ? 'important'
         : ''
       if (isImportant) {
-        value = value.slice(0, -10).trim()
+        value = value.replace(importantRE, '').trim()
       }
       this.el.style.setProperty(prop, value, isImportant)
       if (this.prefixed) {
         var i = prefixes.length
         while (i--) {
-          this.el.style.setProperty(prefixes[i] + prop, value, isImportant)
+          this.el.style.setProperty(
+            prefixes[i] + prop,
+            value,
+            isImportant
+          )
         }
       }
     } else {

+ 38 - 1
test/unit/specs/directives/style_spec.js

@@ -4,7 +4,44 @@ var def = require('../../../../src/directives/style')
 if (_.inBrowser) {
   describe('v-style', function () {
 
-    // TODO
+    var el, dir
+    beforeEach(function () {
+      el = document.createElement('div')
+      dir = { el: el }
+      _.extend(dir, def)      
+    })
+
+    it('normal with arg', function () {
+      dir.arg = 'color'
+      dir.bind()
+      dir.update('red')
+      expect(el.style.color).toBe('red')
+    })
+
+    it('normal no arg', function () {
+      dir.bind()
+      dir.update('color:red;')
+      expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
+    })
+
+    it('!important', function () {
+      dir.arg = 'color'
+      dir.bind()
+      dir.update('red !important;')
+      expect(el.style.getPropertyPriority('color')).toBe('important')
+    })
+
+    it('auto prefixing', function () {
+      var spy = el.style.setProperty = jasmine.createSpy()
+      dir.arg = '$transform'
+      dir.bind()
+      var val = 'scale(0.5)'
+      dir.update(val)
+      expect(spy).toHaveBeenCalledWith('transform', val, '')
+      expect(spy).toHaveBeenCalledWith('-ms-transform', val, '')
+      expect(spy).toHaveBeenCalledWith('-moz-transform', val, '')
+      expect(spy).toHaveBeenCalledWith('-webkit-transform', val, '')
+    })
 
   })
 }