I am trying to write unit tests for my component but cannot seem to get it to work. My component looks like this:
import { Component, OnInit } from '@angular/core';
import {ModalController} from '@ionic/angular';
import {CheckInsProvider} from '../../providers/check-ins.service';
import * as _ from 'lodash'
import {WeighInsProvider} from '../../pages/clients/client-detail/providers/weigh-ins/weigh-ins.service';
@Component({
selector: 'app-re-evaluation',
templateUrl: './re-evaluation.component.html',
styleUrls: ['./re-evaluation.component.scss'],
})
export class ReEvaluationComponent implements OnInit {
programId: number;
initialWeight: number;
endingWeight: number;
initialBodyFat: number;
endingBodyFat: number;
initialVisceralFat: number;
endingVisceralFat: number;
constructor( private modalCtrl: ModalController,
private checkInsProvider: CheckInsProvider,
private weighInsProvider: WeighInsProvider ) { }
ngOnInit() {
this.fetchWeights()
}
fetchWeights() {
console.log("here", this.weighInsProvider.findFirstWithField('current_weight'))
this.initialWeight = this.weighInsProvider.findFirstWithField('current_weight').current_weight
this.endingWeight = this.weighInsProvider.findLastWithField('current_weight').current_weight
this.initialBodyFat = this.weighInsProvider.findFirstWithField('body_fat').body_fat
this.endingBodyFat = this.weighInsProvider.findLastWithField('body_fat').body_fat
this.initialVisceralFat = this.weighInsProvider.findFirstWithField('visceral_fat').visceral_fat
this.endingVisceralFat = this.weighInsProvider.findLastWithField('visceral_fat').visceral_fat
}
dismiss(): void {
this.modalCtrl.dismiss()
}
}
and my test looks like this:
describe('ReEvaluationComponent', () => {
let component: ReEvaluationComponent;
let fixture: ComponentFixture<ReEvaluationComponent>;
let mockGlobalsService = jasmine.createSpyObj('GlobalsService', ['base_url'])
let checkInsService: any;
let weighInsService: any;
let injector: TestBed;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ReEvaluationComponent ],
imports: [
IonicModule.forRoot(),
HttpClientTestingModule,
RouterTestingModule,
],
providers: [
WeighInsProvider,
{provide: GlobalsService, useValue: mockGlobalsService},
{provide: PurchasesService, useValue: PurchasesServiceMock},
]
}).compileComponents();
fixture = TestBed.createComponent(ReEvaluationComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
beforeEach(() => {
injector = getTestBed()
checkInsService = injector.get(CheckInsProvider)
weighInsService = injector.get(WeighInsProvider)
})
it('should create', () => {
spyOn(weighInsService, 'findFirstWithField').and.returnValue({
current_weight: 200,
visceral_fat: 20,
body_fat: 20
})
expect(component).toBeTruthy();
});
...
And the should create test fails, stating:
ReEvaluationComponent should create FAILED
Failed: Cannot read properties of undefined (reading 'current_weight')
Which is strange to me because I thought my spy would return the value:
{
current_weight: 200,
visceral_fat: 20,
body_fat: 20
}
What am I doing wrong?