I am trying to read simple users list with wire method. However, I'm unable to test the variable value in jest promise, is there anything wrong with my code/understanding?
LWC -
@wire(getUsers)
users;
Jest -
import getUsers from "@salesforce/apex/UserUtil.getUsers";
import { registerLdsTestWireAdapter } from "@salesforce/sfdx-lwc-jest";
const mockGetRecord = require("./data/users.json");
const getRecordWireAdapter = registerLdsTestWireAdapter(getUsers);
describe("c-data-table-dragable", () => {
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();
}
});
it("Check if data is loaded or not", () => {
const element = createElement("c-data-table-dragable", {
is: DataTableDragable
});
document.body.appendChild(element);
getRecordWireAdapter.emit(mockGetRecord);
return Promise.resolve().then(() => {
expect(element).toBeAccessible();
//expect(element.users.data).toBe(mockGetRecord) //--giving error : cannot read property data of undefined.
//expect(element.users.error).toBeUndefined()//--giving error : cannot read proper error of undefined.
//however, the below code is working fine means it's rendering on HTML part.
const trEls = element.shadowRoot.querySelectorAll('tr');
expect(trEls.length).toBeGreaterThan(mockGetRecord.length);
});
});
mock file -
[{
"Id": "0050U000004YzSVQA0",
"Username": "autoproc@00d0u000000gvvruac",
"Email": "autoproc@00d0u000000gvvruac",
"index": 0
}, {
"Id": "0050U000004YzSaQAK",
"Username": "automatedclean@00d0u000000gvvruac",
"Email": "automatedclean@00d0u000000gvvruac",
"index": 1
}, {
"Id": "0050U000004YzSPQA0",
"Username": "[email protected]",
"Email": "[email protected]",
"index": 2
}, {
"Id": "0050U000004YzSUQA0",
"Username": "[email protected]",
"Email": "[email protected]",
"index": 3
}, {
"Id": "0050U000004YzSYQA0",
"Username": "[email protected]",
"Email": "[email protected]",
"index": 4
}, {
"Id": "0050U000004YzSZQA0",
"Username": "[email protected]",
"Email": "[email protected]",
"index": 5
}]