Skip to content

Commit

Permalink
fix: fixed label resolver implementation and adjusted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jan 31, 2024
1 parent ccb483f commit 17021ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
20 changes: 12 additions & 8 deletions src/app/shared/label.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Observable, of } from 'rxjs'
import { LabelResolver } from './label.resolver'

let labelResolver: LabelResolver

describe('LabelResolver', () => {
const translateServiceSpy = jasmine.createSpyObj('TranslateService', ['instant'])
const translateServiceSpy = jasmine.createSpyObj('TranslateService', ['get'])

const activatedRouteSpy = jasmine.createSpyObj('ActivatedRouteSnapshot', [], {
routeConfig: {
Expand All @@ -16,28 +17,31 @@ describe('LabelResolver', () => {

beforeEach(async () => {
labelResolver = new LabelResolver(translateServiceSpy)
translateServiceSpy.instant.calls.reset()
translateServiceSpy.get.calls.reset()
const dataSpy = Object.getOwnPropertyDescriptor(activatedRouteSpy, 'data')?.get as jasmine.Spy<() => {}>
dataSpy.and.returnValue({})
})

it('should translate if breadcrumb is present', () => {
it('should translate if breadcrumb is present', (done: DoneFn) => {
const dataSpy = Object.getOwnPropertyDescriptor(activatedRouteSpy, 'data')?.get as jasmine.Spy<() => {}>
dataSpy.and.returnValue({
breadcrumb: 'defined'
})
translateServiceSpy.instant.and.returnValue('translation')
translateServiceSpy.get.and.returnValue(of('translation'))

const result = labelResolver.resolve(activatedRouteSpy, routerStateSpy)
const obsResult = labelResolver.resolve(activatedRouteSpy, routerStateSpy) as Observable<string>
obsResult.subscribe((result) => {
expect(result).toBe('translation')
expect(translateServiceSpy.get).toHaveBeenCalledOnceWith('defined')

expect(result).toBe('translation')
expect(translateServiceSpy.instant).toHaveBeenCalledOnceWith('defined')
done()
})
})

it('should use route path if breadcrumb is not present', () => {
const result = labelResolver.resolve(activatedRouteSpy, routerStateSpy)

expect(result).toBe('path')
expect(translateServiceSpy.instant).toHaveBeenCalledTimes(0)
expect(translateServiceSpy.get).toHaveBeenCalledTimes(0)
})
})
6 changes: 1 addition & 5 deletions src/app/shared/label.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router'
import { TranslateService } from '@ngx-translate/core'
import { Observable } from 'rxjs'
import { Observable, map } from 'rxjs'

//dont use `providedIn root` - wont work when we are in shell
@Injectable()
export class LabelResolver implements Resolve<string> {
constructor(private translate: TranslateService) {}
resolve(route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): string | Observable<string> | Promise<string> {
return route.data['breadcrumb'] ? this.translate.instant(route.data['breadcrumb']) : route.routeConfig?.path
/* TODO: use this if tests are running with it.
resolve(route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): string | Observable<string> | Promise<string> {
return route.data['breadcrumb']
? this.translate.get(route.data['breadcrumb']).pipe(map((t) => t.toString()))
: route.routeConfig?.path ?? ''
*/
}
}

0 comments on commit 17021ea

Please sign in to comment.