wire-service-jest-util
wire-service-jest-util copied to clipboard
Config empty for test wire adapter
I was attempting to emit getRelatedListRecords with 2 different sets of data and using the config as mentioned in #15 but the config is always an empty object. There are other reports of the config being blank on stack exchange.
@jjbennett do you have a repro we can look at?
I created a simple repo and in doing so I found the issue to be when using a dynamic and reactive property in the config. If I hard code the Ids on line 15 and 18 then it works as expected.
config is available if you change line 15 and 18 to...
@wire(getRecord, { recordId: '0017i00001MIvJOAA1', fields: [ID] })
@wire(getRecord, { recordId: '0017i00001MIvKdAAL', fields: [ID] })
import { LightningElement, api, wire } from "lwc";
import { getRecord } from "lightning/uiRecordApi";
import ID from "@salesforce/schema/Account.Id";
export default class ConfigIssue extends LightningElement {
@api firstRecordId = "0017i00001MIvJOAA1";
@api secondRecordId = "0017i00001MIvKdAAL";
@wire(getRecord, { recordId: '$firstRecordId', fields: [ID] })
firstRecord;
@wire(getRecord, { recordId: '$secondRecordId', fields: [ID] })
secondRecord;
@api getFirstRecordId() {
return this.firstRecord?.data?.Id;
}
@api getSecondRecordId() {
return this.secondRecord?.data?.Id;
}
}
import { createElement } from "lwc";
import configIssue from "../configIssue";
import { getRecord } from "lightning/uiRecordApi";
describe("c-config-issue", () => {
const mockGetRecordFirst = { "Id": "0017i00001MIvJOAA1" };
const mockGetRecordSecond = { "Id": "0017i00001MIvKdAAL" };
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
jest.clearAllMocks();
});
test("input => result", () => {
const element = createElement("c-config-issue", {
is: configIssue
});
document.body.appendChild(element);
getRecord.emit(mockGetRecordFirst, (config) => {
console.log('config: ', config);
return config.recordId === "0017i00001MIvJOAA1"
});
getRecord.emit(mockGetRecordSecond, (config) => config.recordId === "0017i00001MIvKdAAL");
expect(element.getFirstRecordId()).toBe("0017i00001MIvJOAA1");
expect(element.getSecondRecordId()).toBe("0017i00001MIvKdAAL");
});
});
@jjbennett sorry I wasn't able to get to this earlier, could you try your test again but wait for a few ticks on the microtask queue?
Something like this:
describe("c-config-issue", () => {
const mockGetRecordFirst = { "Id": "0017i00001MIvJOAA1" };
const mockGetRecordSecond = { "Id": "0017i00001MIvKdAAL" };
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
jest.clearAllMocks();
});
test("input => result", () => {
const element = createElement("c-config-issue", {
is: Config
});
document.body.appendChild(element);
return Promise.resolve().then(() => {
ldsAdapterMock.emit(mockGetRecordFirst, (config) => {
console.log('config: ', config);
return config.recordId === "0017i00001MIvJOAA1"
});
ldsAdapterMock.emit(mockGetRecordSecond, (config) => config.recordId === "0017i00001MIvKdAAL");
expect(element.getFirstRecordId()).toBe("0017i00001MIvJOAA1");
expect(element.getSecondRecordId()).toBe("0017i00001MIvKdAAL");
});
});
});