Browse Source

test(theme-shared): refactor error handler spec

pull/1826/head
mehmet-erim 7 years ago
parent
commit
3f816b25bd
  1. 1
      npm/ng-packs/package.json
  2. 0
      npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.scss
  3. 2
      npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts
  4. 2
      npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts
  5. 81
      npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts
  6. 2
      npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts

1
npm/ng-packs/package.json

@ -48,6 +48,7 @@
"karma-jasmine-html-reporter": "^1.4.0", "karma-jasmine-html-reporter": "^1.4.0",
"ng-packagr": "^5.5.0", "ng-packagr": "^5.5.0",
"ngx-perfect-scrollbar": "^8.0.0", "ngx-perfect-scrollbar": "^8.0.0",
"ngxs-reset-plugin": "^1.2.0",
"primeicons": "^1.0.0", "primeicons": "^1.0.0",
"primeng": "^8.0.0", "primeng": "^8.0.0",
"protractor": "~5.4.0", "protractor": "~5.4.0",

0
npm/ng-packs/packages/theme-shared/src/lib/components/errors/error.component.scss → npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.scss

2
npm/ng-packs/packages/theme-shared/src/lib/components/errors/error.component.ts → npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts

@ -4,7 +4,7 @@ import { Component, Renderer2, ElementRef } from '@angular/core';
selector: 'abp-error', selector: 'abp-error',
template: ` template: `
<div class="error"> <div class="error">
<button id="abp-close-button mr-2" type="button" class="close" (click)="destroy()"> <button id="abp-close-button mr-4" type="button" class="close" (click)="destroy()">
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
<div class="row centered"> <div class="row centered">

2
npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

@ -11,7 +11,7 @@ import {
import { Navigate, RouterState } from '@ngxs/router-plugin'; import { Navigate, RouterState } from '@ngxs/router-plugin';
import { Actions, ofActionSuccessful, Store } from '@ngxs/store'; import { Actions, ofActionSuccessful, Store } from '@ngxs/store';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { ErrorComponent } from '../components/errors/error.component'; import { ErrorComponent } from '../components/error/error.component';
import { Toaster } from '../models/toaster'; import { Toaster } from '../models/toaster';
import { ConfirmationService } from '../services/confirmation.service'; import { ConfirmationService } from '../services/confirmation.service';
import snq from 'snq'; import snq from 'snq';

81
npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts

@ -1,9 +1,9 @@
import { CoreModule, RestOccurError, RouterOutletComponent } from '@abp/ng.core'; import { ConfigState, CoreModule, RestOccurError, RouterOutletComponent } from '@abp/ng.core';
import { Location } from '@angular/common'; import { Location } from '@angular/common';
import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule } from '@angular/router';
import { createHostFactory, SpectatorHost } from '@ngneat/spectator'; import { createRoutingFactory, SpectatorRouting } from '@ngneat/spectator';
import { RouterState } from '@ngxs/router-plugin'; import { RouterState } from '@ngxs/router-plugin';
import { NgxsModule, Store } from '@ngxs/store'; import { NgxsModule, Store } from '@ngxs/store';
import { NgxsResetPluginModule, StateOverwrite } from 'ngxs-reset-plugin'; import { NgxsResetPluginModule, StateOverwrite } from 'ngxs-reset-plugin';
@ -16,87 +16,84 @@ class DummyComponent {
} }
describe('With Custom Host Component', function() { describe('With Custom Host Component', function() {
let host: SpectatorHost<DummyComponent>; let component: SpectatorRouting<DummyComponent>;
const createHost = createHostFactory({ const createComponent = createRoutingFactory({
component: DummyComponent, component: DummyComponent,
imports: [ imports: [CoreModule, ThemeSharedModule.forRoot(), NgxsModule.forRoot([]), NgxsResetPluginModule.forRoot()],
CoreModule, stubsEnabled: false,
ThemeSharedModule.forRoot(), routes: [{ path: '', component: DummyComponent }, { path: 'account/login', component: RouterOutletComponent }],
NgxsModule.forRoot([]),
NgxsResetPluginModule.forRoot(),
RouterModule.forRoot([{ path: 'account/login', component: RouterOutletComponent }]),
],
}); });
beforeEach(() => { beforeEach(() => {
host = createHost(`<dummy></dummy>`); component = createComponent();
const abpError = document.querySelector('abp-error'); const abpError = document.querySelector('abp-error');
if (abpError) document.body.removeChild(abpError); if (abpError) document.body.removeChild(abpError);
}); });
it('should display the error component when server error occurs', () => { it('should display the error component when server error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 500 }))); component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 500 })));
host.detectChanges(); component.detectChanges();
expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError500.title); expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError500.title);
expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError500.details); expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError500.details);
}); });
it('should display the error component when authorize error occurs', () => { it('should display the error component when authorize error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 403 }))); component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 403 })));
host.detectChanges(); component.detectChanges();
expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError403.title); expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError403.title);
expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError403.details); expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError403.details);
}); });
it('should display the error component when unknown error occurs', () => { it('should display the error component when unknown error occurs', () => {
host.component.store.dispatch( component.component.store.dispatch(
new RestOccurError(new HttpErrorResponse({ status: 0, statusText: 'Unknown Error' })), new RestOccurError(new HttpErrorResponse({ status: 0, statusText: 'Unknown Error' })),
); );
host.detectChanges(); component.detectChanges();
expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultErrorUnknown.title); expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultErrorUnknown.title);
expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultErrorUnknown.details); expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultErrorUnknown.details);
}); });
it('should display the confirmation when not found error occurs', () => { it('should display the confirmation when not found error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 404 }))); component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 404 })));
host.detectChanges(); component.detectChanges();
expect(host.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.title); expect(component.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.title);
expect(host.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.details); expect(component.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.details);
}); });
it('should display the confirmation when default error occurs', () => { it('should display the confirmation when default error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 412 }))); component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 412 })));
host.detectChanges(); component.detectChanges();
expect(host.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.title); expect(component.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.title);
expect(host.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.details); expect(component.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.details);
}); });
it('should display the confirmation when authenticated error occurs', async () => { it('should display the confirmation when authenticated error occurs', async () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401 }))); component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401 })));
host.detectChanges(); component.detectChanges();
host.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }]));
host.click('#confirm'); component.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }]));
await host.fixture.whenStable(); component.click('#confirm');
expect(host.get(Location).path()).toBe('/account/login'); await component.fixture.whenStable();
expect(component.get(Location).path()).toBe('/account/login');
}); });
it('should display the confirmation when authenticated error occurs with _AbpErrorFormat header', async () => { it('should display the confirmation when authenticated error occurs with _AbpErrorFormat header', async () => {
let headers: HttpHeaders = new HttpHeaders(); let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('_AbpErrorFormat', '_AbpErrorFormat'); headers = headers.append('_AbpErrorFormat', '_AbpErrorFormat');
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401, headers }))); component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401, headers })));
host.detectChanges(); component.detectChanges();
host.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }])); component.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }]));
host.click('#confirm'); component.click('#confirm');
await host.fixture.whenStable(); await component.fixture.whenStable();
expect(host.get(Location).path()).toBe('/account/login'); expect(component.get(Location).path()).toBe('/account/login');
}); });
it('should display the confirmation when error occurs with _AbpErrorFormat header', () => { it('should display the confirmation when error occurs with _AbpErrorFormat header', () => {
let headers: HttpHeaders = new HttpHeaders(); let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('_AbpErrorFormat', '_AbpErrorFormat'); headers = headers.append('_AbpErrorFormat', '_AbpErrorFormat');
host.component.store.dispatch( component.component.store.dispatch(
new RestOccurError( new RestOccurError(
new HttpErrorResponse({ new HttpErrorResponse({
error: { error: { message: 'test message', details: 'test detail' } }, error: { error: { message: 'test message', details: 'test detail' } },
@ -105,9 +102,9 @@ describe('With Custom Host Component', function() {
}), }),
), ),
); );
host.detectChanges(); component.detectChanges();
expect(host.query('.abp-confirm-summary')).toHaveText('test message'); expect(component.query('.abp-confirm-summary')).toHaveText('test message');
expect(host.query('.abp-confirm-body')).toHaveText('test detail'); expect(component.query('.abp-confirm-body')).toHaveText('test detail');
}); });
}); });

2
npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts

@ -9,7 +9,7 @@ import { ButtonComponent } from './components/button/button.component';
import { ChangePasswordComponent } from './components/change-password/change-password.component'; import { ChangePasswordComponent } from './components/change-password/change-password.component';
import { ChartComponent } from './components/chart/chart.component'; import { ChartComponent } from './components/chart/chart.component';
import { ConfirmationComponent } from './components/confirmation/confirmation.component'; import { ConfirmationComponent } from './components/confirmation/confirmation.component';
import { ErrorComponent } from './components/errors/error.component'; import { ErrorComponent } from './components/error/error.component';
import { LoaderBarComponent } from './components/loader-bar/loader-bar.component'; import { LoaderBarComponent } from './components/loader-bar/loader-bar.component';
import { ModalComponent } from './components/modal/modal.component'; import { ModalComponent } from './components/modal/modal.component';
import { ProfileComponent } from './components/profile/profile.component'; import { ProfileComponent } from './components/profile/profile.component';

Loading…
Cancel
Save