4

I have a LWC component which uses CurrentPageReference from lightning/navigation bundle in a @wire function.

I am using lwc-jest to create unit tests for my components.

Using the wire with CurrentPageReference fails all tests.

I've tried to mock this wire just as all other wires connected to custom Apex controller - by emiting mock JSON data with .emit(json) method on object returned by registerApexTestWireAdapter(importedApexController) function. It seem not to work as there is still the same error as before:

TypeError: Cannot read property 'c__jobId' of undefined

So basically even that I've mocked the object returned by the wire and provided the 'c__jobId' property it still sees the state object as undefined

I found something promising on ebikes-lwc repo, some kind of mocking replacement:

export const CurrentPageReference = jest.fn();

let _pageReference, _replace;

const Navigate = Symbol('Navigate');
const GenerateUrl = Symbol('GenerateUrl');
export const NavigationMixin = Base => {
    return class extends Base {
        [Navigate](pageReference, replace) {
            _pageReference = pageReference;
            _replace = replace;
        }
        [GenerateUrl](pageReference) {}
    };
};
NavigationMixin.Navigate = Navigate;
NavigationMixin.GenerateUrl = GenerateUrl;

export const getNavigateCalledWith = () => {
    return {
        pageReference: _pageReference,
        replace: _replace
    };
};

But either I can't use it properly or it's not working for that case. (Haven't found any usages of that in the repo though).

Has anyone approached similar issue or simply know how to make those tests working?

Any trace could be helpful, thanks in advance.

2

CurrentPageReference isn't Apex Wire. Use registerTestWireAdapter(CurrentPageReference) instead of Apex Wire :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.