Skip to content

Commit

Permalink
refactor: fix some tests
Browse files Browse the repository at this point in the history
Co-Authored-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr committed Oct 9, 2024
1 parent 03d90c2 commit cab7c82
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 152 deletions.
23 changes: 0 additions & 23 deletions projects/rero/ng-core/src/lib/dialog/dialog.component.html

This file was deleted.

2 changes: 2 additions & 0 deletions projects/rero/ng-core/src/lib/dialog/dialog.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { TranslateModule } from '@ngx-translate/core';
import { DynamicDialogConfig, DynamicDialogModule, DynamicDialogRef } from 'primeng/dynamicdialog';
import { Nl2brPipe } from '../pipe/nl2br.pipe';
import { DialogComponent } from './dialog.component';
import { ButtonModule } from 'primeng/button';

describe('DialogComponent', () => {
let component: DialogComponent;
Expand All @@ -28,6 +29,7 @@ describe('DialogComponent', () => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
ButtonModule,
DynamicDialogModule,
TranslateModule.forRoot()
],
Expand Down
13 changes: 9 additions & 4 deletions projects/rero/ng-core/src/lib/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
import { Component, inject } from '@angular/core';
import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';

/**
* Show dialog modal
*/
@Component({
selector: 'ng-core-dialog',
templateUrl: './dialog.component.html'
template: `
<div class="flex" [innerHtml]="config.data.body|nl2br"></div>
<div class="flex justify-content-end gap-2">
<p-button [label]="config.data.cancelTitleButton || 'Cancel' | translate" severity="secondary" (onClick)="cancel()" />
@if (config.data.confirmButton) {
<p-button [label]="config.data.confirmTitleButton || 'OK' | translate" (onClick)="confirm()" />
}
</div>
`
})
export class DialogComponent {

Expand Down
18 changes: 13 additions & 5 deletions projects/rero/ng-core/src/lib/pipe/get-record.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RERO angular core
* Copyright (C) 2020 RERO
* Copyright (C) 2020-2024 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand All @@ -17,6 +17,7 @@
import { Observable, of } from 'rxjs';
import { RecordService } from '../record/record.service';
import { GetRecordPipe } from './get-record.pipe';
import { TestBed } from '@angular/core/testing';

class RecordServiceMock {
getRecord(type: string, pid: string, resolve = 0): Observable<any> {
Expand All @@ -25,27 +26,34 @@ class RecordServiceMock {
}

describe('GetRecordPipe', () => {
let pipe: GetRecordPipe;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
GetRecordPipe,
{ provide: RecordService, useClass: RecordServiceMock }
]
})
pipe = TestBed.inject(GetRecordPipe);
});

it('create an instance', () => {
const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService);
expect(pipe).toBeTruthy();
});

it('transform with $ref return object', () => {
const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService);
pipe.transform('http://foo/1', 'resource').subscribe((result: object) => {
expect(result).toEqual({metadata: { pid: '1', name: 'foo' }});
});
});

it('transform with id return name', () => {
const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService);
pipe.transform('10', 'resource', 'field', 'name').subscribe((result: string) => {
expect(result).toEqual('foo');
});
});

