From b06e03b3a4fd3e4e3099237b02369507983901e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Panzar?= Date: Fri, 2 Oct 2020 18:51:51 +0200 Subject: [PATCH] Migrate to latest Play version - addresses #92 Fixes UI unit tests. --- .../app/interceptors/auth.interceptor.spec.ts | 7 +++-- .../project-info.component.spec.ts | 4 ++- .../registration-page.component.spec.ts | 28 ++++++++++++++++++- ui/jest.config.js | 3 +- .../login-page/login-page.component.spec.ts | 13 ++++++++- .../project-infographic/testing/index.ts | 1 + .../project-infographic-testing.module.ts | 18 ++++++++++++ .../src/lib/modules/project/testing.ts | 1 + .../src/lib/modules/testing.ts | 1 + .../src/lib/modules/user/testing.ts | 1 + .../user-edit-dialog.component.html | 2 +- .../user-edit-dialog.component.spec.ts | 3 ++ .../user/user-edit-form/testing/index.ts | 1 + .../testing/user-edit-form-testing.module.ts | 25 +++++++++++++++++ .../user-edit-form.component.spec.ts | 17 ++++++++++- .../user-edit-form.component.ts | 4 +++ .../src/lib/services/error-handler.ts | 4 +++ ui/package-lock.json | 5 ++++ ui/package.json | 1 + 19 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/index.ts create mode 100644 ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/project-infographic-testing.module.ts create mode 100644 ui/libs/translatr-components/src/lib/modules/project/testing.ts create mode 100644 ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/index.ts create mode 100644 ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/user-edit-form-testing.module.ts diff --git a/ui/apps/translatr/src/app/interceptors/auth.interceptor.spec.ts b/ui/apps/translatr/src/app/interceptors/auth.interceptor.spec.ts index 3524c577..03f8336b 100644 --- a/ui/apps/translatr/src/app/interceptors/auth.interceptor.spec.ts +++ b/ui/apps/translatr/src/app/interceptors/auth.interceptor.spec.ts @@ -1,11 +1,14 @@ import { TestBed } from '@angular/core/testing'; +import { RouterTestingModule } from '@angular/router/testing'; +import { LOGIN_URL } from '@translatr/utils'; import { AuthInterceptor } from './auth.interceptor'; -describe('ErrorInterceptor', () => { +describe('AuthInterceptor', () => { beforeEach(() => TestBed.configureTestingModule({ - providers: [AuthInterceptor] + providers: [AuthInterceptor, { provide: LOGIN_URL, useValue: '' }], + imports: [RouterTestingModule] }) ); diff --git a/ui/apps/translatr/src/app/modules/pages/project-page/project-info/project-info.component.spec.ts b/ui/apps/translatr/src/app/modules/pages/project-page/project-info/project-info.component.spec.ts index 5efa3b08..a9b6b5d3 100644 --- a/ui/apps/translatr/src/app/modules/pages/project-page/project-info/project-info.component.spec.ts +++ b/ui/apps/translatr/src/app/modules/pages/project-page/project-info/project-info.component.spec.ts @@ -16,7 +16,8 @@ import { WINDOW } from '@translatr/utils'; import { EmptyViewTestingModule, FeatureFlagTestingModule, - MetricTestingModule + MetricTestingModule, + ProjectInfographicTestingModule } from '@translatr/components/testing'; import { NavListTestingModule } from '../../../shared/nav-list/testing'; import { ActivityListTestingModule } from '../../../shared/activity-list/testing'; @@ -38,6 +39,7 @@ describe('ProjectInfoComponent', () => { ShortNumberModule, FeatureFlagTestingModule, EmptyViewTestingModule, + ProjectInfographicTestingModule, RouterTestingModule, diff --git a/ui/apps/translatr/src/app/modules/pages/registration-page/registration-page.component.spec.ts b/ui/apps/translatr/src/app/modules/pages/registration-page/registration-page.component.spec.ts index d895f28d..e7a143d7 100644 --- a/ui/apps/translatr/src/app/modules/pages/registration-page/registration-page.component.spec.ts +++ b/ui/apps/translatr/src/app/modules/pages/registration-page/registration-page.component.spec.ts @@ -1,4 +1,15 @@ +import { HttpClientTestingModule } from '@angular/common/http/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { MatButtonModule } from '@angular/material/button'; +import { MatCardModule } from '@angular/material/card'; +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { RouterTestingModule } from '@angular/router/testing'; +import { FeatureFlagModule, UserEditFormModule } from '@dev/translatr-components'; +import { LanguageProvider } from '@dev/translatr-sdk'; +import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'; +import { TranslocoModule } from '@ngneat/transloco'; +import { SidenavModule } from '../../nav/sidenav/sidenav.module'; +import { RegistrationPageRoutingModule } from './registration-page-routing.module'; import { RegistrationPageComponent } from './registration-page.component'; @@ -8,7 +19,22 @@ describe('RegistrationPageComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [RegistrationPageComponent] + declarations: [RegistrationPageComponent], + imports: [ + HttpClientTestingModule, + RouterTestingModule, + NoopAnimationsModule, + + RegistrationPageRoutingModule, + SidenavModule, + TranslocoModule, + FeatureFlagModule, + UserEditFormModule, + MatCardModule, + MatButtonModule, + FontAwesomeModule + ], + providers: [LanguageProvider] }).compileComponents(); })); diff --git a/ui/jest.config.js b/ui/jest.config.js index f4d778f3..87bd2c81 100644 --- a/ui/jest.config.js +++ b/ui/jest.config.js @@ -1,7 +1,8 @@ module.exports = { testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], transform: { - '^.+\\.(ts|js|html)$': 'ts-jest' + '^.+\\.(ts|js|html)$': 'ts-jest', + '.+\\.(svg)$': 'jest-transform-stub' }, resolver: '@nrwl/jest/plugins/resolver', moduleFileExtensions: ['ts', 'js', 'html'], diff --git a/ui/libs/translatr-components/src/lib/modules/pages/login-page/login-page.component.spec.ts b/ui/libs/translatr-components/src/lib/modules/pages/login-page/login-page.component.spec.ts index d51d62fc..d63eb601 100644 --- a/ui/libs/translatr-components/src/lib/modules/pages/login-page/login-page.component.spec.ts +++ b/ui/libs/translatr-components/src/lib/modules/pages/login-page/login-page.component.spec.ts @@ -1,5 +1,8 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { MatCardModule } from '@angular/material/card'; import { MatIconModule } from '@angular/material/icon'; +import { RouterTestingModule } from '@angular/router/testing'; +import { FontAwesomeTestingModule } from '@fortawesome/angular-fontawesome/testing'; import { NavbarTestingModule } from '@translatr/components/testing'; import { AuthClientService } from '@translatr/translatr-sdk/src/lib/services/auth-client.service'; import { ENDPOINT_URL } from '@translatr/utils'; @@ -15,7 +18,15 @@ describe('LoginPageComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [LoginPageComponent], - imports: [NavbarTestingModule, MatIconModule], + imports: [ + RouterTestingModule, + + NavbarTestingModule, + + MatIconModule, + MatCardModule, + FontAwesomeTestingModule + ], providers: [ { provide: AuthClientService, diff --git a/ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/index.ts b/ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/index.ts new file mode 100644 index 00000000..97eb7b6b --- /dev/null +++ b/ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/index.ts @@ -0,0 +1 @@ +export * from './project-infographic-testing.module'; diff --git a/ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/project-infographic-testing.module.ts b/ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/project-infographic-testing.module.ts new file mode 100644 index 00000000..e4995000 --- /dev/null +++ b/ui/libs/translatr-components/src/lib/modules/project/project-infographic/testing/project-infographic-testing.module.ts @@ -0,0 +1,18 @@ +import { Component, Input, NgModule } from '@angular/core'; + +@Component({ + selector: 'dev-project-infographic', + template: '' +}) +export class MockProjectInfographicComponent { + @Input() contributorCount: number; + @Input() localeCount: number; + @Input() keyCount: number; + @Input() messageCount: number; +} + +@NgModule({ + declarations: [MockProjectInfographicComponent], + exports: [MockProjectInfographicComponent] +}) +export class ProjectInfographicTestingModule {} diff --git a/ui/libs/translatr-components/src/lib/modules/project/testing.ts b/ui/libs/translatr-components/src/lib/modules/project/testing.ts new file mode 100644 index 00000000..9a61d8ac --- /dev/null +++ b/ui/libs/translatr-components/src/lib/modules/project/testing.ts @@ -0,0 +1 @@ +export * from './project-infographic/testing'; diff --git a/ui/libs/translatr-components/src/lib/modules/testing.ts b/ui/libs/translatr-components/src/lib/modules/testing.ts index 7df1e47a..dd7099a5 100644 --- a/ui/libs/translatr-components/src/lib/modules/testing.ts +++ b/ui/libs/translatr-components/src/lib/modules/testing.ts @@ -8,4 +8,5 @@ export * from './filter-field/testing'; export * from './metric/testing'; export * from './nav/testing'; export * from './pages/testing'; +export * from './project/testing'; export * from './user/testing'; diff --git a/ui/libs/translatr-components/src/lib/modules/user/testing.ts b/ui/libs/translatr-components/src/lib/modules/user/testing.ts index 69267271..2453d297 100644 --- a/ui/libs/translatr-components/src/lib/modules/user/testing.ts +++ b/ui/libs/translatr-components/src/lib/modules/user/testing.ts @@ -1 +1,2 @@ export * from './user-card/testing'; +export * from './user-edit-form/testing'; diff --git a/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.html b/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.html index a1d939bf..d2d4d7f3 100644 --- a/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.html +++ b/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.html @@ -23,7 +23,7 @@ mat-flat-button color="primary" (click)="editForm.onSubmit()" - [disabled]="editForm.form.invalid" + [disabled]="editForm.invalid" transloco="button.save" >Save diff --git a/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.spec.ts b/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.spec.ts index 23e9442b..82f07d40 100644 --- a/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.spec.ts +++ b/ui/libs/translatr-components/src/lib/modules/user/user-edit-dialog/user-edit-dialog.component.spec.ts @@ -8,6 +8,7 @@ import { MatSelectModule } from '@angular/material/select'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { UserEditDialogComponent } from '@dev/translatr-components'; import { TranslocoTestingModule } from '@ngneat/transloco'; +import { UserEditFormTestingModule } from '@translatr/components/testing'; import { mockObservable } from '@translatr/utils/testing'; describe('UserEditDialogComponent', () => { @@ -22,6 +23,8 @@ describe('UserEditDialogComponent', () => { NoopAnimationsModule, TranslocoTestingModule, + UserEditFormTestingModule, + MatFormFieldModule, MatDialogModule, MatInputModule, diff --git a/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/index.ts b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/index.ts new file mode 100644 index 00000000..4cef530a --- /dev/null +++ b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/index.ts @@ -0,0 +1 @@ +export * from './user-edit-form-testing.module'; diff --git a/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/user-edit-form-testing.module.ts b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/user-edit-form-testing.module.ts new file mode 100644 index 00000000..a8001429 --- /dev/null +++ b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/testing/user-edit-form-testing.module.ts @@ -0,0 +1,25 @@ +import { Component, EventEmitter, Input, NgModule, Output } from '@angular/core'; +import { ConstraintViolationErrorInfo, User } from '@dev/translatr-model'; + +@Component({ + selector: 'dev-user-edit-form', + template: '' +}) +export class MockUserEditFormComponent { + @Input() user: User; + @Input() errors: ConstraintViolationErrorInfo; + + @Output() edit = new EventEmitter(); + + get invalid(): boolean { + return true; + } + + onSubmit() {} +} + +@NgModule({ + declarations: [MockUserEditFormComponent], + exports: [MockUserEditFormComponent] +}) +export class UserEditFormTestingModule {} diff --git a/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.spec.ts b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.spec.ts index 905938ad..0b6b9931 100644 --- a/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.spec.ts +++ b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.spec.ts @@ -1,4 +1,10 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { ReactiveFormsModule } from '@angular/forms'; +import { MatFormFieldModule } from '@angular/material/form-field'; +import { MatInputModule } from '@angular/material/input'; +import { MatSelectModule } from '@angular/material/select'; +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { TranslocoModule } from '@ngneat/transloco'; import { UserEditFormComponent } from './user-edit-form.component'; @@ -8,7 +14,16 @@ describe('UserEditFormComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [UserEditFormComponent] + declarations: [UserEditFormComponent], + imports: [ + ReactiveFormsModule, + TranslocoModule, + NoopAnimationsModule, + + MatFormFieldModule, + MatInputModule, + MatSelectModule + ] }).compileComponents(); })); diff --git a/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.ts b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.ts index f416fc4c..aa75b6bf 100644 --- a/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.ts +++ b/ui/libs/translatr-components/src/lib/modules/user/user-edit-form/user-edit-form.component.ts @@ -65,4 +65,8 @@ export class UserEditFormComponent implements OnInit { ? this.form.value.allowedRoles.indexOf(role) !== -1 : false; } + + get invalid(): boolean { + return this.form.invalid; + } } diff --git a/ui/libs/translatr-sdk/src/lib/services/error-handler.ts b/ui/libs/translatr-sdk/src/lib/services/error-handler.ts index abd10cab..da693376 100644 --- a/ui/libs/translatr-sdk/src/lib/services/error-handler.ts +++ b/ui/libs/translatr-sdk/src/lib/services/error-handler.ts @@ -1,4 +1,5 @@ import { HttpErrorResponse } from '@angular/common/http'; +import { Injectable } from '@angular/core'; import { Observable, throwError } from 'rxjs'; export interface RestRequest { @@ -8,6 +9,9 @@ export interface RestRequest { path: string; } +@Injectable({ + providedIn: 'root' +}) export class ErrorHandler { handleError(error: HttpErrorResponse, request?: RestRequest): Observable { return throwError(error); diff --git a/ui/package-lock.json b/ui/package-lock.json index 456802d3..f1b73086 100644 --- a/ui/package-lock.json +++ b/ui/package-lock.json @@ -16221,6 +16221,11 @@ "semver": "^6.2.0" } }, + "jest-transform-stub": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/jest-transform-stub/-/jest-transform-stub-2.0.0.tgz", + "integrity": "sha512-lspHaCRx/mBbnm3h4uMMS3R5aZzMwyNpNIJLXj4cEsV0mIUtS4IjYJLSoyjRCtnxb6RIGJ4NL2quZzfIeNhbkg==" + }, "jest-util": { "version": "24.9.0", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-24.9.0.tgz", diff --git a/ui/package.json b/ui/package.json index 3ff085c7..e9a830ec 100644 --- a/ui/package.json +++ b/ui/package.json @@ -103,6 +103,7 @@ "core-js": "^2.6.11", "d3": "5.16.0", "d3-time": "^1.1.0", + "jest-transform-stub": "2.0.0", "messageformat": "2.3.0", "moment": "2.26.0", "ngx-gravatar": "^7.2.2",