Unclear reason for difference between testing components with properties VS attributes #2117
-
Let's say I have the following web-component that I want to test. // wc-a.js
class ComponentA extends LitElement {
@property({type: Boolean}) readonly: boolean;
render() {
return html `<div>
<span>I am a span</span>
<wc-button .type=${this.readonly ? 'disabled' : 'big' }/>
</div>`
}
} The My test looks as follows: import { html, fixture, expect } from '@open-wc/testing'
import { ComponentA } from './ComponentA'
describe('ComponentA', () => {
describe('given the component is in readonly mode', () => {
new ComponentA()
it('disables the button', async () => {
const wcA: ComponentA = await fixture(
html`<wc-a .readonly=${true}></wc-a>`,
)
const wcButton = wcA.shadowRoot.querySelector('wc-button')
expect(wcButton).to.have.property('type').equal('disabled')
// the propery type is "normal" (because the wc-button component's
// "type" property has a default value of "normal". => never got changed
})
})
}) However if I change the I tried adding |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Wow, as soon as I posted this here, I rubber ducked it myself. Apparantly if you give a static value like |
Beta Was this translation helpful? Give feedback.
-
there's a few things to be aware of here:
html`
<wc-a ?readonly="${bool}"></wc-a>
`; The
@property({ type: Boolean, reflect: true }) disabled = false; the @property({ type: String, attribute: 'camel-case' }) camelCase |
Beta Was this translation helpful? Give feedback.
there's a few things to be aware of here:
<wc-a readonly="${true}">
will evaluate to<wc-a readonly="true">
and<wc-a readonly="${false}">
will evaluate to<wc-a readonly="false">
this is likely not what you want. Probably you want the attribute to be present when true and absent when false. For that, use this template syntax:The
?
prefix on the attribute name signals that its a boolean attribute.the
reflect
flag makes lit-element reflect property assignments…