test: improve coverage
This commit is contained in:
55
packages/runtime-dom/__tests__/helpers/useCssModule.spec.ts
Normal file
55
packages/runtime-dom/__tests__/helpers/useCssModule.spec.ts
Normal file
@@ -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()
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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.`)
|
||||
|
||||
Reference in New Issue
Block a user