Skip to content

Commit

Permalink
✅ improve dataset-detail coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bunop committed Jun 21, 2024
1 parent 605438c commit b986129
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/app/datasets/dataset-detail/dataset-detail.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SpyLocation } from '@angular/common/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { of } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { Location } from '@angular/common';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

import { DatasetDetailComponent } from './dataset-detail.component';
import { Dataset } from '../datasets.model';
import { SamplesComponent } from 'src/app/samples/samples.component';

const dataset: Dataset = {
"_id": {
Expand Down Expand Up @@ -38,35 +41,58 @@ const route = { data: of({ dataset: dataset }) };
describe('DatasetDetailComponent', () => {
let component: DatasetDetailComponent;
let fixture: ComponentFixture<DatasetDetailComponent>;
let location: SpyLocation;
let mockRouter = {
navigate: jasmine.createSpy('navigate') // Create a spy for the navigate method
};

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule,
RouterTestingModule.withRoutes(
[{path: 'samples', component: SamplesComponent}]
),
HttpClientTestingModule,
],
declarations: [ DatasetDetailComponent ],
// https://testing-angular.com/testing-components-with-children/#unit-test
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
providers: [
{ provide: ActivatedRoute, useValue: route },
{ provide: Location, useClass: SpyLocation },
{ provide: Router, useValue: mockRouter },
]
})
.compileComponents();

fixture = TestBed.createComponent(DatasetDetailComponent);
component = fixture.componentInstance;
location = <SpyLocation>TestBed.inject(Location);
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});

describe('ngOnInit', () => {
it('should get dataset data', () => {
component.ngOnInit();
expect(component.dataset).toEqual(dataset);
});
it('should get dataset data', () => {
component.ngOnInit();
expect(component.dataset).toEqual(dataset);
});

// https://codeutility.org/unit-testing-angular-6-location-go-back-stack-overflow/
it('should go back to previous page on back button click', () => {
spyOn(location, 'back');
component.goBack();
expect(location.back).toHaveBeenCalled();
});

it('should navigate to /samples with query params when getSamples is called', () => {
component.getSamples();
expect(mockRouter.navigate).toHaveBeenCalledWith(
['/samples'],
{ queryParams: { dataset: component.dataset._id.$oid, species: component.dataset.species } }
);
});

});

0 comments on commit b986129

Please sign in to comment.