How to JEST test a @wire(CurrentPagereference)

lightning-web-componentslwc-jestnavigation

I am unable to hit these lines of code even after I added some jest-mocks to my test folder. I am sure that I would have to do something like below, but how would I then hit it to verify what is inside the if statement?

let mockCurrentPageReference = {
     type: "standard__navItemPage",
     attributes: {
          apiName: "Wire"
     },
     state: {
          c__mediaId: "mediaId"
     }
};
currentPageReferenceAdapter.emit(mockCurrentPageReference);
 

JavaScript:

@wire(CurrentPageReference)
wiredPageReference(currentPageReference){
     if(currentPageReference){
          this.currentPageRefernce = currentPageReference;
          this.mediaId = this.currentPageRefernce.state.c__mediaId;
     }
}

Best Answer

I found the issue, it was from wrong placement of .emit. currentPageReferenceAdpater.emit(mockCurrentPageReference) was being set before the document.body.apendChild(testComp) so it wasn't being registered.

The expect statement was:

expect(testComp.mediaId).toBe('mediaId');
Related Topic