]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: tests for script setup helpers
authorEvan You <yyx990803@gmail.com>
Thu, 26 Nov 2020 15:01:36 +0000 (10:01 -0500)
committerEvan You <yyx990803@gmail.com>
Thu, 26 Nov 2020 15:01:36 +0000 (10:01 -0500)
packages/runtime-core/__tests__/apiSetupHelpers.spec.ts [new file with mode: 0644]
packages/runtime-core/src/apiSetupHelpers.ts

diff --git a/packages/runtime-core/__tests__/apiSetupHelpers.spec.ts b/packages/runtime-core/__tests__/apiSetupHelpers.spec.ts
new file mode 100644 (file)
index 0000000..3c0e228
--- /dev/null
@@ -0,0 +1,49 @@
+import {
+  defineComponent,
+  h,
+  nodeOps,
+  render,
+  SetupContext
+} from '@vue/runtime-test'
+import { defineEmit, defineProps, useContext } from '../src/apiSetupHelpers'
+
+describe('SFC <script setup> helpers', () => {
+  test('should warn runtime usage', () => {
+    defineProps()
+    expect(`defineProps() is a compiler-hint`).toHaveBeenWarned()
+
+    defineEmit()
+    expect(`defineEmit() is a compiler-hint`).toHaveBeenWarned()
+  })
+
+  test('useContext (no args)', () => {
+    let ctx: SetupContext | undefined
+    const Comp = {
+      setup() {
+        ctx = useContext()
+        return () => {}
+      }
+    }
+    render(h(Comp), nodeOps.createElement('div'))
+    expect(ctx).toMatchObject({
+      attrs: {},
+      slots: {}
+    })
+    expect(typeof ctx!.emit).toBe('function')
+  })
+
+  test('useContext (with args)', () => {
+    let ctx: SetupContext | undefined
+    let ctxArg: SetupContext | undefined
+    const Comp = defineComponent({
+      setup(_, _ctxArg) {
+        ctx = useContext()
+        ctxArg = _ctxArg
+        return () => {}
+      }
+    })
+    render(h(Comp), nodeOps.createElement('div'))
+    expect(ctx).toBeDefined()
+    expect(ctx).toBe(ctxArg)
+  })
+})
index 719412ee074ba74a0405c29c711e19232e3bccda..7858bb39d9b6a2838ae15dbb7d9ee59993f34415 100644 (file)
@@ -27,8 +27,8 @@ export function defineProps<
   InferredProps = ExtractPropTypes<PP>
 >(props?: PP): Readonly<TypeProps extends undefined ? InferredProps : TypeProps>
 // implementation
-export function defineProps(props?: any) {
-  if (__DEV__ && props) {
+export function defineProps() {
+  if (__DEV__) {
     warn(
       `defineProps() is a compiler-hint helper that is only usable inside ` +
         `<script setup> of a single file component. Its arguments should be ` +
@@ -45,8 +45,8 @@ export function defineEmit<
   InferredEmit = EmitFn<E>
 >(emitOptions?: E | EE[]): TypeEmit extends undefined ? InferredEmit : TypeEmit
 // implementation
-export function defineEmit(emitOptions?: any) {
-  if (__DEV__ && emitOptions) {
+export function defineEmit() {
+  if (__DEV__) {
     warn(
       `defineEmit() is a compiler-hint helper that is only usable inside ` +
         `<script setup> of a single file component. Its arguments should be ` +