]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: improve tests and typing for runtime-core (#100)
authorCarlos Rodrigues <carlos@hypermob.co.uk>
Sat, 5 Oct 2019 19:37:55 +0000 (20:37 +0100)
committerEvan You <yyx990803@gmail.com>
Sat, 5 Oct 2019 19:37:55 +0000 (15:37 -0400)
* test: add test case for declaring Array prop type with constructor casting in `createComponent`

* test: add test case for `setup(props)` with explicit props declaration

packages/runtime-core/__tests__/apiApp.spec.ts
packages/runtime-core/__tests__/apiCreateComponent.spec.tsx
packages/runtime-core/__tests__/apiSetupContext.spec.ts
packages/runtime-core/__tests__/rendererSuspense.spec.ts

index d01ffb254fba70f932e14d22eeba1660dd9957e9..64899eba15af9b1acb089bd547a7a2c5325f5f20 100644 (file)
@@ -124,10 +124,10 @@ describe('api: createApp', () => {
       },
       setup() {
         // resolve in setup
-        const FooBar = resolveDirective('foo-bar') as any
+        const FooBar = resolveDirective('foo-bar')!
         return () => {
           // resolve in render
-          const BarBaz = resolveDirective('bar-baz') as any
+          const BarBaz = resolveDirective('bar-baz')!
           return applyDirectives(h('div'), [[FooBar], [BarBaz]])
         }
       }
index 978cd32a06e513b5357367d4d8f48e82479bb142..c64eaa074885eb6764b2fe4cdcb287a8559fb1da 100644 (file)
@@ -22,10 +22,17 @@ test('createComponent type inference', () => {
         default: 'hello'
       },
       // explicit type casting
-      cc: (Array as any) as PropType<string[]>,
+      cc: Array as PropType<string[]>,
       // required + type casting
       dd: {
-        type: (Array as any) as PropType<string[]>,
+        type: Array as PropType<string[]>,
+        required: true
+      },
+      // explicit type casting with constructor
+      ccc: Array as () => string[],
+      // required + contructor type casting
+      ddd: {
+        type: Array as () => string[],
         required: true
       }
     } as const, // required to narrow for conditional check
@@ -60,7 +67,7 @@ test('createComponent type inference', () => {
     }
   })
   // test TSX props inference
-  ;(<MyComponent a={1} b="foo" dd={['foo']}/>)
+  ;(<MyComponent a={1} b="foo" dd={['foo']} ddd={['foo']}/>)
 })
 
 test('type inference w/ optional props declaration', () => {
index fd9c8ab7387fffc4c3f890aa8f150c6e729b3f44..d0602fc99fcf4bcc90fd5d884827585063326d44 100644 (file)
@@ -74,6 +74,39 @@ describe('api: setup context', () => {
     expect(dummy).toBe(1)
   })
 
+  it('setup props should resolve the correct types from props object', async () => {
+    const count = ref(0)
+    let dummy
+
+    const Parent = {
+      render: () => h(Child, { count: count.value })
+    }
+
+    const Child = createComponent({
+      props: {
+        count: Number
+      },
+
+      setup(props) {
+        watch(() => {
+          dummy = props.count
+        })
+        return () => h('div', props.count)
+      }
+    })
+
+    const root = nodeOps.createElement('div')
+    render(h(Parent), root)
+    expect(serializeInner(root)).toMatch(`<div>0</div>`)
+    expect(dummy).toBe(0)
+
+    // props should be reactive
+    count.value++
+    await nextTick()
+    expect(serializeInner(root)).toMatch(`<div>1</div>`)
+    expect(dummy).toBe(1)
+  })
+
   it('context.attrs', async () => {
     const toggle = ref(true)
 
index 78221da42fdcb3a54fa2d43399a86da132fba75e..62e5001743768de074e5beeeb9555794b3d3c6a3 100644 (file)
@@ -517,7 +517,7 @@ describe('renderer: suspense', () => {
 
     const Comp = {
       setup() {
-        const error = ref<any>(null)
+        const error = ref<Error | null>(null)
         onErrorCaptured(e => {
           error.value = e
           return true