How to write a jest test on conditional rendering?
so I have a page in that page few input and combobox and when the combobox selected to a particular type which is accepted
then I display the input field called amount
.
my question how would you write a jest test to a conditional rendering, please see for detail below.
<lightning-combobox
name="pType"
value={pType}
options={options}
onchange={handleChange}>
</lightning-combobox>
<lightning-input ....</lightning-input>
<lightning-input ....</lightning-input>
<lightning-input ....</lightning-input>
<div if:true={isSelected} >
<lightning-input type="number" onchange={handleChange} label="amount" name="amount"></lightning-input>
</div>
.JS:
isSelected = false; //hide amount field by default
handleChange(event) {
const {name} = event.target
const {value} = event.detail;
......
if (name === 'pType') {
if (value) {
this.isSelected = value.toLowerCase() === 'accepted'
}
}
......
}
JEST test class:
import { createElement } from 'lwc';
import MyConditionalRendering from 'c/myConditionalRendering';
describe('c-my-conditional-rendering suite', ()=>{
beforeEach(() => {
const element = createElement('c-my-conditional-rendering', {
is: MyConditionalRendering
})
document.body.appendChild(element)
})
it("show amount input field when combobox is accepted", ()=>{
const element = document.querySelector('c-my-conditional-rendering')
const inputElement = element.shadowRoot.querySelector('lightning-input');
//lets select the type from the combobox:
const comboBox = element.shadowRoot.querySelector("lightning-combobox");
comboBox.dispatchEvent(new CustomEvent("change", {detail: {value: 'accepted'}}));
//not sure how should I grab the input amount text field from here.
})
})