Need help for writing test cases for dynamic content in Angular 6 and using Karma packages.
spec.ts:
I wrote test case for checking the views of the article. Before executing the function, it is passing but after getting the data it is not passing. How to write test cases for dynamic content.
describe('SingleArticleVideoComponent', () => {
let originalTimeout;
let debugTest: DebugElement[];
let el: HTMLElement;
let component: SingleArticleVideoComponent;
let fixture: ComponentFixture<SingleArticleVideoComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SingleArticleVideoComponent,
PollsComponent,
AdBannerComponent],
imports: [
RouterTestingModule,
NavModule,
FooterModule,
VgCoreModule,
VgControlsModule,
VgOverlayPlayModule,
VgBufferingModule,
FormsModule,
ReactiveFormsModule,
MatFormFieldModule,
MatRadioModule,
MatDialogModule,
HttpModule,
HttpClientModule,
BrowserAnimationsModule,
BrowserModule
],
providers: [
ArticleService,
AdService,
UserServiceService
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SingleArticleVideoComponent);
component = fixture.componentInstance;
fixture.detectChanges();
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;
});
afterEach(function() {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
fixture = TestBed.createComponent(SingleArticleVideoComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('views should be more than 100', async(() => {
expect(component.anchor).toEqual('before');
expect(component.anchor).toEqual('after');
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
component.ts:
It is giving the correct result when I use ng serve, but it is not working for testing using ng test.
ngOnInit() {
this.get_single_video('emcure-csi-tv-dr-pk-dep-ACE-inhibitor-or-ARNI-what-should-be-used-in-heart-failure-with-reduced-ejection-fraction', this.category);
}
get_single_video(slug, category) {
console.log('get one video calling');
this.anchor ='before';
this.service.get_single_video(slug, category).subscribe(
data => {
if(data['success'])
{
this.anchor='after';
this.load_data = true;
if(data['data']['guest3'].length > 0 || data['data']['guest4'].length > 0){
this.gus = true;
}
}
});
component.html:
Views is giving null when I console it. Views are displaying normally if I run it using ng serve, but not for ng test.
<li class="views"><code>{{single_article['anchor']}}</code><br>Views</li>
user-service.service.ts:
I can able to see the data.json() in map function, but I unable to get inside the subscribe function in component.ts file as I mentioned above.
import 'rxjs/Rx';
get_single_video(slug, catagory) {
console.log('in article service single video');
const final_url = this.api_url + '/' + slug + '?key=' + this.api_key;
console.log(final_url);
return this._http.get(final_url)
.map(data => {
data.json();
// the console.log(...) line prevents your code from working
// either remove it or add the line below (return ...)
console.log(' I CAN SEE DATA HERE: ', data.json());
return data.json();
}).catch(error => observableThrowError(error.json()));
}