-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from cexbrayat/feat/get-component
feat: getComponent
- Loading branch information
Showing
4 changed files
with
92 additions
and
3 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
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,39 @@ | ||
import { expectType } from 'tsd' | ||
import { defineComponent, ComponentPublicInstance } from 'vue' | ||
import { mount } from '../src' | ||
|
||
const ComponentToFind = defineComponent({ | ||
props: { | ||
a: { | ||
type: String, | ||
required: true | ||
} | ||
}, | ||
template: '' | ||
}) | ||
|
||
const AppWithDefine = defineComponent({ | ||
template: '' | ||
}) | ||
|
||
const wrapper = mount(AppWithDefine) | ||
|
||
// get by type | ||
const componentByType = wrapper.getComponent(ComponentToFind) | ||
// returns a wrapper with properly typed vm | ||
expectType<string>(componentByType.vm.a) | ||
|
||
// get by name | ||
const componentByName = wrapper.getComponent({ name: 'ComponentToFind' }) | ||
// returns a wrapper with a generic vm (any) | ||
expectType<ComponentPublicInstance>(componentByName.vm) | ||
|
||
// get by string | ||
const componentByString = wrapper.getComponent('other') | ||
// returns a wrapper with a generic vm (any) | ||
expectType<ComponentPublicInstance>(componentByString.vm) | ||
|
||
// get by ref | ||
const componentByRef = wrapper.getComponent({ ref: 'ref' }) | ||
// returns a wrapper with a generic vm (any) | ||
expectType<ComponentPublicInstance>(componentByRef.vm) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { defineComponent } from 'vue' | ||
import { mount } from '../src' | ||
import { VueWrapper } from '../src/vue-wrapper' | ||
|
||
const compA = defineComponent({ | ||
template: `<div class="A"></div>` | ||
}) | ||
|
||
describe('getComponent', () => { | ||
it('should delegate to findComponent', () => { | ||
const wrapper = mount(compA) | ||
jest.spyOn(wrapper, 'findComponent').mockReturnThis() | ||
wrapper.getComponent('.domElement') | ||
expect(wrapper.findComponent).toHaveBeenCalledWith('.domElement') | ||
}) | ||
|
||
it('should throw if not found', () => { | ||
const wrapper = mount(compA) | ||
expect(() => wrapper.getComponent('.domElement')).toThrowError( | ||
'Unable to get component with selector .domElement within: <div class="A"></div>' | ||
) | ||
}) | ||
}) |