Skip to content

Commit

Permalink
Add test cases for filters service (#312)
Browse files Browse the repository at this point in the history
Filters service does not have associated tests.

Tests for filters service would make the project
more stable.

Let's add test cases for the filters service.
  • Loading branch information
Arif-Khalid authored Apr 26, 2024
1 parent 1fc836f commit 468bc8f
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/app/core/services/filters.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export class FiltersService {
}

clearFilters(): void {
this.updateFilters(this.defaultFilter);
this.updatePresetView('currentlyActive');
this.previousMilestonesLength = 0;
}
Expand Down Expand Up @@ -190,7 +191,6 @@ export class FiltersService {
}

this.updateFilters(nextFilter);

// Use preset view if set in url
const presetView = queryParams.get(FiltersService.PRESET_VIEW_QUERY_PARAM_KEY);
if (presetView && this.presetViews.hasOwnProperty(presetView)) {
Expand Down Expand Up @@ -318,7 +318,6 @@ export class FiltersService {
if (latestClosedMilestone) {
return [latestClosedMilestone, Milestone.PRWithoutMilestone];
}

return [...this.milestoneService.milestones, Milestone.PRWithoutMilestone];
}
}
25 changes: 25 additions & 0 deletions tests/constants/filter.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Filter } from '../../src/app/core/services/filters.service';

export const DEFAULT_FILTER: Filter = {
title: '',
status: ['open pullrequest', 'merged pullrequest', 'open issue', 'closed issue'],
type: 'all',
sort: { active: 'status', direction: 'asc' },
labels: [],
milestones: ['PR without a milestone'],
hiddenLabels: new Set<string>(),
deselectedLabels: new Set<string>(),
itemsPerPage: 20
};

export const CHANGED_FILTER: Filter = {
title: 'test',
status: ['open pullrequest'],
type: 'issue',
sort: { active: 'id', direction: 'asc' },
labels: ['aspect-testing', 'aspect-documentation'],
milestones: ['V3.3.6'],
hiddenLabels: new Set<string>(['aspect-testing']),
deselectedLabels: new Set<string>(['aspect-documentation']),
itemsPerPage: 50
};
128 changes: 128 additions & 0 deletions tests/services/filters.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { ActivatedRoute, convertToParamMap, Router } from '@angular/router';
import { FiltersService } from '../../src/app/core/services/filters.service';
import { LoggingService } from '../../src/app/core/services/logging.service';
import { MilestoneService } from '../../src/app/core/services/milestone.service';
import { CHANGED_FILTER, DEFAULT_FILTER } from '../constants/filter.constants';

let filtersService: FiltersService;
let loggingServiceSpy: jasmine.SpyObj<LoggingService>;
let routerSpy: jasmine.SpyObj<Router>;
let activatedRouteSpy: jasmine.SpyObj<ActivatedRoute>;
let milestoneServiceSpy: jasmine.SpyObj<MilestoneService>;

describe('FiltersService', () => {
beforeEach(() => {
loggingServiceSpy = jasmine.createSpyObj('LoggingService', ['info']);
routerSpy = jasmine.createSpyObj('Router', ['navigate']);
activatedRouteSpy = jasmine.createSpyObj('ActivatedRoute', ['snapshot'], {
snapshot: {
queryParamMap: convertToParamMap({})
}
});
milestoneServiceSpy = jasmine.createSpyObj('MilestoneService', ['milestones', 'getEarliestOpenMilestone', 'getLatestClosedMilestone'], {
milestones: []
});
filtersService = new FiltersService(loggingServiceSpy, routerSpy, activatedRouteSpy, milestoneServiceSpy);
filtersService.initializeFromURLParams();
});

it('should initially be on currentlyActive preset', (done) => {
filtersService.presetView$.subscribe((presetView) => {
expect(presetView).toEqual('currentlyActive');
done();
});
});

it('should initially have the correct default filters', (done) => {
filtersService.filter$.subscribe((filter) => {
expect(filter).toEqual(DEFAULT_FILTER);
done();
});
});

describe('.clearFilters', () => {
it('should reset to default filters', (done) => {
filtersService.updateFilters(CHANGED_FILTER);
filtersService.clearFilters();
filtersService.filter$.subscribe((filter) => {
expect(filter).toEqual(DEFAULT_FILTER);
done();
});
});
it('should reset to default preset view', (done) => {
filtersService.updateFilters(CHANGED_FILTER);
filtersService.clearFilters();
filtersService.presetView$.subscribe((presetView) => {
expect(presetView).toEqual('currentlyActive');
done();
});
});
});

describe('.updateFilters', () => {
beforeEach(() => {
filtersService.clearFilters();
});

it('should correctly update filters', (done) => {
filtersService.updateFilters(CHANGED_FILTER);
filtersService.filter$.subscribe((filter) => {
expect(filter).toEqual(CHANGED_FILTER);
done();
});
});

it('should update preset views when certain filters changed', (done) => {
filtersService.updateFilters({ labels: ['aspect-testing'] });
filtersService.presetView$.subscribe((presetView) => {
expect(presetView).toEqual('custom');
done();
});
});

it('should retain preset view when certain filters are changed', (done) => {
filtersService.updateFilters({ title: 'test' });
filtersService.presetView$.subscribe((presetView) => {
expect(presetView).toEqual('currentlyActive');
done();
});
});

it('should push filters to URL', (done) => {
filtersService.updateFilters(CHANGED_FILTER);
expect(routerSpy.navigate).toHaveBeenCalled();
done();
});
});

describe('.updatePresetView', () => {
beforeEach(() => {
filtersService.clearFilters();
});

it('should correctly update preset view', (done) => {
filtersService.updatePresetView('contributions');
filtersService.presetView$.subscribe((presetView) => {
expect(presetView).toEqual('contributions');
done();
});
});

it('should correctly apply filters when updating preset view', (done) => {
filtersService.updatePresetView('contributions');
filtersService.filter$.subscribe((filter) => {
expect(filter.sort).toEqual({ active: 'id', direction: 'desc' });
done();
});
});

it('should not overwrite certain filters', (done) => {
filtersService.updateFilters({ hiddenLabels: new Set(['aspect-testing']) });
filtersService.updatePresetView('contributions');
filtersService.filter$.subscribe((filter) => {
expect(filter.hiddenLabels).toEqual(new Set(['aspect-testing']));
done();
});
});
});
});

0 comments on commit 468bc8f

Please sign in to comment.