0

The documentation about unit testing for the wire service in LWC https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.unit_testing_using_wire_utility suggests to collect testing data using the UI:

Grab a snapshot of data using a REST client that accesses the UI API. This approach is more accurate than writing JSON by hand. Then, save the JSON in the file in data folder.

While this approach certainly works for getRecord, it is not possible to use the UI API to invoke a custom Apex controllers, nor it seem possible to do it via the Workbench.

Are there alternative solutions to generate a valid json response from a custom apex controller?

2

lightning/ui*Api Wire Adapters and Functions are built on top of Lightning Data Service (LDS) and User Interface API. If you use lightning/ui*Api in your LWC, only then you would use registerLdsTestWireAdapter(wireAdapter) (from @salesforce/sfdx-lwc-jest) in the JEST test. And, only in this case, you will need to use UI API to get the snapshot of wire service mock data. The link you have referred in the question provides details about the same.

In the LWC wire service test, mocking wire service data can be done via 3 adapters given below:

  • Generic wire adapter: The generic adapter emits data on demand when you call the emit() API. It does not include any extra information about the data itself.
  • Lightning Data Service (LDS) wire adapter: The LDS adapter mimics Lightning Data Service behavior and includes information about the data’s properties.
  • Apex wire adapter: The Apex wire adapter mimics calls to an Apex method and includes any error status.

For mocking custom apex controller (wire service) data, you need to use the Apex wire adapter. Since the custom apex method code was written by you (or a team), you will know that return data type/format and hence, you can draft the mock data accordingly. Refer to the Using the Apex Wire Adapter section of this trailhead module for complete details and code sample.

For example, if you have a wired property (say, contacts) in the LWC, then the apex result is provided inside contacts.data property. Or, if have a wired function, then you would get the apex result inside result.data (or result) and exceptions in the error param. So, the data format inside these wired function param or wired property depends on what the apex method returns. Easiest way to figure out the exact format is by debugging the LWC code in chrome dev console (or just by doing a console.log() within the connectedCallback).

2
  • Thanks, I understand that: my question is how to I generate a valid (in terms of structure) json response for both error case and success. I imagine the apex controller place a wrapper around the objects I am returning from it? Nov 7 '20 at 17:16
  • 1
    Yes, that's correct. If you have a wired property (for example, contacts) in the LWC, then the apex result is provided inside contacts.data property. Or, if have a wired function, then you would get the apex result inside result.data (or result) and exceptions in the error param. So, the data format inside these wired function param or wired property depends on what the apex method returns. You can figure out the exact format by debugging the LWC code in chrome dev console (or just by doing a console.log()).
    – arut
    Nov 7 '20 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.