File tree Expand file tree Collapse file tree 3 files changed +60
-5
lines changed
runtime-vapor/__tests__/helpers Expand file tree Collapse file tree 3 files changed +60
-5
lines changed Original file line number Diff line number Diff line change @@ -92,7 +92,7 @@ export const setCurrentInstance = (
9292 }
9393}
9494
95- const internalOptions = [ 'ce' ] as const
95+ const internalOptions = [ 'ce' , 'type' ] as const
9696
9797/**
9898 * @internal
Original file line number Diff line number Diff line change 1- import { getCurrentInstance , warn } from '@vue/runtime-core'
1+ import { useInstanceOption , warn } from '@vue/runtime-core'
22import { EMPTY_OBJ } from '@vue/shared'
33
44export function useCssModule ( name = '$style' ) : Record < string , string > {
55 if ( ! __GLOBAL__ ) {
6- const instance = getCurrentInstance ( ) !
7- if ( ! instance ) {
6+ const { hasInstance , value : type } = useInstanceOption ( 'type' , true )
7+ if ( ! hasInstance ) {
88 __DEV__ && warn ( `useCssModule must be called inside setup()` )
99 return EMPTY_OBJ
1010 }
11- const modules = instance . type . __cssModules
11+ const modules = type ! . __cssModules
1212 if ( ! modules ) {
1313 __DEV__ && warn ( `Current instance does not have CSS modules injected.` )
1414 return EMPTY_OBJ
Original file line number Diff line number Diff line change 1+ import { useCssModule } from '@vue/runtime-dom'
2+ import { makeRender } from '../_utils'
3+ import { defineVaporComponent , template } from '@vue/runtime-vapor'
4+
5+ const define = makeRender < any > ( )
6+
7+ describe ( 'useCssModule' , ( ) => {
8+ function mountWithModule ( modules : any , name ?: string ) {
9+ let res
10+ define (
11+ defineVaporComponent ( {
12+ __cssModules : modules ,
13+ setup ( ) {
14+ res = useCssModule ( name )
15+ const n0 = template ( '<div></div>' ) ( )
16+ return n0
17+ } ,
18+ } ) ,
19+ ) . render ( )
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+ } )
You can’t perform that action at this time.
0 commit comments