I have the following code.
Html template
<template>
<tr class="table__row" aria-hidden={isHidden} data-test-class="table-row">
<td>Cell</td>
</tr>
</template>
CSS
.table__row[aria-hidden="true"] {
display: none;
}
JS Controller
import { LightningElement } from 'lwc';
export default class Example extends LightningElement {
isHidden = true;
}
Jest
import { createElement } from 'lwc';
import Example from 'c/example'
// eslint-disable-next-line no-undef
const flushPromises = () => new Promise(resolve => setImmediate(resolve));
describe('starnger things', () => {
it('expects to get display:none', async () => {
const componentElement = createElement('c-example',{ is: Example });
document.body.appendChild(componentElement);
await flushPromises();
const row = componentElement.shadowRoot.querySelector('[data-test-class="table-row"]');
const styles = getComputedStyle(row);
expect(row.ariaHidden).toBe('true')
expect(row.classList.contains('table__row')).toBe(true)
// next line fails with 'Received: "table-row"'
expect(styles.display).toBe('none');
});
});
I want to test the style change with Jest, but getComputedStyle
for the table row returns display: table-row
. Am I missing something? Or is it just impossible?
Update
I updated the code to make sure it's working and the issue is easily reproducible with the provided code.
I want to test specifically the display: none
and not how it is achieved in order to segregate the behavior and the implementation.