I have been hacking at this for a few days. I have an LWC with a wired Apex method. When it gets into the renderedCallBack() and tries to assign focus to an element the test fails with a message of 'cannot read property focus of null' the dom element in question also shows as null when I try to console.log it.
The component gets passed data into a public property and renders when the data from the wire service loads.
I have copied the examples from the LWC recipes repo about testing the wire service.
I assume that I am making some mistakes when it comes to emitting the data from the wire adapter. The component is not rendering and this leads to the failure when rendereredCallback() tries to assign focus.
HTML
<template if:true={userInfo}>
<lightning-input label="Parent First Name"
type="text"
value={patient.LegalGuardian__r.FirstName}
onchange={handleChange}
name="firstName"
required
data-id="parentFirstName">
</lightning-input>
</div>
</template>
JS
export default class VerifyGuardianScreen extends Screen {
@api patient;
userInfo;
error;
isLoaded = false;
renderedCallback() {
let target = this.template.querySelector(`[data-id="parentFirstName"]`);
console.log(`target ${target}`);
target.focus();
}
@wire(getUserInfo)
wiredUser(value) {
this.provisionedValue = value;
const { data, error } = this.provisionedValue;
if (data) {
this.userInfo = data;
} else if (error) {
this.error = error;
}
}
And my test: pardon the lack of assertions.
import { createElement } from "lwc";
import VerifyGuardianScreen from "c/verifyGuardianScreen";
import getUserInfo from "@salesforce/apex/OnboardingControllers.getUserInfo";
const PATIENT_MOCK = require("../../onboardingMocks/over18Patient.json");
const USER_MOCK = require("../../onboardingMocks/userInfo.json");
jest.mock(
"@salesforce/apex/OnboardingControllers.getUserInfo",
() => {
const { createApexTestWireAdapter } = require("@salesforce/sfdx-lwc-jest");
return {
default: createApexTestWireAdapter(jest.fn())
};
},
{ virtual: true }
);
describe("c-verify-guardian-screen test suite", () => {
afterEach(() => {
if (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
jest.clearAllMocks();
});
it("it should display custom labels", () => {
const element = createElement("c-verify-guardian-screen", {
is: VerifyGuardianScreen
});
document.body.appendChild(element);
getUserInfo.emit(USER_MOCK);
element.patient = PATIENT_MOCK;
return Promise.resolve().then(() => {});
});
});
this permutation of the test setup also fails.
it("it should display custom labels", () => {
const element = createElement("c-verify-guardian-screen", {
is: VerifyGuardianScreen
});
element.patient = PATIENT_MOCK;
document.body.appendChild(element);
getUserInfo.emit(USER_MOCK);
return Promise.resolve().then(() => {});
});
Best Answer
Two things:
userData
. If the element is not found, you shouldn't be doing a focus. That may happen in real-world usage when the wire returns an error.For #2 above, a public property (
patient
) would be getting set when created (<c-mycomponent patient={passedInData}...
).As such, to mimic the real-world scenario - you'd want to set your api value before creating the component/element in your jest test.
If you don't want to do #1 above, you just need to emit the mocked data for the wire before appending the component - otherwise, you risk having a
renderedCallback()
occur where there is no mocked data returned in your wire to set your input field to visible (thus leading to trying to focus on an element that doesn't exist).