Sub topic 6
Driving a component with test wire adapters
This chapter assumes you can read a basic LWC that uses @wire, and that you know Jest's describe, it, expect, and async/await. It also assumes you know how to create a component with createElement, which an earlier chapter covered.
What a test wire adapter is
In production, a wire adapter is a function that connects a component to a data source. The platform calls it when the component connects, then sends it values as the component's reactive parameters change. In Jest there is no platform, so a wired property would never receive anything.
@salesforce/sfdx-lwc-jest fixes this by replacing the adapter with a test wire adapter. The test adapter does no work of its own. It only gives your test two methods:
emit(value)sends a value to every component instance wired to that adapter.error()sends an error to those instances.
Your test then acts as the data source and decides what the component receives and when.
The flow of a test
The diagram shows the order of events in a typical test.
The order matters. The wire connects when the component is attached to the DOM. If you call emit before appendChild, no instance is listening and the value is lost.
A worked example
Suppose c-contact-list wires an Apex method to a property.
import { LightningElement, wire } from 'lwc';
import getContactList from '@salesforce/apex/ContactController.getContactList';
export default class ContactList extends LightningElement {
@wire(getContactList) contacts;
}Its template renders one li for each item in contacts.data. When contacts.error is set, it renders an element with the class error.
The test replaces the Apex import with a test wire adapter, then drives it.
import { createElement } from 'lwc';
import ContactList from 'c/contactList';
import getContactList from '@salesforce/apex/ContactController.getContactList';
jest.mock(
'@salesforce/apex/ContactController.getContactList',
() => {
const { createApexTestWireAdapter } = require('@salesforce/sfdx-lwc-jest');
return { default: createApexTestWireAdapter(jest.fn()) };
},
{ virtual: true }
);
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0));
describe('c-contact-list wired Apex', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('renders one item per contact', async () => {
const element = createElement('c-contact-list', { is: ContactList });
document.body.appendChild(element);
getContactList.emit([
{ Id: '001', Name: 'Amy Taylor' },
{ Id: '002', Name: 'Michael Jones' }
]);
await flushPromises();
const items = element.shadowRoot.querySelectorAll('li');
expect(items.length).toBe(2);
});
it('renders an error message when the wire errors', async () => {
const element = createElement('c-contact-list', { is: ContactList });
document.body.appendChild(element);
getContactList.error();
await flushPromises();
expect(element.shadowRoot.querySelector('.error')).not.toBeNull();
});
});Three details in this code are worth noting:
- The
jest.mockcall uses{ virtual: true }. The Apex module does not exist on disk, so Jest needs permission to mock a module it cannot resolve. - The imported
getContactListis the test adapter, soemitanderrorare available on it. - The test awaits
flushPromises()before reading the DOM. Emitting updates the wired property, but the component re-renders asynchronously.
What the component sees
After emit, the wired property holds an object shaped like { data, error }. With emit([...]) the component sees data set and error undefined. After error() it sees error set and data undefined. This is the same shape the platform delivers, so the template logic you test is the logic that runs in production.
Because each it creates a new element, you can test the loading state too. Attach the component and assert before calling emit. At that point neither data nor error exists.
Exercise
A test calls getContactList.emit([...]), then document.body.appendChild(element), and the list never renders. What is wrong?
Show answer
The component was not attached when emit ran, so no wire instance existed to receive the value. Attach the element first, then call emit, then await flushPromises() before querying the DOM.