function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PugetSoundJimPugetSoundJim 

How do you selectrows of a lightning-datatable in a jest test?

So I have created a LWC that has multiple lighting-inputs, lightning-comboboxes and a lightning-datatable, where the selected rows combined with the other fields are utilized to bulk create leads, events and tasks.  The LWC works fine.  Unfortunately the jest test is not so well.  To get to the point of actually mocking the Apex insert, I need to figure out how to get passed the code in the LWC where it is using the getSelectedRows() function.

Here is my test code so far.  I can't figure out how to select a row or if I should be mocking it.  This is my attempt at mocking.  
 
describe('Save/Close', () => {
        it('Should be able to save leads.', done => {
            getObjectInfo.emit(getLeadObjectInfo);
            getPicklistValues.emit(leadStatusPicklistValues);
            getPicklistValues.emit(leadSourcePicklistValues);
            getProducts.mockResolvedValue(testProductRecords);
            document.body.appendChild(element);

            flushPromises().then(() => {
                const dataTable = element.shadowRoot.querySelector('lightning-datatable');
                expect(dataTable.data).not.toBeNull();
                const tableRows = dataTable.data;
                expect(tableRows.length).toBe(2);
                console.log(JSON.stringify(tableRows));

                simulateUserSelect(element, 'salutation', 'Mr.');
                simulateUserInput(element, 'firstName', 'John');
                simulateUserInput(element, 'middleName', 'B');
                simulateUserInput(element, 'lastName', 'Doe');
                simulateUserInput(element, 'email', 'doe@example.com');
                simulateUserSelect(element, 'leadStatus', 'New');
                simulateUserSelect(element, 'leadSource', 'Referral');
                simulateUserInput(element, 'budget', '100');

                const rowSelectionHandler = jest.fn();
                element.addEventListener('rowSelect', rowSelectionHandler);
                dataTable.getSelectedRows = jest.fn().mockImplementation(() => [testProductRecords[0]]);
                dataTable.dispatchEvent(new CustomEvent('rowselection'));
                expect(rowSelectionHandler).toHaveBeenCalled();

                flushPromises().then(() => {
                    const saveButton = element.shadowRoot.querySelector('lightning-button[data-element-id="save"]');
                    saveButton.click();

                    flushPromises().then(() => {
                        const calculatedInputs = element.shadowRoot.querySelectorAll('lightning-input');
                        expect(calculatedInputs.length).toBe(0);
                        done();
                    });
                });
            });
        });
expect(rowSelectionHandler).toHaveBeenCalled();  fails.

I do know that the datatable with data is in the dom based on the earlier expects.  

Would appreciate any help.  Thanks.

 
Best Answer chosen by PugetSoundJim
PugetSoundJimPugetSoundJim
For anyone curious.  This was a result of assigning the listener to the element and not the dataTable.
 
const rowSelectionHandler = jest.fn();
element.addEventListener('rowSelect', rowSelectionHandler);  ===>>  dataTable.addEventListener('rowSelect', rowSelectionHandler); 
dataTable.getSelectedRows = jest.fn().mockImplementation(() => [testProductRecords[0]]);
dataTable.dispatchEvent(new CustomEvent('rowselection'));
expect(rowSelectionHandler).toHaveBeenCalled();

 

All Answers

PugetSoundJimPugetSoundJim
For anyone curious.  This was a result of assigning the listener to the element and not the dataTable.
 
const rowSelectionHandler = jest.fn();
element.addEventListener('rowSelect', rowSelectionHandler);  ===>>  dataTable.addEventListener('rowSelect', rowSelectionHandler); 
dataTable.getSelectedRows = jest.fn().mockImplementation(() => [testProductRecords[0]]);
dataTable.dispatchEvent(new CustomEvent('rowselection'));
expect(rowSelectionHandler).toHaveBeenCalled();

 
This was selected as the best answer
SwethaSwetha (Salesforce Developers) 
@PugetSoundJim, Thanks for sharing the fix that worked for you.