it('transform return null', () => {
const pipe = new GetRecordPipe(new RecordServiceMock() as RecordService);
pipe.transform('12', 'resource', 'field', 'foo').subscribe((result: string) => {
expect(result).toBeNull();
});
Expand Down
37 changes: 18 additions & 19 deletions projects/rero/ng-core/src/lib/pipe/nl2br.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RERO angular core
* Copyright (C) 2020 RERO
* Copyright (C) 2020-2024 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand All @@ -14,37 +14,36 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { SecurityContext } from '@angular/core';
import { inject, TestBed } from '@angular/core/testing';
import { BrowserModule, DomSanitizer, ɵDomSanitizerImpl } from '@angular/platform-browser';
import { TestBed } from '@angular/core/testing';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';
import { Nl2brPipe } from './nl2br.pipe';

describe('Nl2brPipe', () => {
const sanitizer: DomSanitizer = new ɵDomSanitizerImpl(null);
let pipe: Nl2brPipe;
let domSanitizer: DomSanitizer;

beforeEach(() => {
TestBed
.configureTestingModule({
imports: [
BrowserModule
],
providers: [
Nl2brPipe,
{ provide: DomSanitizer, useValue: { bypassSecurityTrustHtml: (val: string) => val } }
]
});
pipe = TestBed.inject(Nl2brPipe);
domSanitizer = TestBed.inject(DomSanitizer);
});

it('convert carriage return to <br> html tags', inject([DomSanitizer], (domSanitizer: DomSanitizer) => {
const pipe = new Nl2brPipe(domSanitizer);
it('convert carriage return to <br> html tags', () => {
const safeText = pipe.transform('Text with\ncarriage return');
const sanitizedValue = sanitizer.sanitize(SecurityContext.HTML, safeText);

expect(sanitizedValue).toBe('Text with<br>\ncarriage return');
}));

it('should return empty string', inject([DomSanitizer], (domSanitizer: DomSanitizer) => {
const text: string = null;
const pipe = new Nl2brPipe(domSanitizer);
const safeText = pipe.transform(text);
const sanitizedValue = sanitizer.sanitize(SecurityContext.HTML, safeText);
expect(safeText).toBe('Text with<br>\ncarriage return')
});

expect(sanitizedValue).toBe('');
}));
it('should return empty string', () => {
const safeText = pipe.transform(null);
expect(safeText).toBe('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class EditorComponent extends AbstractCanDeactivateComponent implements O
protected location: Location = inject(Location);
protected routeCollectionService: RouteCollectionService = inject(RouteCollectionService);
protected loggerService: LoggerService = inject(LoggerService);
protected jsonschemaService: JSONSchemaService
protected jsonschemaService: JSONSchemaService = inject(JSONSchemaService);
protected dialogService: DialogService = inject(DialogService);
protected messageService: MessageService = inject(MessageService);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RERO angular core
* Copyright (C) 2020 RERO
* Copyright (C) 2020-2024 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand All @@ -14,19 +14,32 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { TestBed } from '@angular/core/testing';
import { TranslateLanguagePipe } from './translate-language.pipe';
import { TranslateModule } from '@ngx-translate/core';
import { TranslateLanguageService } from './translate-language.service';

class TranslateLanguageMock {
class TranslateLanguageServiceMock {
translate(langCode: string) {
return langCode + '-translate';
}
}

describe('TranslateLanguagePipe', () => {
const pipe = new TranslateLanguagePipe(
new TranslateLanguageMock() as TranslateLanguageService
);
let pipe: TranslateLanguagePipe;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot()
],
providers: [
TranslateLanguagePipe,
{ provide: TranslateLanguageService, useClass: TranslateLanguageServiceMock }
]
});
pipe = TestBed.inject(TranslateLanguagePipe);
});

it('create an instance', () => {
expect(pipe).toBeTruthy();
Expand Down
24 changes: 13 additions & 11 deletions projects/rero/ng-core/src/lib/translate/translate-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RERO angular core
* Copyright (C) 2020 RERO
* Copyright (C) 2020-2024 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand All @@ -15,36 +15,38 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { TestBed } from '@angular/core/testing';
import { CoreConfigService } from '../core-config.service';
import { TranslateLoader } from './translate-loader';
import { HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateService, TranslateLoader as NgxTranslateLoader } from '@ngx-translate/core';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { CoreConfigService } from '../core-config.service';

describe('TranslateLoader', () => {
let translate: TranslateService;
let http: HttpTestingController;
let config = {};

beforeEach(() => {
config = {
languages: ['fr'],
translationsURLs: [
'/assets/i18n/${lang}.json'
]
};
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
TranslateModule.forRoot({
loader: {
provide: NgxTranslateLoader,
useFactory: (httpClient: HttpClient) => new TranslateLoader(config as CoreConfigService, httpClient),
useFactory: () => new TranslateLoader(),
deps: [HttpClient]
}
})
],
providers: [TranslateService]
providers: [
TranslateService,
{ provide: CoreConfigService, useValue: {
languages: ['fr'],
translationsURLs: [
'/assets/i18n/${lang}.json'
]
}
}
]
});
translate = TestBed.inject(TranslateService);
http = TestBed.inject(HttpTestingController);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class TranslateLoader implements BaseTranslateLoader {
url => {
const langURL = url.replace('${lang}', lang);
return this.http.get(langURL).pipe(
catchError((err) => {
catchError(() => {
console.log(`ERROR: Cannot load translation: ${langURL}`);
return of([]);
})
Expand Down

0 comments on commit cab7c82

Please sign in to comment.