wire-service-jest-util icon indicating copy to clipboard operation
wire-service-jest-util copied to clipboard

Config empty for test wire adapter

Open jjbennett opened this issue 2 years ago • 3 comments

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 avatar Feb 14 '23 17:02 jjbennett

@jjbennett do you have a repro we can look at?

jmsjtu avatar Feb 23 '23 17:02 jmsjtu

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 avatar Feb 23 '23 20:02 jjbennett

@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");
        });
    });
});

jmsjtu avatar Apr 06 '23 01:04 jmsjtu