2

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
}]
2
  • Please add the detail of your mockGetRecord.
    – Phil W
    Nov 20, 2020 at 8:04
  • @PhilW updated the mock data and its importing details.
    – Ysr Shk
    Nov 20, 2020 at 9:58

1 Answer 1

2

The error you are getting is because you can access only @api properties in the jest test context.

Everything that is marked @track or the @wire is going to be not defined in the test context.

You should just test the DOM elements that have rendered fine with the proper number of elements instead.

2
  • Hi Mohit, I tried giving track decorator to another variable(which is a map) and try to read in the jest, still it's giving undefined for that. can you point me to any recipe which is using that so that I can try that?
    – Ysr Shk
    Nov 22, 2020 at 6:03
  • 1
    You need @api to access them! Track and wire won't be accessible! You query the dom elements for the same! Nov 22, 2020 at 6:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .