![]() |
从容的椰子 · Advanced Usage· 6 天前 · |
![]() |
追风的便当 · Unable to install ...· 4 天前 · |
![]() |
乐观的甜瓜 · 博客來-新托業備考攻略標準試卷(英漢雙語‧附 ...· 3 天前 · |
![]() |
善良的白开水 · 元数据转换规则 | YashanDB Doc· 2 天前 · |
![]() |
强健的沙发 · ClickHouse/Doris vs ...· 昨天 · |
![]() |
叛逆的警车 · Oracle数据库拼接字符长度超过4000异 ...· 8 月前 · |
![]() |
豪情万千的花生 · spring @Scheduled ...· 1 年前 · |
![]() |
温文尔雅的蘑菇 · Running cells with ...· 1 年前 · |
![]() |
爱看球的小马驹 · python - ...· 2 年前 · |
![]() |
闷骚的冲锋衣 · Python猫的个人空间 - ...· 2 年前 · |
![]() |
威武的钥匙扣
2 年前 |
![]() |
从容的椰子 · Advanced Usage 6 天前 |
![]() |
善良的白开水 · 元数据转换规则 | YashanDB Doc 2 天前 |
![]() |
温文尔雅的蘑菇 · Running cells with ‘Python 3.6.5 (‘XXX‘)‘ requires ipykernel package._running cells with python_Reza.的博客-CSDN博客 1 年前 |
Test a Property Change
it('Renders with Hello Matt', () => { const element = createElement('c-hello-component', { is: HelloComponent, }); document.body.appendChild(element); element.person = "Matt"; return Promise.resolve().then(() => { const pTag = element.shadowRoot.querySelector('p'); expect(pTag.textContent).toEqual('Hello, Matt!'); it('Renders with showDesktopView = true', () => { const element = createElement('c-test', { is: testLWC, }); document.body.appendChild(element); element.showDesktopView = true; return Promise.resolve().then(() => { // My promise code
https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.unit_testing_using_jest_patterns
That is the same documentation I've been using. That is basically my Trial 2 approach, but document.body.appendChild(element) is before the element assignment. I tried it in that order as well, but it didn't work. The error I get back is the querySelector doesn't find the element. However, when I default the value in my code to true for showDesktopView (which I cannot leave it like that), the query selector does find the "Name" element it is looking for.
This is a closer representation of my code following what you suggested, but doens't work: it('Displays Contact Name field', () => { const element = createElement('c-test', { is: testLWC }); document.body.appendChild(element); element.showDesktopView = true; getRecordWireAdapter.emit(mockGetRecord); // Resolve a promise to wait for a rerender of the new content. return Promise.resolve().then(() => { const content = element.shadowRoot.querySelector('.myclass'); const nameField = mockGetRecord.fields.Name.value; console.log('Expected: ' + nameField) console.log('Found: ' + content.textContent); expect(content.textContent).toBe(nameField);
The error I get back is "TypeError: Cannot read property 'textContent' of null" which is recieved when the querySelector doesn't find the rendered element. Again, the test case does pass if I default the showDesktopView to true.
const content = element.shadowRoot.querySelector ('.myclass') ;
element.shadowRoot is not the common this.shadowRoot as you can find in the mozilla documentation but it is equivalent to this.template for LWC.
You can try in your code (not the jest test) if const content = this.template.querySelector('.myclass'); is resolved.
In my jest test, the element.shadowroot.querySelector does work if I default showDesktopView to true. The test case passes, it finds the name value.
I also found this in my jest test, if I use element.querySelector when default showDesktopView is true, the case fails.