0

I have a list of components, some of them come with the property "factor" and others comes with the property "quickpay".

I created a couple getters for them:

get hasFactor(): boolean {
    return this.load.factor;
}
get hasQuickpay(): boolean {
    return this.load.quickpay;
}

and then created a function that among other things opens a certain URL depending on if "factor" or "quickpay" is true (if one of them is true the other is false).

visitPage(){
    if(this.hasFactor){
        this.trackLink(SearchSharedTracking.goToFactor,this.Factor);
        window.open('https://www.spacepln.com/factor/ ','_blank');
    }else{
        this.trackLink(SearchSharedTracking.goToQuickpay,this.Quickpay);
        window.open('https://www.spacepln.com/quickpay','_blank');
    }
}

In my .spect file, I have to test using Karma - Jasmine whether the factor page was opened or quickpay was opened. I'm stuck and don't know how to write a unit test for that.

halfer
  • 19,824
  • 17
  • 99
  • 186
Julio Rodriguez
  • 499
  • 6
  • 16

1 Answers1

1

You can spy on window.open to test which URL gets opened according to your specified conditions. These tests should work.

    it('shout open factor page', () => {
        spyOn(window, 'open');
        component.hasFactor = true;
        visitPage(false);        
        expect(window.open).toHaveBeenCalledWith('https://www.spacepln.com/factor/','_blank')
    });

    
    it('shout open quickpay page', () => {
        spyOn(window, 'open');
        component.hasFactor = false;
        visitPage(false);        
        expect(window.open).toHaveBeenCalledWith('https://www.spacepln.com/quickpay','_blank')
    });
Ankit Choudhary
  • 145
  • 2
  • 13
  • I throws me error that I can not assign "true" or "false" to hasFactor here: component.hasFactor = false; since it is a read-only property. Maybe because it is a getter? How could I work around this? thanks. – Julio Rodriguez Aug 01 '21 at 18:49
  • 1
    You can spy on your getter functions and return a custom value like this - spyOn(component, 'hasFactor').and.returnValue(true); – Ankit Choudhary Aug 02 '21 at 08:53