-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for filters service (#312)
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
1 parent
1fc836f
commit 468bc8f
Showing
3 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |