I've been writing lwcs since they came out, but never wrote unit tests for them. Just getting my feet wet here :)
I have an lwc with three datatables. The first one has row actions that process the row and then removes if from the first datatable rows (data array) and adds it to one of the others.
The code is working - I see it when I manually test on the page, and I also put console.log lines in the lwc js controller to debug the number of rows before and after.
However, when I test it, the numbers don't match.
LWC js file:
removeProcessedItem(iliRow){
console.log('before remove ili row, the invoiceLineItem length: ', this.invoiceLineItems.length);
this.invoiceLineItems.forEach(inv => {console.log('the ili id: ', inv.Id, ' matches iliRow.Id: ', iliRow.Id, ': ', (inv.Id !== iliRow.Id))});
this.invoiceLineItems = this.invoiceLineItems.filter(invLI => invLI.Id !== iliRow.Id);
console.log('after remove ili row, the ili length: ', this.invoiceLineItems.length);
}
Test js:
const detailEls = element.shadowRoot.querySelectorAll('lightning-datatable');
const billedDataTable = detailEls[0];
const originalBilledRows = billedDataTable.data.length; //outputs 22
...
billedDataTable.dispatchEvent(confirmRowActionEvent); // this calls the method in the controller, the logs are printed in the terminal
return Promise.resolve().then(() => {
const detailEls2 = element.shadowRoot.querySelectorAll('lightning-datatable');
const billedRows2 = detailEls2[0].data.length;
//assert that there is one less billed
expect(billedRows2).toBe(originalBilledRows - 1);
});
Output:
Expected: 21
Received: 22
How can I force the test to recognize the new data rows?
confirmRowAction
- put another way, are you providing the necessary details that a real row action would provide in manual testing?return Promise.resolve().then(async () => {...
. Similarly, you can try using the async function used in the lwc-recipes repo which will accomplish the same thing