vue-test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import Vue, { VNode } from '../index'
  2. import { ComponentOptions } from '../options'
  3. class Test extends Vue {
  4. a: number = 0
  5. testProperties() {
  6. this.$data
  7. this.$el
  8. this.$options
  9. this.$parent
  10. this.$root
  11. this.$children
  12. this.$refs
  13. this.$slots
  14. this.$isServer
  15. this.$ssrContext
  16. this.$vnode
  17. }
  18. // test property reification
  19. $el!: HTMLElement | SVGElement
  20. $refs!: {
  21. vue: Vue
  22. element: HTMLInputElement
  23. vues: Vue[]
  24. elements: HTMLInputElement[]
  25. }
  26. testReification() {
  27. this.$refs.vue.$data
  28. this.$refs.element.value
  29. this.$refs.vues[0].$data
  30. this.$refs.elements[0].value
  31. }
  32. testMethods() {
  33. this.$mount('#app', false)
  34. this.$forceUpdate()
  35. this.$destroy()
  36. this.$set({}, 'key', 'value')
  37. this.$delete({}, 'key')
  38. this.$watch('a', (val: number, oldVal: number) => {}, {
  39. immediate: true,
  40. deep: false
  41. })()
  42. this.$watch(
  43. () => this.a,
  44. (val: number) => {}
  45. )
  46. this.$on('', () => {})
  47. this.$once('', () => {})
  48. this.$off('', () => {})
  49. this.$emit('', 1, 2, 3)
  50. this.$nextTick(function () {
  51. this.$nextTick
  52. })
  53. this.$nextTick().then(() => {})
  54. this.$createElement('div', {}, 'message')
  55. }
  56. static testConfig() {
  57. const { config } = this
  58. config.silent
  59. config.optionMergeStrategies
  60. config.devtools
  61. config.errorHandler = (err, vm) => {
  62. if (vm instanceof Test) {
  63. vm.testProperties()
  64. vm.testMethods()
  65. }
  66. }
  67. config.warnHandler = (msg, vm) => {
  68. if (vm instanceof Test) {
  69. vm.testProperties()
  70. vm.testMethods()
  71. }
  72. }
  73. config.keyCodes = { esc: 27 }
  74. config.ignoredElements = ['foo', /^ion-/]
  75. config.async = false
  76. }
  77. static testMethods() {
  78. this.extend({
  79. data() {
  80. return {
  81. msg: ''
  82. }
  83. }
  84. })
  85. this.nextTick(() => {})
  86. this.nextTick(
  87. function () {
  88. console.log(this.text === 'test')
  89. },
  90. { text: 'test' }
  91. )
  92. this.nextTick().then(() => {})
  93. this.set({}, '', '')
  94. this.set({}, 1, '')
  95. this.set([true, false, true], 1, true)
  96. this.delete({}, '')
  97. this.delete({}, 1)
  98. this.delete([true, false], 0)
  99. this.directive('', { bind() {} })
  100. this.filter('', (value: number) => value)
  101. this.component('', { data: () => ({}) })
  102. this.component('', {
  103. functional: true,
  104. render(h) {
  105. return h('div', 'hello!')
  106. }
  107. })
  108. this.use
  109. this.mixin(Test)
  110. this.compile('<div>{{ message }}</div>')
  111. this.use(() => {})
  112. .use(() => {})
  113. .mixin({})
  114. .mixin({})
  115. }
  116. }
  117. const HelloWorldComponent = Vue.extend({
  118. props: ['name'],
  119. data() {
  120. return {
  121. message: 'Hello ' + this.name
  122. }
  123. },
  124. computed: {
  125. shouted(): string {
  126. return this.message.toUpperCase()
  127. }
  128. },
  129. methods: {
  130. getMoreExcited() {
  131. this.message += '!'
  132. }
  133. },
  134. watch: {
  135. message(a: string) {
  136. console.log(`Message ${this.message} was changed!`)
  137. }
  138. }
  139. })
  140. const FunctionalHelloWorldComponent = Vue.extend({
  141. functional: true,
  142. props: ['name'],
  143. render(createElement, ctxt) {
  144. return createElement('div', 'Hello ' + ctxt.props.name)
  145. }
  146. })
  147. const FunctionalScopedSlotsComponent = Vue.extend({
  148. functional: true,
  149. render(h, ctx) {
  150. return (
  151. (ctx.scopedSlots.default && ctx.scopedSlots.default({})) ||
  152. h('div', 'functional scoped slots')
  153. )
  154. }
  155. })
  156. const Parent = Vue.extend({
  157. data() {
  158. return { greeting: 'Hello' }
  159. }
  160. })
  161. const Child = Parent.extend({
  162. methods: {
  163. foo() {
  164. console.log(this.greeting.toLowerCase())
  165. }
  166. }
  167. })
  168. const GrandChild = Child.extend({
  169. computed: {
  170. lower(): string {
  171. return this.greeting.toLowerCase()
  172. }
  173. }
  174. })
  175. new GrandChild().lower.toUpperCase()
  176. for (let _ in new Test().$options) {
  177. }
  178. declare const options: ComponentOptions<Vue>
  179. Vue.extend(options)
  180. Vue.component('test-comp', options)
  181. new Vue(options)
  182. // cyclic example
  183. Vue.extend({
  184. props: {
  185. bar: {
  186. type: String
  187. }
  188. },
  189. methods: {
  190. foo() {}
  191. },
  192. mounted() {
  193. this.foo()
  194. },
  195. // manual annotation
  196. render(h): VNode {
  197. const a = this.bar
  198. return h('canvas', {}, [a])
  199. }
  200. })
  201. declare function decorate<VC extends typeof Vue>(v: VC): VC
  202. @decorate
  203. class Decorated extends Vue {
  204. a = 123
  205. }
  206. const obj = Vue.observable({ a: 1 })
  207. obj.a++
  208. // VNodeData style tests.
  209. const ComponentWithStyleInVNodeData = Vue.extend({
  210. render(h) {
  211. const elementWithStyleAsString = h('div', {
  212. style: 'background-color: red;'
  213. })
  214. const elementWithStyleAsObject = h('div', {
  215. style: { backgroundColor: 'green' }
  216. })
  217. const elementWithStyleAsArrayOfObjects = h('div', {
  218. style: [{ backgroundColor: 'blue' }]
  219. })
  220. return h('div', undefined, [
  221. elementWithStyleAsString,
  222. elementWithStyleAsObject,
  223. elementWithStyleAsArrayOfObjects
  224. ])
  225. }
  226. })