I believe it is a simple fix, but I am new to everything and Jest being the latest one I am exposed to.
I am trying to test if the lightning-combobox received the data to fill its options after the apex imperative call.
I still have no clue what I am doing, I have been copying code from orgs other components The error I receive is on the data being returned, I expect two records to be returned from the mock data, but it received undefined.
Here is the mock data -
[
{
"value": "00T2h00000BC9naEAD",
"label": "Test Task 2 | 2021-08-06 10:00 AM | owner name"
},
{
"value": "00T2h00000BC9naTAS",
"label": "Test Task 2 | 2021-08-06 10:05 AM | owner name"
}
]
Here is component HTML
<lightning-combobox class="tasksList" label="Select Task"
options={tasks}
value={defaultTask}
onchange={handleTaskSelection} required>
</lightning-combobox>
Here is the JS -
connectedCallback() {
getTasksAssociatedToContact({
contactRecordId: this.contactRecordId
}).then(data => {
if (data) {
this.tasks = data.map(item => {
return {
value: item.Id,
label: item.Subject + ' | ' + item.CreatedDate + ' | ' + item.Owner.Name,
}
})
}
})
}
Here is the LWC Jest Test, I am attaching the whole Jest code as I dont know what helps you all to help me -
import { createElement } from 'lwc';
import { registerApexTestWireAdapter } from '@salesforce/sfdx-lwc-jest';
import getTasksAssociatedToContact from "@salesforce/apex/PopulateTasksForContactController.getTasksAssociatedToContact";
import PopulateTasksForContact from 'c/populateTasksForContact';
jest.mock('lightning/flowSupport', () => {
return 'FlowNavigation';
},
{ virtual: true }
);
const mockData = require('./data/tasksData.json');
const noMockData = require('./data/notasksData.json');
const getTasksAssociatedToContactAdapter = registerApexTestWireAdapter(getTasksAssociatedToContact);
describe('c-populate-tasks-for-contact', () => {
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
// Prevent data saved on mocks from leaking between tests
jest.clearAllMocks();
});
describe('getTasksForContact Apex @imperative call', () => {
it('renders tasks to combobox if data returned from imperative apex call', () => {
const element = createElement('c-populate-tasks-for-combobox', {
is: PopulateTasksForContact
});
element.contactRecordId = '0032h00000YxNzIAAV';
document.body.appendChild(element);
getTasksAssociatedToContactAdapter.emit(mockData);
return Promise.resolve().then(() => {
let combobox = element.shadowRoot.querySelector('.tasksList');
expect(combobox.length).toBe(2);
})
})
})
});
Please advise.