Skip to content

Fix: sync element's properties when async component is resolved #65

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ export default function wrap (Vue, Component) {
}
initialize(resolved)
syncInitialAttributes()
// sync existing element properties with props
camelizedPropsList.forEach(prop => {
if (typeof this[prop] !== 'undefined') {
wrapper.props[prop] = this[prop]
}
})
})
}
// initialize children
Expand Down
9 changes: 9 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"jest": true
},
"globals": {
"el": true,
"els": true
}
}
7 changes: 6 additions & 1 deletion test/fixtures/async.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const Component = () => new Promise(resolve => {
setTimeout(() => {
resolve({
template: `<div>{{ foo }} {{ someProp }}</div>`,
template: `<div>{{ foo }} {{ someProp }} {{ extraProp.value }}</div>`,
props: {
foo: {
type: Number,
Expand All @@ -14,6 +14,10 @@
'some-prop': {
type: String,
default: 'bar'
},
extraProp: {
type: Object,
default: () => ({value: 'banana'})
}
}
})
Expand All @@ -26,6 +30,7 @@
customElements.define('my-element', wrap(Vue, Component))

window.els = document.querySelectorAll('my-element')
window.els[1].extraProp = {value: 'apple'}
</script>

<my-element></my-element>
Expand Down
7 changes: 5 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ test('properties', async () => {
el.foo = 234
el.someProp = 'lol'
})
const newFoo = await page.evaluate(() => el.vueComponent.foo)
const newFoo = await page.evaluate(() => el.vueComponent.foo)
expect(newFoo).toBe(234)
const newBar = await page.evaluate(() => el.vueComponent.someProp)
const newBar = await page.evaluate(() => el.vueComponent.someProp)
expect(newBar).toBe('lol')
})

Expand Down Expand Up @@ -114,6 +114,9 @@ test('async', async () => {
expect(await page.evaluate(() => els[0].shadowRoot.textContent)).toMatch(`123 bar`)
expect(await page.evaluate(() => els[1].shadowRoot.textContent)).toMatch(`234 baz`)

// props assigned as node properties should be synced
expect(await page.evaluate(() => els[1].shadowRoot.textContent)).toMatch(`apple`)

// attribute sync should work
await page.evaluate(() => {
els[0].setAttribute('foo', '345')
Expand Down