Skip to content

Commit 32a4cb8

Browse files
committed
test: improve coverage
1 parent 056a131 commit 32a4cb8

File tree

7 files changed

+89
-4
lines changed

7 files changed

+89
-4
lines changed

packages/runtime-core/__tests__/rendererChildren.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function shuffle(array: Array<any>) {
4040
return array
4141
}
4242

43-
it('should patch previously empty children', () => {
43+
test('should patch previously empty children', () => {
4444
const root = nodeOps.createElement('div')
4545

4646
render(h('div', []), root)
@@ -50,7 +50,7 @@ it('should patch previously empty children', () => {
5050
expect(inner(root)).toBe('<div>hello</div>')
5151
})
5252

53-
it('should patch previously null children', () => {
53+
test('should patch previously null children', () => {
5454
const root = nodeOps.createElement('div')
5555

5656
render(h('div'), root)
@@ -60,6 +60,15 @@ it('should patch previously null children', () => {
6060
expect(inner(root)).toBe('<div>hello</div>')
6161
})
6262

63+
test('array children -> text children', () => {
64+
const root = nodeOps.createElement('div')
65+
render(h('div', [h('div')]), root)
66+
expect(inner(root)).toBe('<div><div></div></div>')
67+
68+
render(h('div', 'hello'), root)
69+
expect(inner(root)).toBe('<div>hello</div>')
70+
})
71+
6372
describe('renderer: keyed children', () => {
6473
let root: TestElement
6574
let elm: TestElement

packages/runtime-core/__tests__/rendererFragment.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ describe('renderer: fragment', () => {
129129
root
130130
)
131131
expect(serializeInner(root)).toBe(`<div>foo</div>barbaz`)
132+
133+
render(
134+
createVNode(
135+
Fragment,
136+
null,
137+
[
138+
createTextVNode('baz'),
139+
createVNode('div', null, 'foo', PatchFlags.TEXT)
140+
],
141+
PatchFlags.UNKEYED_FRAGMENT
142+
),
143+
root
144+
)
145+
expect(serializeInner(root)).toBe(`baz<div>foo</div>`)
132146
})
133147

134148
it('patch fragment children (compiler generated, keyed)', () => {

packages/runtime-core/src/component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ const classifyRE = /(?:^|[-_])(\w)/g
690690
const classify = (str: string): string =>
691691
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
692692

693+
/* istanbul ignore next */
693694
export function formatComponentName(
694695
instance: ComponentInternalInstance | null,
695696
Component: Component,

packages/runtime-core/src/warning.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export function warn(msg: string, ...args: any[]) {
5757
)
5858
} else {
5959
const warnArgs = [`[Vue warn]: ${msg}`, ...args]
60+
/* istanbul ignore if */
6061
if (
6162
trace.length &&
6263
// avoid spamming console during tests
@@ -99,6 +100,7 @@ function getComponentTrace(): ComponentTraceStack {
99100
return normalizedStack
100101
}
101102

103+
/* istanbul ignore next */
102104
function formatTrace(trace: ComponentTraceStack): any[] {
103105
const logs: any[] = []
104106
trace.forEach((entry, i) => {
@@ -122,6 +124,7 @@ function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] {
122124
: [open + close]
123125
}
124126

127+
/* istanbul ignore next */
125128
function formatProps(props: Data): any[] {
126129
const res: any[] = []
127130
const keys = Object.keys(props)
@@ -136,6 +139,7 @@ function formatProps(props: Data): any[] {
136139

137140
function formatProp(key: string, value: unknown): any[]
138141
function formatProp(key: string, value: unknown, raw: true): any
142+
/* istanbul ignore next */
139143
function formatProp(key: string, value: unknown, raw?: boolean): any {
140144
if (isString(value)) {
141145
value = JSON.stringify(value)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { render, h, nodeOps } from '@vue/runtime-test'
2+
import { useCssModule } from '../../src/helpers/useCssModule'
3+
import { mockWarn } from '@vue/shared'
4+
5+
describe('useCssModule', () => {
6+
mockWarn()
7+
8+
function mountWithModule(modules: any, name?: string) {
9+
let res
10+
render(
11+
h({
12+
render() {},
13+
__cssModules: modules,
14+
setup() {
15+
res = useCssModule(name)
16+
}
17+
}),
18+
nodeOps.createElement('div')
19+
)
20+
return res
21+
}
22+
23+
test('basic usage', () => {
24+
const modules = {
25+
$style: {
26+
red: 'red'
27+
}
28+
}
29+
expect(mountWithModule(modules)).toMatchObject(modules.$style)
30+
})
31+
32+
test('basic usage', () => {
33+
const modules = {
34+
foo: {
35+
red: 'red'
36+
}
37+
}
38+
expect(mountWithModule(modules, 'foo')).toMatchObject(modules.foo)
39+
})
40+
41+
test('warn out of setup usage', () => {
42+
useCssModule()
43+
expect('must be called inside setup').toHaveBeenWarned()
44+
})
45+
46+
test('warn missing injection', () => {
47+
mountWithModule(undefined)
48+
expect('instance does not have CSS modules').toHaveBeenWarned()
49+
})
50+
51+
test('warn missing injection', () => {
52+
mountWithModule({ $style: { red: 'red' } }, 'foo')
53+
expect('instance does not have CSS module named "foo"').toHaveBeenWarned()
54+
})
55+
})

packages/runtime-dom/src/helpers/useCssModule.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { warn, getCurrentInstance } from '@vue/runtime-core'
22
import { EMPTY_OBJ } from '@vue/shared'
33

44
export function useCssModule(name = '$style'): Record<string, string> {
5+
/* istanbul ignore else */
56
if (!__GLOBAL__) {
67
const instance = getCurrentInstance()!
78
if (!instance) {
8-
__DEV__ && warn(`useCSSModule must be called inside setup()`)
9+
__DEV__ && warn(`useCssModule must be called inside setup()`)
910
return EMPTY_OBJ
1011
}
1112
const modules = instance.type.__cssModules
@@ -22,7 +23,7 @@ export function useCssModule(name = '$style'): Record<string, string> {
2223
return mod as Record<string, string>
2324
} else {
2425
if (__DEV__) {
25-
warn(`useCSSModule() is not supported in the global build.`)
26+
warn(`useCssModule() is not supported in the global build.`)
2627
}
2728
return EMPTY_OBJ
2829
}

packages/runtime-dom/src/helpers/useCssVars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function useCssVars(
1414
scoped = false
1515
) {
1616
const instance = getCurrentInstance()
17+
/* istanbul ignore next */
1718
if (!instance) {
1819
__DEV__ &&
1920
warn(`useCssVars is called without current active component instance.`)

0 commit comments

Comments
 (0)