Reputation: 5018
I'm new to Unit testing in Angular using Jasmine and Karma. I've written a spec file but I'm getting an error. Let me first show what I've done so far. I've created a custom component:
TimeselectorComponent
startRange = moment('12-01-01');
endRange = moment ('12-12-01');
modes= ['Calendar Year', 'Year-to-date', 'Rolling Year', 'Custom'];
date = new Date();
currentYear = this.date.getFullYear(); // or simply currentYear = 2020;
@ViewChild('primaryMonthPicker') primaryMonthPicker: MonthpickerComponent; // a child component
primaryDropDown(startRange, endRange) {
if (this.primaryMode === this.modes[0]) {
this.initCalendarYear(this.primaryMonthPicker, this.currentYear);
} else if (this.primaryMode === this.modes[1]) {
this.initYearToDate(this.primaryMonthPicker, this.currentYear);
} else if (this.primaryMode === this.modes[2]) {
this.initRollingYear(this.primaryMonthPicker, this.currentYear);
}
}
In the above code MonthpickerComponent
is another custom component which is the only child component of TimeselectorComponent
.
I just want to check when primaryDropDown
is called then initCalendarYear
should also be called. Here's the spec file:
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { TimeselectorComponent } from './timeselector.component';
...
describe('TimeselectorComponent', () => {
let fixture: ComponentFixture<TimeselectorComponent>;
@Component({
selector: 'app-monthpicker',
template: '<div></div>'
})
class FakeMonthpickerComponent {
// methods of MonthpickerComponent
}
let MODES = [];
beforeEach(async(() => {
MODES = ['Calendar Year', 'Year-to-date', 'Rolling Year', 'Custom'];
TestBed.configureTestingModule({
declarations: [FakeMonthpickerComponent, TimeselectorComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TimeselectorComponent);
});
// other test cases that are passing
// need help with this test case
it('should call initCalendarYear when primaryDropDown is called', () => {
const startRange=moment('2020-01-01');
const endRange= moment('2020-12-01');
spyOn(fixture.componentInstance, 'initCalendarYear');
fixture.componentInstance.primaryMode=MODES[0];
fixture.componentInstance.primaryDropDown(startRange, endRange);
expect(fixture.componentInstance.initCalendarYear).toHaveBeenCalled();
});
});
I have to test whether correct method is called for associated if-else
statement. Please help me.
Here's the stackblitz.
Upvotes: 0
Views: 975
Reputation: 10979
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { TimeselectorComponent } from './timeselector.component';
import { Component, NO_ERRORS_SCHEMA } from '@angular/core';
var moment = require('moment/moment')
describe('TimeselectorComponent', () => {
let fixture: ComponentFixture<TimeselectorComponent>;
@Component({
selector: 'app-monthpicker',
template: '<div></div>'
})
class FakeMonthpickerComponent {
// methods of MonthpickerComponent
}
let MODES = [];
beforeEach(async(() => {
MODES = ['Calendar Year', 'Year-to-date', 'Rolling Year', 'Custom'];
TestBed.configureTestingModule({
declarations: [FakeMonthpickerComponent, TimeselectorComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TimeselectorComponent);
});
// other test cases that are passing
// need help with this test case
it('should call initCalendarYear when primaryDropDown is called', () => {
const startRange=moment('2020-01-01');
const endRange= moment('2020-12-01');
spyOn(fixture.componentInstance, 'initCalendarYear');
fixture.componentInstance.primaryMode=MODES[0];
fixture.componentInstance.primaryDropDown(startRange, endRange);
expect(fixture.componentInstance.initCalendarYear).toHaveBeenCalled();
});
});
The above game me success :-
Upvotes: 2
Reputation: 1019
Add a spy like this
spyOn(fixture.componentInstance, 'initCalendarYear');
Change the last line by this
expect(fixture.componentInstance.initCalendarYear).toHaveBeenCalled();
So your final test case should look like this
it('should call initCalendarYear when primaryDropDown is called', () => {
const startRange=moment('2020-01-01');
const endRange= moment('2020-12-01');
fixture.componentInstance.primaryMode = MODES[0];
spyOn(fixture.componentInstance, 'initCalendarYear');
fixture.componentInstance.primaryDropDown(startRange, endRange);
expect(fixture.componentInstance.initCalendarYear).toHaveBeenCalled();
});
Upvotes: 1