Sub topic 3
How client-side caching affects wired methods
Assume you know basic JavaScript modules, LWC properties, Apex, and SOQL. This chapter focuses on choosing and testing data access after a method is wired.
How client-side caching affects wired methods
An Apex method used with @wire must use @AuraEnabled(cacheable=true). Salesforce can return a cached result instead of making a server request. This improves response time, but the cached value can become stale after a record changes.
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(Id accountId) {
return [
SELECT Id, Name, Email
FROM Contact
WHERE AccountId = :accountId
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}The cache duration is controlled by Salesforce. Do not build logic that depends on a fixed duration.
Refreshing wired data with refreshApex
Store the complete value supplied to the wired function. Pass that value to refreshApex after a change.
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactList extends LightningElement {
@api recordId;
wiredResult;
rows = [];
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
this.wiredResult = result;
if (result.data) this.rows = result.data;
}
async handleRefresh() {
await refreshApex(this.wiredResult);
}
}When to call Apex imperatively instead
Use an imperative call when a user action controls when data loads, when the operation changes data, or when the method needs custom sequencing.
Enforcing sharing and field-level security
Use with sharing to respect record-sharing rules. Use WITH USER_MODE in SOQL to enforce object and field permissions. Also validate inputs and avoid returning fields the component does not need.
Choosing a data-access method
| Method | Best use |
|---|---|
| `@wire` | Reactive, read-only data |
| Imperative Apex | User-controlled calls and mutations |
| Lightning Data Service | Standard record loading, editing, and cache sharing |
Unit testing wired components with Jest
Mock the Apex module and emit data or errors through the wire adapter. Test both rendering paths and verify that the component displays the expected result.
import { createElement } from 'lwc';
import ContactList from 'c/contactList';
import getContacts from '@salesforce/apex/ContactController/getContacts';
import { registerApexTestWireAdapter } from '@salesforce/sfdx-lwc-jest';
const adapter = registerApexTestWireAdapter(getContacts);
it('renders contacts', async () => {
const element = createElement('c-contact-list', { is: ContactList });
element.recordId = '001000000000001AAA';
document.body.appendChild(element);
adapter.emit([{ Id: '0031', Name: 'Ada Lovelace' }]);
await Promise.resolve();
expect(element.shadowRoot.textContent).toContain('Ada Lovelace');
});Exercise
Why must a component keep the complete wired result for refreshApex?
Show answer
refreshApex needs the value provisioned by the wire service, not only the data array.