Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prepared theme ui tests #20

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@
"assets": ["src/favicon.ico", "src/assets"],
"styles": ["src/styles.scss"],
"scripts": [],
"codeCoverageExclude": ["src/app/test/**", "src/app/generated/**"]
"codeCoverageExclude": [
"**/*.module.ts",
"src/app/test/**",
"src/app/environments/**",
"src/app/generated/**"
]
}
},
"lint": {
Expand Down
86 changes: 86 additions & 0 deletions src/app/shared/can-active-guard.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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),
lang: 'en'
})

const activatedRouteSpy = jasmine.createSpyObj('ActivatedRouteSnapshot', [], {
routeConfig: {
path: 'path'
}
})

const routerStateSnapshotSpy = jasmine.createSpyObj('RouterStateSnapshot', [''])

beforeEach(async () => {
canActivateGuard = new CanActivateGuard(translateServiceSpy, configSpy)
translateServiceSpy.setDefaultLang.calls.reset()
translateServiceSpy.use.calls.reset()
})

it('should return default lang if provided is not supported', () => {
const result = canActivateGuard.getBestMatchLanguage('pl')
expect(result).toBe('en')
})

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, routerStateSnapshotSpy) as Observable<boolean>
resultObs.subscribe({
next: (result) => {
expect(result).toBe(true)
doneFn()
},
error: () => {
doneFn.fail
}
})

expect(translateServiceSpy.setDefaultLang).toHaveBeenCalledOnceWith('en')
expect(console.log).toHaveBeenCalledWith('Start Translation guard - default language en')
expect(console.log).toHaveBeenCalledWith(`Translations guard done en`)
expect(console.log).toHaveBeenCalledWith(`Configuration language: pl`)
expect(translateServiceSpy.use).toHaveBeenCalledTimes(2)
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, routerStateSnapshotSpy) as Observable<boolean>
resultObs.subscribe({
next: (result) => {
expect(result).toBe(true)
doneFn()
},
error: () => {
doneFn.fail
}
})

expect(console.log).toHaveBeenCalledWith('Start Translation guard - default language en')
expect(console.log).toHaveBeenCalledWith(`Translations guard done en`)
expect(console.log).toHaveBeenCalledWith(`Configuration language: de`)
expect(translateServiceSpy.use).toHaveBeenCalledTimes(2)
expect(translateServiceSpy.use).toHaveBeenCalledWith('en')
expect(translateServiceSpy.use).toHaveBeenCalledWith('de')
})
})
2 changes: 1 addition & 1 deletion src/app/shared/can-active-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class CanActivateGuard implements CanActivate {
)
}

private getBestMatchLanguage(lang: string): string {
public getBestMatchLanguage(lang: string): string {
if (SUPPORTED_LANGUAGES.includes(lang)) {
return lang
} else {
Expand Down
70 changes: 70 additions & 0 deletions src/app/shared/image-container/image-container.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
import { NO_ERRORS_SCHEMA, SimpleChange } from '@angular/core'
import { ImageContainerComponent } from './image-container.component'
import { TranslateLoader, TranslateModule } from '@ngx-translate/core'
import { HttpLoaderFactory } from '../shared.module'
import { HttpClient } from '@angular/common/http'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { environment } from 'src/environments/environment'

describe('ThemeColorBoxComponent', () => {
let component: ImageContainerComponent
let fixture: ComponentFixture<ImageContainerComponent>

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [ImageContainerComponent],
imports: [
HttpClientTestingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents()
}))

beforeEach(() => {
fixture = TestBed.createComponent(ImageContainerComponent)
component = fixture.componentInstance
fixture.detectChanges()
})

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

it('should display placeholder on image error', () => {
component.onImageError()

expect(component.displayPlaceHolder).toBeTrue()
})

it('should use imageUrl on backend after change', () => {
const changes = {
imageUrl: new SimpleChange('', 'imageUrl', false)
}

component.imageUrl = 'imageUrl'

component.ngOnChanges(changes)

expect(component.imageUrl).toBe(environment.apiPrefix + 'imageUrl')
})

it('should use image from external resource after change', () => {
const changes = {
imageUrl: new SimpleChange('', 'http://web.com/imageUrl', false)
}

component.imageUrl = 'http://web.com/imageUrl'
component.ngOnChanges(changes)

expect(component.imageUrl).toBe('http://web.com/imageUrl')
})
})
43 changes: 43 additions & 0 deletions src/app/shared/label.resolver.spec.ts
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)
})
})
40 changes: 40 additions & 0 deletions src/app/shared/theme-color-box/theme-color-box.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
import { ThemeColorBoxComponent } from './theme-color-box.component'
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { TranslateLoader, TranslateModule } from '@ngx-translate/core'
import { HttpLoaderFactory } from '../shared.module'
import { HttpClient } from '@angular/common/http'

describe('ThemeColorBoxComponent', () => {
let component: ThemeColorBoxComponent
let fixture: ComponentFixture<ThemeColorBoxComponent>

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [ThemeColorBoxComponent],
imports: [
HttpClientTestingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents()
}))

beforeEach(() => {
fixture = TestBed.createComponent(ThemeColorBoxComponent)
component = fixture.componentInstance
fixture.detectChanges()
})

it('should create', () => {
expect(component).toBeTruthy()
})
})
8 changes: 8 additions & 0 deletions src/app/shared/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { limitText } from './utils'

describe('utils', () => {
it('should limit text if text too long', () => {
const result = limitText('textData', 4)
expect(result).toBe('text...')
})
})
Loading
Loading