debug.spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import Vue from 'vue'
  2. import { formatComponentName, warn } from 'core/util/debug'
  3. describe('Debug utilities', () => {
  4. it('properly format component names', () => {
  5. const vm = new Vue()
  6. expect(formatComponentName(vm)).toBe('<Root>')
  7. vm.$root = null
  8. vm.$options.name = 'hello-there'
  9. expect(formatComponentName(vm)).toBe('<HelloThere>')
  10. vm.$options.name = null
  11. vm.$options._componentTag = 'foo-bar-1'
  12. expect(formatComponentName(vm)).toBe('<FooBar1>')
  13. vm.$options._componentTag = null
  14. vm.$options.__file = '/foo/bar/baz/SomeThing.vue'
  15. expect(formatComponentName(vm)).toBe(`<SomeThing> at ${vm.$options.__file}`)
  16. expect(formatComponentName(vm, false)).toBe('<SomeThing>')
  17. vm.$options.__file = 'C:\\foo\\bar\\baz\\windows_file.vue'
  18. expect(formatComponentName(vm)).toBe(`<WindowsFile> at ${vm.$options.__file}`)
  19. expect(formatComponentName(vm, false)).toBe('<WindowsFile>')
  20. })
  21. it('generate correct component hierarchy trace', () => {
  22. const one = {
  23. name: 'one',
  24. render: h => h(two)
  25. }
  26. const two = {
  27. name: 'two',
  28. render: h => h(three)
  29. }
  30. const three = {
  31. name: 'three'
  32. }
  33. new Vue({
  34. render: h => h(one)
  35. }).$mount()
  36. expect(
  37. `Failed to mount component: template or render function not defined.
  38. found in
  39. ---> <Three>
  40. <Two>
  41. <One>
  42. <Root>`
  43. ).toHaveBeenWarned()
  44. })
  45. it('generate correct component hierarchy trace (recursive)', () => {
  46. let i = 0
  47. const one = {
  48. name: 'one',
  49. render: h => i++ < 5 ? h(one) : h(two)
  50. }
  51. const two = {
  52. name: 'two',
  53. render: h => h(three)
  54. }
  55. const three = {
  56. name: 'three'
  57. }
  58. new Vue({
  59. render: h => h(one)
  60. }).$mount()
  61. expect(
  62. `Failed to mount component: template or render function not defined.
  63. found in
  64. ---> <Three>
  65. <Two>
  66. <One>... (5 recursive calls)
  67. <Root>`
  68. ).toHaveBeenWarned()
  69. })
  70. describe('warn', () => {
  71. const msg = 'message'
  72. const vm = new Vue()
  73. it('calls warnHandler if warnHandler is set', () => {
  74. Vue.config.warnHandler = jasmine.createSpy()
  75. warn(msg, vm)
  76. expect(Vue.config.warnHandler).toHaveBeenCalledWith(msg, vm, jasmine.any(String))
  77. Vue.config.warnHandler = null
  78. })
  79. it('calls console.error if silent is false', () => {
  80. Vue.config.silent = false
  81. warn(msg, vm)
  82. expect(msg).toHaveBeenWarned()
  83. expect(console.error).toHaveBeenCalled()
  84. })
  85. it('does not call console.error if silent is true', () => {
  86. Vue.config.silent = true
  87. warn(msg, vm)
  88. expect(console.error).not.toHaveBeenCalled()
  89. Vue.config.silent = false
  90. })
  91. })
  92. })