Browse Source

test: basic 2.x compat test case

Evan You 7 years ago
parent
commit
232d14c0dd

+ 5 - 0
packages/core/src/componentUtils.ts

@@ -183,6 +183,11 @@ export function createComponentClassFromOptions(
         set: isGet ? undefined : value.set
       })
     }
+    if (key === 'methods') {
+      for (const method in value) {
+        ;(ObjectComponent.prototype as any)[method] = value[method]
+      }
+    }
   }
   return ObjectComponent as ComponentClass
 }

+ 32 - 0
packages/vue/__tests__/compat.spec.ts

@@ -0,0 +1,32 @@
+;(global as any).__COMPAT__ = true
+
+import Vue from '../src/index'
+
+describe('2.x compat build', async () => {
+  test('should work', async () => {
+    const root = document.createElement('div')
+    document.body.appendChild(root)
+
+    const instance = new Vue({
+      data() {
+        return { count: 0 }
+      },
+      methods: {
+        change() {
+          this.count++
+        }
+      },
+      render(h: any) {
+        return h('div', this.count)
+      }
+    }).$mount(root)
+
+    expect(instance.count).toBe(0)
+    expect(root.textContent).toBe('0')
+
+    instance.change()
+    expect(instance.count).toBe(1)
+    await Vue.nextTick()
+    expect(root.textContent).toBe('1')
+  })
+})

+ 4 - 2
packages/vue/src/index.ts

@@ -1,6 +1,7 @@
 import {
   h,
   render,
+  nextTick,
   Component,
   ComponentOptions,
   createComponentInstance
@@ -9,8 +10,9 @@ import {
 class Vue extends Component {
   static h = h
   static render = render
+  static nextTick = nextTick
 
-  constructor(options: ComponentOptions & { el: any }) {
+  constructor(options: ComponentOptions & { el?: any }) {
     super()
     if (!options) {
       return
@@ -21,7 +23,7 @@ class Vue extends Component {
     vnode.children = instance
 
     function mount(el: any) {
-      const dom = document.querySelector(el)
+      const dom = typeof el === 'string' ? document.querySelector(el) : el
       render(vnode, dom)
       return instance.$proxy
     }