]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: improve coverage
authorEvan You <yyx990803@gmail.com>
Wed, 15 Jul 2020 14:38:45 +0000 (10:38 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 15 Jul 2020 14:38:45 +0000 (10:38 -0400)
packages/runtime-core/__tests__/rendererChildren.spec.ts
packages/runtime-core/__tests__/rendererFragment.spec.ts
packages/runtime-core/src/component.ts
packages/runtime-core/src/warning.ts
packages/runtime-dom/__tests__/helpers/useCssModule.spec.ts [new file with mode: 0644]
packages/runtime-dom/src/helpers/useCssModule.ts
packages/runtime-dom/src/helpers/useCssVars.ts

index 93ef3b99b73d58e287b9dd5b81cd37cff45f866f..22c2d101b62a55c8b876874dd70baa11795f2970 100644 (file)
@@ -40,7 +40,7 @@ function shuffle(array: Array<any>) {
   return array
 }
 
-it('should patch previously empty children', () => {
+test('should patch previously empty children', () => {
   const root = nodeOps.createElement('div')
 
   render(h('div', []), root)
@@ -50,7 +50,7 @@ it('should patch previously empty children', () => {
   expect(inner(root)).toBe('<div>hello</div>')
 })
 
-it('should patch previously null children', () => {
+test('should patch previously null children', () => {
   const root = nodeOps.createElement('div')
 
   render(h('div'), root)
@@ -60,6 +60,15 @@ it('should patch previously null children', () => {
   expect(inner(root)).toBe('<div>hello</div>')
 })
 
+test('array children -> text children', () => {
+  const root = nodeOps.createElement('div')
+  render(h('div', [h('div')]), root)
+  expect(inner(root)).toBe('<div><div></div></div>')
+
+  render(h('div', 'hello'), root)
+  expect(inner(root)).toBe('<div>hello</div>')
+})
+
 describe('renderer: keyed children', () => {
   let root: TestElement
   let elm: TestElement
index a44c89d3bd464cd9fb9b9db785951e96b8be6be1..9e1f87aec8e3dc3f8a309ef7d77b30a7d46af52c 100644 (file)
@@ -129,6 +129,20 @@ describe('renderer: fragment', () => {
       root
     )
     expect(serializeInner(root)).toBe(`<div>foo</div>barbaz`)
+
+    render(
+      createVNode(
+        Fragment,
+        null,
+        [
+          createTextVNode('baz'),
+          createVNode('div', null, 'foo', PatchFlags.TEXT)
+        ],
+        PatchFlags.UNKEYED_FRAGMENT
+      ),
+      root
+    )
+    expect(serializeInner(root)).toBe(`baz<div>foo</div>`)
   })
 
   it('patch fragment children (compiler generated, keyed)', () => {
index 404bb15bd4c2dad3ff1b31c1de5d9896b0067abb..a3a1127691146d34b893bd415fe81128d3d57343 100644 (file)
@@ -690,6 +690,7 @@ const classifyRE = /(?:^|[-_])(\w)/g
 const classify = (str: string): string =>
   str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
 
+/* istanbul ignore next */
 export function formatComponentName(
   instance: ComponentInternalInstance | null,
   Component: Component,
index 07f8a27b7f9157e693170baf75f19e15c8c99cb5..f297d92607f41e4462d7caff8d32dd7072855c86 100644 (file)
@@ -57,6 +57,7 @@ export function warn(msg: string, ...args: any[]) {
     )
   } else {
     const warnArgs = [`[Vue warn]: ${msg}`, ...args]
+    /* istanbul ignore if */
     if (
       trace.length &&
       // avoid spamming console during tests
@@ -99,6 +100,7 @@ function getComponentTrace(): ComponentTraceStack {
   return normalizedStack
 }
 
+/* istanbul ignore next */
 function formatTrace(trace: ComponentTraceStack): any[] {
   const logs: any[] = []
   trace.forEach((entry, i) => {
@@ -122,6 +124,7 @@ function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] {
     : [open + close]
 }
 
+/* istanbul ignore next */
 function formatProps(props: Data): any[] {
   const res: any[] = []
   const keys = Object.keys(props)
@@ -136,6 +139,7 @@ function formatProps(props: Data): any[] {
 
 function formatProp(key: string, value: unknown): any[]
 function formatProp(key: string, value: unknown, raw: true): any
+/* istanbul ignore next */
 function formatProp(key: string, value: unknown, raw?: boolean): any {
   if (isString(value)) {
     value = JSON.stringify(value)
diff --git a/packages/runtime-dom/__tests__/helpers/useCssModule.spec.ts b/packages/runtime-dom/__tests__/helpers/useCssModule.spec.ts
new file mode 100644 (file)
index 0000000..b92c9c1
--- /dev/null
@@ -0,0 +1,55 @@
+import { render, h, nodeOps } from '@vue/runtime-test'
+import { useCssModule } from '../../src/helpers/useCssModule'
+import { mockWarn } from '@vue/shared'
+
+describe('useCssModule', () => {
+  mockWarn()
+
+  function mountWithModule(modules: any, name?: string) {
+    let res
+    render(
+      h({
+        render() {},
+        __cssModules: modules,
+        setup() {
+          res = useCssModule(name)
+        }
+      }),
+      nodeOps.createElement('div')
+    )
+    return res
+  }
+
+  test('basic usage', () => {
+    const modules = {
+      $style: {
+        red: 'red'
+      }
+    }
+    expect(mountWithModule(modules)).toMatchObject(modules.$style)
+  })
+
+  test('basic usage', () => {
+    const modules = {
+      foo: {
+        red: 'red'
+      }
+    }
+    expect(mountWithModule(modules, 'foo')).toMatchObject(modules.foo)
+  })
+
+  test('warn out of setup usage', () => {
+    useCssModule()
+    expect('must be called inside setup').toHaveBeenWarned()
+  })
+
+  test('warn missing injection', () => {
+    mountWithModule(undefined)
+    expect('instance does not have CSS modules').toHaveBeenWarned()
+  })
+
+  test('warn missing injection', () => {
+    mountWithModule({ $style: { red: 'red' } }, 'foo')
+    expect('instance does not have CSS module named "foo"').toHaveBeenWarned()
+  })
+})
index 18b7048f63f764ef490ef74abde8af2cf9008d9b..d96dc69c3426938c62e6a1a8f16ea29dc8e9976a 100644 (file)
@@ -2,10 +2,11 @@ import { warn, getCurrentInstance } from '@vue/runtime-core'
 import { EMPTY_OBJ } from '@vue/shared'
 
 export function useCssModule(name = '$style'): Record<string, string> {
+  /* istanbul ignore else */
   if (!__GLOBAL__) {
     const instance = getCurrentInstance()!
     if (!instance) {
-      __DEV__ && warn(`useCSSModule must be called inside setup()`)
+      __DEV__ && warn(`useCssModule must be called inside setup()`)
       return EMPTY_OBJ
     }
     const modules = instance.type.__cssModules
@@ -22,7 +23,7 @@ export function useCssModule(name = '$style'): Record<string, string> {
     return mod as Record<string, string>
   } else {
     if (__DEV__) {
-      warn(`useCSSModule() is not supported in the global build.`)
+      warn(`useCssModule() is not supported in the global build.`)
     }
     return EMPTY_OBJ
   }
index 5bf4f46971914be61161fb497119eb822ca81fb3..fde9388018655376de4fed7de50d604924dbf54f 100644 (file)
@@ -14,6 +14,7 @@ export function useCssVars(
   scoped = false
 ) {
   const instance = getCurrentInstance()
+  /* istanbul ignore next */
   if (!instance) {
     __DEV__ &&
       warn(`useCssVars is called without current active component instance.`)