You need to sign in to do that
Don't have an account?

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.
I do know that the datatable with data is in the dom based on the earlier expects.
Would appreciate any help. Thanks.
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.
All Answers