Sub topic 5
How @wire delivers data to a component
This chapter explains how a wired property or function receives data. You need this model before you can replace the data source in a test. It assumes you know basic LWC (templates, decorators, lifecycle hooks), have used @wire and imported Apex methods, and can read Jest tests and async/await.
What a wire adapter is
A wire adapter is a function-like module that supplies data to a component. The component does not call it directly. Instead, the component declares what it wants with the @wire decorator, and the LWC engine connects the declaration to the adapter.
import { LightningElement, wire } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getContactList';
export default class ContactList extends LightningElement {
@wire(getContactList)
contacts;
}Here getContactList is the adapter. The imported Apex method is itself a wire adapter, so the same mechanism covers built-in adapters such as getRecord and Apex methods.
The delivery cycle
The engine controls when the adapter runs and when data arrives. The component only reads the result.
- The component is inserted into the DOM.
- The engine connects the adapter and passes it the configuration, such as
{ recordId: '001...' }. - The adapter provisions data, usually from the server or a cache.
- The adapter emits a value to the engine.
- The engine writes the value to the wired property or calls the wired function.
- The template re-renders because a tracked value changed.
Steps 3 and 4 are the only parts that need a server. In a Jest test there is no server, so a test replaces the adapter and plays the part of step 4 itself.
The shape of the emitted value
A wired property receives an object with two fields, data and error. Only one has a value at a time.
| State | `data` | `error` |
|---|---|---|
| Before the first emit | `undefined` | `undefined` |
| Success | the result | `undefined` |
| Failure | `undefined` | an error object |
Before the first emit, both fields are undefined. Code that treats "no data yet" as "no results" shows the wrong message while loading. A template should handle all three states.
import { LightningElement, wire } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getContactList';
export default class ContactList extends LightningElement {
contacts;
error;
@wire(getContactList)
wiredContacts({ data, error }) {
if (data) {
this.contacts = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.contacts = undefined;
}
}
}A wired function receives that same object as its argument. Using a function instead of a property gives you a place to react to each emit, which also makes the behavior easy to assert on in a test.
Reactive configuration
An adapter can take a configuration object. Prefix a component property with $ to make it reactive.
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class RecordName extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name'] })
record;
}When recordId changes, the engine sends the adapter an updated configuration. The adapter then emits again. If recordId is undefined, the adapter does not provision data.
Why this matters for testing
Three facts from this model drive the test techniques in the next chapters.
- The component never calls the adapter, so a test cannot simply stub a function the component calls. It must replace the adapter that the engine uses.
- Delivery happens after the component connects. A test must attach the component to the DOM before it emits data.
- The template updates after the emit, not during it. A test must wait for rendering before it checks the DOM.
Exercise
A component has @wire(getContactList) contacts;. Which values do contacts.data and contacts.error hold right after the component is attached to the DOM, before any data is emitted, and why does this matter for the template?
Show answer
Both are undefined. The template cannot tell "still loading" from "no results" by looking only at data, so it must handle the loading state separately, for example by checking that either data or error is set before it shows an empty-state message.
Exercise
Which step of the delivery cycle does a Jest test take over, and what must the test do before it performs that step?
Show answer
The test takes over the adapter's emit step. Before it emits, the test must attach the component to the DOM so the engine connects the adapter.