-
Notifications
You must be signed in to change notification settings - Fork 275
setProps #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
setProps #51
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc9f13f
wip: setProps
lmiller1990 ca24da7
wip: return nextTick from setProps
lmiller1990 26e6a1e
tests: add tests for setProps and watcher
lmiller1990 1070598
tests: test setProps with composition API
lmiller1990 59ba17f
feat: setProps
lmiller1990 349ff16
refactor: add VTU ref back
lmiller1990 4c21157
Add test for non-exitent props
afontcu b8209a4
chore: resolve conflicts
lmiller1990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,121 @@ | ||
| import { defineComponent, h, computed } from 'vue' | ||
|
|
||
| import { mount } from '../src' | ||
|
|
||
| describe('setProps', () => { | ||
| it('updates a primitive prop', async () => { | ||
| const Foo = { | ||
| props: ['foo'], | ||
| template: '<div>{{ foo }}</div>' | ||
| } | ||
| const wrapper = mount(Foo, { | ||
| props: { | ||
| foo: 'foo' | ||
| } | ||
| }) | ||
| expect(wrapper.html()).toContain('foo') | ||
|
|
||
| await wrapper.setProps({ foo: 'qux' }) | ||
| expect(wrapper.html()).toContain('qux') | ||
| }) | ||
|
|
||
| it('updates a function prop', async () => { | ||
| const Foo = { | ||
| props: ['obj'], | ||
| template: ` | ||
| <div> | ||
| <div v-if="obj.foo()">foo</div> | ||
| </div> | ||
| ` | ||
| } | ||
| const wrapper = mount(Foo, { | ||
| props: { | ||
| obj: { | ||
| foo: () => true | ||
| } | ||
| } | ||
| }) | ||
| expect(wrapper.html()).toContain('foo') | ||
|
|
||
| await wrapper.setProps({ obj: { foo: () => false } }) | ||
| expect(wrapper.html()).not.toContain('foo') | ||
| }) | ||
|
|
||
| it('sets component props, and updates DOM when props were not initially passed', async () => { | ||
| const Foo = { | ||
| props: ['foo'], | ||
| template: `<div>{{ foo }}</div>` | ||
| } | ||
| const wrapper = mount(Foo) | ||
| expect(wrapper.html()).not.toContain('foo') | ||
|
|
||
| await wrapper.setProps({ foo: 'foo' }) | ||
|
|
||
| expect(wrapper.html()).toContain('foo') | ||
| }) | ||
|
|
||
| it('triggers a watcher', async () => { | ||
| const Foo = { | ||
| props: ['foo'], | ||
| data() { | ||
| return { | ||
| bar: 'original-bar' | ||
| } | ||
| }, | ||
| watch: { | ||
| foo(val: string) { | ||
| this.bar = val | ||
| } | ||
| }, | ||
| template: `<div>{{ bar }}</div>` | ||
| } | ||
| const wrapper = mount(Foo) | ||
| expect(wrapper.html()).toContain('original-bar') | ||
|
|
||
| await wrapper.setProps({ foo: 'updated-bar' }) | ||
|
|
||
| expect(wrapper.html()).toContain('updated-bar') | ||
| }) | ||
|
|
||
| it('works with composition API', async () => { | ||
| const Foo = defineComponent({ | ||
| props: { | ||
| foo: { type: String } | ||
| }, | ||
| setup(props) { | ||
| const foobar = computed(() => `${props.foo}-bar`) | ||
| return () => | ||
| h('div', `Foo is: ${props.foo}. Foobar is: ${foobar.value}`) | ||
| } | ||
| }) | ||
| const wrapper = mount(Foo, { | ||
| props: { | ||
| foo: 'foo' | ||
| } | ||
| }) | ||
| expect(wrapper.html()).toContain('Foo is: foo. Foobar is: foo-bar') | ||
|
|
||
| await wrapper.setProps({ foo: 'qux' }) | ||
|
|
||
| expect(wrapper.html()).toContain('Foo is: qux. Foobar is: qux-bar') | ||
| }) | ||
|
|
||
| it('non-existent props are rendered as attributes', async () => { | ||
| const Foo = { | ||
| props: ['foo'], | ||
| template: '<div>{{ foo }}</div>' | ||
| } | ||
| const wrapper = mount(Foo, { | ||
| props: { | ||
| foo: 'foo' | ||
| } | ||
| }) | ||
| expect(wrapper.html()).toContain('foo') | ||
|
|
||
| const nonExistentProp = { bar: 'qux' } | ||
| await wrapper.setProps(nonExistentProp) | ||
|
|
||
| expect(wrapper.attributes()).toEqual(nonExistentProp) | ||
| expect(wrapper.html()).toBe('<div bar="qux">foo</div>') | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.