Since the update to API 56.0 (Winter '23) Salesforce introduced a new set of components to help developers create modals with the Lightning Experience format in an easier way. The components added were the lightning-modal
and its three children (header, body and footer).
A component that implements this new modal mode extends the LightningModal
class and it implements the three sub-components on its body.
My problem is that I have content within the lightning-modal-body
tags in my custom component, but those are not visible on a Jest test, apparently.
The component looks like this:
<template>
<lightning-modal-header label={header}></lightning-modal-header>
<lightning-modal-body>
<template if:true={items}>
<!-- sldsValidatorIgnoreNextLine -->
<lightning-tree-grid key-field="id" columns={columns} data={lineItems}>
</lightning-tree-grid>
</template>
</lightning-modal-body>
<lightning-modal-footer>
<lightning-button
label={labels.Save}
onclick={handleOk}
data-id="save"
variant="brand"
class="slds-var-m-around_xxx-small"
><label>{labels.Save}</label></lightning-button
>
</lightning-modal-footer>
</template>
On the test, if I assert that the modal body exists, it works:
const modalBody = element.shadowRoot.querySelector("lightning-modal-body");
expect(modalBody).toBeTruthy();
But if I assert that the lightning-tree-grid
exists within the body, it does not:
const treeGridComponent = element.shadowRoot.querySelector(
"lightning-tree-grid"
);
expect(treeGridComponent).not.toBe(null);
expect(received).not.toBe(expected) // Object.is equality
Expected: not null
63 | "lightning-tree-grid"
64 | );
> 65 | expect(treeGridComponent).not.toBe(null);
| ^
66 | });
67 | });
68 |
So how does a test in this scenario should look like?