-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Badura
committed
Jan 18, 2024
1 parent
5412376
commit d438a54
Showing
4 changed files
with
272 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { BehaviorSubject, Observable, of } from 'rxjs' | ||
import { CanActivateGuard } from './can-active-guard.service' | ||
|
||
let canActivateGuard: CanActivateGuard | ||
|
||
describe('CanActivateGuard', () => { | ||
const translateServiceSpy = jasmine.createSpyObj('TranslateService', ['setDefaultLang', 'use']) | ||
|
||
const configSpy = jasmine.createSpyObj('ConfigurationService', [], { lang$: new BehaviorSubject(undefined) }) | ||
|
||
const activatedRouteSpy = jasmine.createSpyObj('ActivatedRouteSnapshot', [], { | ||
routeConfig: { | ||
path: 'path' | ||
} | ||
}) | ||
const routerStateeSpy = jasmine.createSpyObj('RouterStateSnapshot', [], { | ||
routeConfig: { | ||
path: 'path' | ||
} | ||
}) | ||
|
||
beforeEach(async () => { | ||
canActivateGuard = new CanActivateGuard(translateServiceSpy, configSpy) | ||
translateServiceSpy.setDefaultLang.calls.reset() | ||
translateServiceSpy.use.calls.reset() | ||
}) | ||
|
||
it('should use default language if current not supported and return true', (doneFn: DoneFn) => { | ||
const langSpy = Object.getOwnPropertyDescriptor(configSpy, 'lang$')?.get as jasmine.Spy< | ||
() => BehaviorSubject<string> | ||
> | ||
langSpy.and.returnValue(new BehaviorSubject('pl')) | ||
// spyOn(console, 'log') | ||
translateServiceSpy.use.and.returnValue(of({})) | ||
|
||
const resultObs = canActivateGuard.canActivate(activatedRouteSpy, routerStateeSpy) as Observable<boolean> | ||
resultObs.subscribe({ | ||
next: (result) => { | ||
expect(result).toBe(true) | ||
doneFn() | ||
}, | ||
error: () => { | ||
doneFn.fail | ||
} | ||
}) | ||
|
||
expect(translateServiceSpy.setDefaultLang).toHaveBeenCalledWith('en') | ||
expect(translateServiceSpy.use).toHaveBeenCalledWith('en') | ||
}) | ||
|
||
it('should use provided language if current supported and return true', (doneFn: DoneFn) => { | ||
const langSpy = Object.getOwnPropertyDescriptor(configSpy, 'lang$')?.get as jasmine.Spy< | ||
() => BehaviorSubject<string> | ||
> | ||
langSpy.and.returnValue(new BehaviorSubject('de')) | ||
// spyOn(console, 'log') | ||
translateServiceSpy.use.and.returnValue(of({})) | ||
|
||
const resultObs = canActivateGuard.canActivate(activatedRouteSpy, routerStateeSpy) as Observable<boolean> | ||
resultObs.subscribe({ | ||
next: (result) => { | ||
expect(result).toBe(true) | ||
doneFn() | ||
}, | ||
error: () => { | ||
doneFn.fail | ||
} | ||
}) | ||
|
||
expect(translateServiceSpy.setDefaultLang).toHaveBeenCalledWith('en') | ||
// expect(console.log).toHaveBeenCalledOnceWith('user profile GUARD path') | ||
expect(translateServiceSpy.use).toHaveBeenCalledWith('de') | ||
}) | ||
}) |
63 changes: 63 additions & 0 deletions
63
src/app/shared/image-container/image-container.component.spec.ts
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,63 @@ | ||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing' | ||
import { ImageContainerComponent } from './image-container.component' | ||
|
||
describe('ImageContainerComponent', () => { | ||
let component: ImageContainerComponent | ||
let fixture: ComponentFixture<ImageContainerComponent> | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ImageContainerComponent] | ||
}).compileComponents() | ||
})) | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ImageContainerComponent) | ||
component = fixture.componentInstance | ||
fixture.detectChanges() | ||
}) | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy() | ||
}) | ||
|
||
describe('ngOnChanges', () => { | ||
it('should prepend apiPrefix to imageUrl if not starting with http/https and not already prefixed', () => { | ||
const testUrl = 'path/to/image.jpg' | ||
const expectedUrl = component['apiPrefix'] + testUrl | ||
|
||
component.imageUrl = testUrl | ||
component.ngOnChanges({ | ||
imageUrl: { | ||
currentValue: testUrl, | ||
previousValue: null, | ||
firstChange: true, | ||
isFirstChange: () => true | ||
} | ||
}) | ||
|
||
expect(component.imageUrl).toBe(expectedUrl) | ||
}) | ||
|
||
it('should not modify imageUrl if it starts with http/https', () => { | ||
const testUrl = 'http://path/to/image.jpg' | ||
component.imageUrl = testUrl | ||
component.ngOnChanges({ | ||
imageUrl: { | ||
currentValue: testUrl, | ||
previousValue: null, | ||
firstChange: true, | ||
isFirstChange: () => true | ||
} | ||
}) | ||
|
||
expect(component.imageUrl).toBe(testUrl) | ||
}) | ||
}) | ||
|
||
it('onImageError should set displayPlaceHolder to true', () => { | ||
component.onImageError() | ||
|
||
expect(component.displayPlaceHolder).toBeTrue() | ||
}) | ||
}) |
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,43 @@ | ||
import { LabelResolver } from './label.resolver' | ||
|
||
let labelResolver: LabelResolver | ||
|
||
describe('LabelResolver', () => { | ||
const translateServiceSpy = jasmine.createSpyObj('TranslateService', ['instant']) | ||
|
||
const activatedRouteSpy = jasmine.createSpyObj('ActivatedRouteSnapshot', [], { | ||
routeConfig: { | ||
path: 'path' | ||
}, | ||
data: {} | ||
}) | ||
|
||
const routerStateSpy = jasmine.createSpyObj('RouterStateSnapshot', ['']) | ||
|
||
beforeEach(async () => { | ||
labelResolver = new LabelResolver(translateServiceSpy) | ||
translateServiceSpy.instant.calls.reset() | ||
const dataSpy = Object.getOwnPropertyDescriptor(activatedRouteSpy, 'data')?.get as jasmine.Spy<() => {}> | ||
dataSpy.and.returnValue({}) | ||
}) | ||
|
||
it('should translate if breadcrumb is present', () => { | ||
const dataSpy = Object.getOwnPropertyDescriptor(activatedRouteSpy, 'data')?.get as jasmine.Spy<() => {}> | ||
dataSpy.and.returnValue({ | ||
breadcrumb: 'defined' | ||
}) | ||
translateServiceSpy.instant.and.returnValue('translation') | ||
|
||
const result = labelResolver.resolve(activatedRouteSpy, routerStateSpy) | ||
|
||
expect(result).toBe('translation') | ||
expect(translateServiceSpy.instant).toHaveBeenCalledOnceWith('defined') | ||
}) | ||
|
||
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) | ||
}) | ||
}) |
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,92 @@ | ||
import { SelectItem } from 'primeng/api' | ||
|
||
import { | ||
limitText, | ||
setFetchUrls, | ||
dropDownSortItemsByLabel, | ||
dropDownGetLabelByValue, | ||
sortByLocale, | ||
filterObject | ||
} from './utils' | ||
|
||
describe('util functions', () => { | ||
describe('limitText', () => { | ||
it('should truncate text that exceeds the specified limit', () => { | ||
const result = limitText('hello', 4) | ||
|
||
expect(result).toEqual('hell...') | ||
}) | ||
|
||
it('should return the original text if it does not exceed the limit', () => { | ||
const result = limitText('hello', 6) | ||
|
||
expect(result).toEqual('hello') | ||
}) | ||
|
||
it('should return an empty string for undefined input', () => { | ||
const str: any = undefined | ||
const result = limitText(str, 5) | ||
|
||
expect(result).toEqual('') | ||
}) | ||
}) | ||
|
||
describe('setFetchUrls', () => { | ||
it('should prepend apiPrefix to a relative URL', () => { | ||
const result = setFetchUrls('ahm-api', '/am') | ||
|
||
expect(result).toEqual('ahm-api/am') | ||
}) | ||
|
||
it('should return the original URL if it is absolute', () => { | ||
const result = setFetchUrls('ahm-api', 'http://am') | ||
|
||
expect(result).toEqual('http://am') | ||
}) | ||
}) | ||
|
||
describe('dropDownSortItemsByLabel', () => { | ||
it('should correctly sort items by label', () => { | ||
const items: SelectItem[] = [ | ||
{ label: 'label2', value: 2 }, | ||
{ label: 'label1', value: 1 } | ||
] | ||
|
||
const sortedItems = items.sort(dropDownSortItemsByLabel) | ||
|
||
expect(sortedItems[0].label).toEqual('label1') | ||
}) | ||
}) | ||
|
||
describe('dropDownGetLabelByValue', () => { | ||
it('should return the label corresponding to the value', () => { | ||
const items: SelectItem[] = [ | ||
{ label: 'label2', value: 2 }, | ||
{ label: 'label1', value: 1 } | ||
] | ||
|
||
const result = dropDownGetLabelByValue(items, '1') | ||
|
||
expect(result).toEqual('label1') | ||
}) | ||
}) | ||
|
||
describe('sortByLocale', () => { | ||
it('should sort strings based on locale', () => { | ||
const strings: string[] = ['str2', 'str1'] | ||
|
||
const sortedStrings = strings.sort(sortByLocale) | ||
|
||
expect(sortedStrings[0]).toEqual('str1') | ||
}) | ||
}) | ||
|
||
describe('filterObject', () => { | ||
it('should exclude specified properties from the object', () => { | ||
const obj = { prop1: 'value1', prop2: 'value2', prop3: 'value3' } | ||
const exProps = ['prop2', 'prop3'] | ||
const result = filterObject(obj, exProps) | ||
expect(result).toEqual({ prop1: 'value1' }) | ||
}) | ||
}) | ||
}) |