-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime-vapor): template ref on component (#185)
- Loading branch information
Showing
4 changed files
with
149 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ref, shallowRef } from '@vue/reactivity' | ||
import { createComponent } from '../src/apiCreateComponent' | ||
import { setRef } from '../src/dom/templateRef' | ||
import { makeRender } from './_utils' | ||
import { | ||
type ComponentInternalInstance, | ||
getCurrentInstance, | ||
} from '../src/component' | ||
|
||
const define = makeRender() | ||
describe('api: expose', () => { | ||
test('via setup context', () => { | ||
const { component: Child } = define({ | ||
setup(_, { expose }) { | ||
expose({ | ||
foo: 1, | ||
bar: ref(2), | ||
}) | ||
return { | ||
bar: ref(3), | ||
baz: ref(4), | ||
} | ||
}, | ||
}) | ||
const childRef = ref() | ||
const { render } = define({ | ||
render: () => { | ||
const n0 = createComponent(Child) | ||
setRef(n0, childRef) | ||
return n0 | ||
}, | ||
}) | ||
|
||
render() | ||
expect(childRef.value).toBeTruthy() | ||
expect(childRef.value.foo).toBe(1) | ||
expect(childRef.value.bar).toBe(2) | ||
expect(childRef.value.baz).toBeUndefined() | ||
}) | ||
|
||
test('via setup context (expose empty)', () => { | ||
let childInstance: ComponentInternalInstance | null = null | ||
const { component: Child } = define({ | ||
setup(_) { | ||
childInstance = getCurrentInstance() | ||
}, | ||
}) | ||
const childRef = shallowRef() | ||
const { render } = define({ | ||
render: () => { | ||
const n0 = createComponent(Child) | ||
setRef(n0, childRef) | ||
return n0 | ||
}, | ||
}) | ||
|
||
render() | ||
expect(childInstance!.exposed).toBeUndefined() | ||
expect(childRef.value).toBe(childInstance!) | ||
}) | ||
|
||
test('warning for ref', () => { | ||
const { render } = define({ | ||
setup(_, { expose }) { | ||
expose(ref(1)) | ||
}, | ||
}) | ||
render() | ||
expect( | ||
'expose() should be passed a plain object, received ref', | ||
).toHaveBeenWarned() | ||
}) | ||
|
||
test('warning for array', () => { | ||
const { render } = define({ | ||
setup(_, { expose }) { | ||
expose(['focus']) | ||
}, | ||
}) | ||
render() | ||
expect( | ||
'expose() should be passed a plain object, received array', | ||
).toHaveBeenWarned() | ||
}) | ||
|
||
test('warning for function', () => { | ||
const { render } = define({ | ||
setup(_, { expose }) { | ||
expose(() => null) | ||
}, | ||
}) | ||
render() | ||
expect( | ||
'expose() should be passed a plain object, received function', | ||
).toHaveBeenWarned() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ref, setRef, template } from '../../src' | ||
import { makeRender } from '../_utils' | ||
|
||
const define = makeRender() | ||
|
||
describe('api: template ref', () => { | ||
test('string ref mount', () => { | ||
const t0 = template('<div ref="refKey"></div>') | ||
const el = ref(null) | ||
const { render } = define({ | ||
setup() { | ||
return { | ||
refKey: el, | ||
} | ||
}, | ||
render() { | ||
const n0 = t0() | ||
setRef(n0 as Element, 'refKey') | ||
return n0 | ||
}, | ||
}) | ||
|
||
const { host } = render() | ||
expect(el.value).toBe(host.children[0]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters