Bwizard
Bwizard

Reputation: 1023

Test Coverage on NgRX store selector Unit test

My test is passing but I am unable to achieve test coverage on a NgRX store select. this.isNIAddress$ is set in ngOnInit.

enter image description here

I am using Jest and setting the return value of this.isNIAddress$ within the test. If I do not do that then I get an error saying addresses inside the store is empty. How do I mock the getAllValidAddresses selector?

enter image description here

Error if I add the component.ngOnInit()

enter image description here

How do I properly test this store observable to obtain code coverage?

Upvotes: 0

Views: 237

Answers (1)

Jai
Jai

Reputation: 74738

As per comments you can have one spy object in your spec:

spyOn(component, 'isPostcodeNI').and.returnValue(true);
component.isPostcodeNI(NiAddress[0].postcode);
expect(component.isPostcodeNI).toHaveBeenCalled();

Now you can expect the spy has been called with the postcode.

Upvotes: 0

Related Questions