Răsfoiți Sursa

warn unknown custom elements (test)

Evan You 10 ani în urmă
părinte
comite
fe5fca2baa
2 a modificat fișierele cu 24 adăugiri și 6 ștergeri
  1. 14 6
      src/util/component.js
  2. 10 0
      test/unit/specs/misc_spec.js

+ 14 - 6
src/util/component.js

@@ -17,13 +17,21 @@ exports.checkComponent = function (el, options) {
     var exp = el.getAttribute('is')
     el.removeAttribute('is')
     return exp
-  } else if (
-    !exports.commonTagRE.test(tag) &&
-    _.resolveAsset(options, 'components', tag)
-  ) {
-    return tag
+  } else if (!exports.commonTagRE.test(tag)) {
+    if (_.resolveAsset(options, 'components', tag)) {
+      return tag
+    } else if (process.env.NODE_ENV !== 'production') {
+      if (tag.indexOf('-') > -1 ||
+          /HTMLUnknownElement/.test(Object.prototype.toString.call(el))) {
+        _.warn(
+          'Unknown custom element: <' + tag + '> - did you ' +
+          'register the component correctly?'
+        )
+      }
+    }
+  }
   /* eslint-disable no-cond-assign */
-  } else if (tag = _.attr(el, 'component')) {
+  if (tag = _.attr(el, 'component')) {
   /* eslint-enable no-cond-assign */
     return tag
   }

+ 10 - 0
test/unit/specs/misc_spec.js

@@ -1,11 +1,14 @@
 // test cases for edge cases & bug fixes
 var Vue = require('../../../src/vue')
+// spies on different objects
 var _ = require('../../../src/util/debug')
+var __ = Vue.util
 
 describe('Misc', function () {
 
   beforeEach(function () {
     spyOn(_, 'warn')
+    spyOn(__, 'warn')
   })
 
   it('should handle directive.bind() altering its childNode structure', function () {
@@ -348,4 +351,11 @@ describe('Misc', function () {
     })
   })
 
+  it('warn unkown custom element', function () {
+    new Vue({
+      el: document.createElement('div'),
+      template: '<custom-stuff></custom-stuff>'
+    })
+    expect(hasWarned(__, 'Unknown custom element')).toBe(true)
+  })
 })